2024-01-15 22:26:51 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/fs"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// RepoMeta is the meta information a Repo can have
|
2024-01-15 22:26:51 +00:00
|
|
|
type RepoMeta struct {
|
|
|
|
Description string `json:"description"`
|
|
|
|
Private bool `json:"private"`
|
|
|
|
}
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// Update updates meta given another RepoMeta
|
2024-01-15 22:26:51 +00:00
|
|
|
func (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")
|
|
|
|
}
|
|
|
|
|
2024-01-19 04:41:16 +00:00
|
|
|
// SaveMeta saves the meta info of a Repo
|
2024-01-15 22:26:51 +00:00
|
|
|
func (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()
|
2024-01-19 04:41:16 +00:00
|
|
|
_, _ = desc.WriteString(r.Meta.Description)
|
2024-01-15 22:26:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Create(r.metaPath())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
return json.NewEncoder(fi).Encode(r.Meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.WriteString(`{"private":true}`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|