admin: New routes to expose git repositories status
This commit is contained in:
parent
598b34eb4f
commit
b08039c997
8 changed files with 265 additions and 0 deletions
|
@ -245,3 +245,96 @@ func (i GitImporter) GetExerciceLink(e *fic.Exercice) (u *url.URL, err error) {
|
|||
|
||||
return
|
||||
}
|
||||
|
||||
func (i GitImporter) GetSubmodules() ([]GitSubmoduleStatus, error) {
|
||||
oneGitPull.Lock()
|
||||
defer oneGitPull.Unlock()
|
||||
|
||||
cmdsubmodule := exec.Command("git", "-C", i.li.Base, "submodule", "status")
|
||||
stdout, err := cmdsubmodule.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Git repository submodule failed: %s\n%s", err, stdout)
|
||||
return nil, fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
var ret []GitSubmoduleStatus
|
||||
for _, line := range strings.Split(string(stdout), "\n") {
|
||||
flds := strings.Fields(line)
|
||||
if len(flds) == 3 {
|
||||
ret = append(ret, GitSubmoduleStatus{
|
||||
Hash: flds[0],
|
||||
Path: flds[1],
|
||||
Branch: strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(flds[2], "("), "refs/"), "remotes/"), "heads/"), "origin/"), ")"),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (i GitImporter) GetSubmodule(repopath string) (*GitSubmoduleStatus, error) {
|
||||
oneGitPull.Lock()
|
||||
defer oneGitPull.Unlock()
|
||||
|
||||
if repopath == "" {
|
||||
cmdsubmodule := exec.Command("git", "-C", i.li.Base, "show", "-q", "--oneline")
|
||||
stdout, err := cmdsubmodule.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Git repository show failed: %s\n%s", err, stdout)
|
||||
return nil, fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
flds := strings.SplitN(string(stdout), " ", 2)
|
||||
return &GitSubmoduleStatus{
|
||||
Hash: flds[0],
|
||||
Text: strings.TrimSpace(flds[1]),
|
||||
Path: "",
|
||||
Branch: i.Branch,
|
||||
}, nil
|
||||
} else {
|
||||
cmdsubmodule := exec.Command("git", "-C", i.li.Base, "submodule", "status", repopath)
|
||||
stdout, err := cmdsubmodule.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Git repository submodule failed: %s\n%s", err, stdout)
|
||||
return nil, fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
flds := strings.Fields(strings.TrimSpace(string(stdout)))
|
||||
if len(flds) == 3 {
|
||||
return &GitSubmoduleStatus{
|
||||
Hash: flds[0],
|
||||
Path: flds[1],
|
||||
Branch: strings.TrimSuffix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(strings.TrimPrefix(flds[2], "("), "refs/"), "remotes/"), "heads/"), "origin/"), ")"),
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Unable to parse")
|
||||
}
|
||||
|
||||
func (i GitImporter) IsRepositoryUptodate(repopath string) (*GitSubmoduleStatus, error) {
|
||||
oneGitPull.Lock()
|
||||
defer oneGitPull.Unlock()
|
||||
|
||||
cmdsubmodule := exec.Command("git", "-C", path.Join(i.li.Base, repopath), "fetch", "origin", i.Branch)
|
||||
stdout, err := cmdsubmodule.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Git repository submodule fetch failed: %s\n%s", err, stdout)
|
||||
return nil, fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
cmdsubmodule = exec.Command("git", "-C", path.Join(i.li.Base, repopath), "show", "-q", "--oneline", "origin/"+i.Branch)
|
||||
stdout, err = cmdsubmodule.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Printf("Git repository submodule status failed: %s\n%s", err, stdout)
|
||||
return nil, fmt.Errorf("%w:\n%s", err, stdout)
|
||||
}
|
||||
|
||||
flds := strings.SplitN(string(stdout), " ", 2)
|
||||
return &GitSubmoduleStatus{
|
||||
Hash: flds[0],
|
||||
Text: strings.TrimSpace(flds[1]),
|
||||
Path: repopath,
|
||||
Branch: i.Branch,
|
||||
}, nil
|
||||
}
|
||||
|
|
Reference in a new issue