Something went wrong. Try again.
Mirror of https://git.jolheiser.com/ugit
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129package git
import ( "encoding/json" "errors" "fmt" "io/fs" "os" "path/filepath" "slices")
// RepoMeta is the meta information a Repo can havetype RepoMeta struct { Description string `json:"description"` Private bool `json:"private"` Tags TagSet `json:"tags"`}
// TagSet is a Set of tagstype TagSet map[string]struct{}
// Add adds a tag to the setfunc (t TagSet) Add(tag string) { t[tag] = struct{}{}}
// Remove removes a tag from the setfunc (t TagSet) Remove(tag string) { delete(t, tag)}
// Contains checks if a tag is in the setfunc (t TagSet) Contains(tag string) bool { _, ok := t[tag] return ok}
// Slice returns the set as a (sorted) slicefunc (t TagSet) Slice() []string { s := make([]string, 0, len(t)) for k := range t { s = append(s, k) } slices.Sort(s) return s}
// MarshalJSON implements [json.Marshaler]func (t TagSet) MarshalJSON() ([]byte, error) { return json.Marshal(t.Slice())}
// UnmarshalJSON implements [json.Unmarshaler]func (t *TagSet) UnmarshalJSON(b []byte) error { if *t == nil { ts := make(TagSet) t = &ts } var s []string if err := json.Unmarshal(b, &s); err != nil { return err } for _, ss := range s { t.Add(ss) } return nil}
// Update updates meta given another RepoMetafunc (m *RepoMeta) Update(meta RepoMeta) error { data, err := json.Marshal(meta) if err != nil { return err } return json.Unmarshal(data, m)}
func (r Repo) metaPath() string { return filepath.Join(r.path, "ugit.json")}
// SaveMeta saves the meta info of a Repofunc (r Repo) SaveMeta() error { // Compatibility with gitweb, because why not // Ignoring the error because it's not technically detrimental to ugit desc, err := os.Create(filepath.Join(r.path, "description")) if err == nil { defer desc.Close() _, _ = desc.WriteString(r.Meta.Description) }
fi, err := os.Create(r.metaPath()) if err != nil { return err } defer fi.Close() return json.NewEncoder(fi).Encode(r.Meta)}
var defaultMeta = func() []byte { b, err := json.Marshal(RepoMeta{ Private: true, }) if err != nil { panic(fmt.Sprintf("could not init default meta: %v", err)) } return b}()
func ensureJSONFile(path string) error { _, err := os.Stat(path) if err == nil { return nil } if !errors.Is(err, fs.ErrNotExist) { return err } fi, err := os.Create(path) if err != nil { return err } defer fi.Close() if _, err := fi.Write(defaultMeta); err != nil { return err } return nil}