admin: New routes to expose git repositories status

This commit is contained in:
nemunaire 2023-10-23 12:03:40 +02:00
parent 598b34eb4f
commit b08039c997
8 changed files with 265 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import (
"log"
"os"
"path"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
@ -131,3 +132,83 @@ func (i GitImporter) Sync() error {
})
return err
}
func (i GitImporter) GetSubmodules() ([]GitSubmoduleStatus, error) {
oneGitPull.Lock()
defer oneGitPull.Unlock()
r, err := git.PlainOpen(i.li.Base)
if err != nil {
return nil, err
}
w, err := r.Worktree()
if err != nil {
return nil, err
}
modules, err := w.Submodules()
var ret []GitSubmoduleStatus
for _, mod := range modules {
st, err := mod.Status()
if err == nil {
ret = append(ret, GitSubmoduleStatus{
Hash: st.Expected.String(),
Path: st.Path,
Branch: strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(st.Branch.String(), "("), "refs/"), "heads/"), ")"),
})
}
}
return ret, err
}
func (i GitImporter) GetSubmodule(repopath string) (*GitSubmoduleStatus, error) {
oneGitPull.Lock()
defer oneGitPull.Unlock()
r, err := git.PlainOpen(path.Join(i.li.Base, repopath))
if err != nil {
return nil, err
}
st, err := r.Head()
if err != nil {
return nil, err
}
return &GitSubmoduleStatus{
Hash: st.Hash().String(),
Path: repopath,
Branch: st.Name().Short(),
}, nil
}
func (i GitImporter) IsRepositoryUptodate(repopath string) (*GitSubmoduleStatus, error) {
oneGitPull.Lock()
defer oneGitPull.Unlock()
r, err := git.PlainOpen(path.Join(i.li.Base, repopath))
if err != nil {
return nil, err
}
// Perform a git pull --rebase origin/master
err = r.Fetch(&git.FetchOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{config.RefSpec("+refs/heads/" + i.Branch + ":refs/remotes/origin/" + i.Branch)},
Auth: i.Auth,
})
st, err := r.Reference(plumbing.ReferenceName("origin/"+i.Branch), true)
if err != nil {
return nil, err
}
return &GitSubmoduleStatus{
Hash: st.Hash().String(),
Path: repopath,
Branch: st.Name().Short(),
}, nil
}