6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-06 05:52:58 +00:00

Add a "dump" output option to git-list

It prints a list of all repos URL and is supposed to be consumed by `git get --dump` option.
This commit is contained in:
Grzegorz Dlugoszewski
2020-06-18 11:05:39 +02:00
parent edf60064a8
commit da8f0931d0
4 changed files with 50 additions and 12 deletions

37
pkg/print/dump.go Normal file
View File

@@ -0,0 +1,37 @@
package print
import (
"git-get/pkg/git"
"strings"
)
type DumpPrinter struct{}
// Print generates a list of repos URLs. Each line contains a URL and, if applicable, a currently checked out branch name.
// It's a way to dump all repositories managed by git-get and is supposed to be consumed by `git get --dump`.
func (p *DumpPrinter) Print(_ string, repos []*git.Repo) string {
var str strings.Builder
for i, repo := range repos {
remotes, err := repo.Remotes()
if err != nil || len(remotes) == 0 {
continue
}
// TODO: Needs work. Right now we're just assuming the first remote is the origin one and the one from which the current branch is checked out.
url := remotes[0].Config().URLs[0]
current := repo.Status.CurrentBranch
str.WriteString(url)
if current != git.StatusDetached && current != git.StatusUnknown {
str.WriteString(" " + current)
}
if i < len(repos)-1 {
str.WriteString("\n")
}
}
return str.String()
}