6
0
mirror of https://github.com/grdl/git-get.git synced 2026-02-11 09:29:17 +00:00

Add a --fetch flag to first fetch from remotes before printing repos status

This commit is contained in:
Grzegorz Dlugoszewski
2020-06-08 14:54:19 +02:00
parent e5c3285040
commit ee26ddc38f
3 changed files with 26 additions and 7 deletions

View File

@@ -10,8 +10,11 @@ import (
"github.com/spf13/viper" "github.com/spf13/viper"
) )
// Gitconfig section name and env var prefix
const GitgetPrefix = "gitget"
// Flag keys and default values
const ( const (
GitgetPrefix = "gitget"
KeyReposRoot = "reposRoot" KeyReposRoot = "reposRoot"
DefReposRoot = "repositories" DefReposRoot = "repositories"
KeyDefaultHost = "defaultHost" KeyDefaultHost = "defaultHost"
@@ -20,8 +23,11 @@ const (
DefPrivateKey = "id_rsa" DefPrivateKey = "id_rsa"
KeyOutput = "out" KeyOutput = "out"
DefOutput = OutFlat DefOutput = OutFlat
KeyFetch = "fetch"
KeyList = "list"
) )
// Allowed values for the --out flag
const ( const (
OutFlat = "flat" OutFlat = "flat"
OutSmart = "smart" OutSmart = "smart"

View File

@@ -28,13 +28,15 @@ var cmd = &cobra.Command{
Version: fmt.Sprintf("%s - %s, build at %s", version, commit, date), Version: fmt.Sprintf("%s - %s, build at %s", version, commit, date),
} }
var list bool
func init() { func init() {
cmd.PersistentFlags().BoolVarP(&list, "list", "l", false, "Lists all repositories inside git-get root") cmd.PersistentFlags().BoolP(cfg.KeyList, "l", false, "Lists all repositories inside git-get root")
cmd.PersistentFlags().BoolP(cfg.KeyFetch, "f", false, "Fetch from remotes when listing repositories")
cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root") cmd.PersistentFlags().StringP(cfg.KeyReposRoot, "r", "", "repos root")
cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path") cmd.PersistentFlags().StringP(cfg.KeyPrivateKey, "p", "", "SSH private key path")
cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.") cmd.PersistentFlags().StringP(cfg.KeyOutput, "o", cfg.DefOutput, "output format.")
viper.BindPFlag(cfg.KeyList, cmd.PersistentFlags().Lookup(cfg.KeyList))
viper.BindPFlag(cfg.KeyFetch, cmd.PersistentFlags().Lookup(cfg.KeyFetch))
viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyReposRoot, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot)) viper.BindPFlag(cfg.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput)) viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
@@ -44,7 +46,7 @@ func Run(cmd *cobra.Command, args []string) {
cfg.InitConfig() cfg.InitConfig()
root := viper.GetString(cfg.KeyReposRoot) root := viper.GetString(cfg.KeyReposRoot)
if list { if viper.GetBool(cfg.KeyList) {
// TODO: move it to OpenAll and don't export // TODO: move it to OpenAll and don't export
paths, err := path.FindRepos() paths, err := path.FindRepos()
exitIfError(err) exitIfError(err)
@@ -61,7 +63,7 @@ func Run(cmd *cobra.Command, args []string) {
case cfg.OutSmart: case cfg.OutSmart:
printer = &print.SmartTreePrinter{} printer = &print.SmartTreePrinter{}
default: default:
err = fmt.Errorf("invalid --output flag; allowed values: %v", []string{cfg.OutFlat, cfg.OutSimple, cfg.OutSmart}) err = fmt.Errorf("invalid --out flag; allowed values: %v", []string{cfg.OutFlat, cfg.OutSimple, cfg.OutSmart})
} }
exitIfError(err) exitIfError(err)

View File

@@ -1,9 +1,12 @@
package git package git
import ( import (
"git-get/cfg"
"sort" "sort"
"strings" "strings"
"github.com/spf13/viper"
"github.com/go-git/go-billy/v5/osfs" "github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/plumbing/format/gitignore" "github.com/go-git/go-git/v5/plumbing/format/gitignore"
@@ -39,12 +42,20 @@ type BranchStatus struct {
} }
func (r *Repo) LoadStatus() error { func (r *Repo) LoadStatus() error {
// Fetch from remotes if executed with --fetch flag. Ignore the "already up-to-date" errors.
if viper.GetBool(cfg.KeyFetch) {
err := r.Fetch()
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return errors.Wrap(err, "Failed fetching from remotes")
}
}
wt, err := r.Worktree() wt, err := r.Worktree()
if err != nil { if err != nil {
return errors.Wrap(err, "Failed getting worktree") return errors.Wrap(err, "Failed getting worktree")
} }
// worktree.Status doesn't load gitignore patterns that may be defined outside of .gitignore file using excludesfile // worktree.Status doesn't load gitignore patterns that are defined outside of .gitignore file using excludesfile.
// We need to load them explicitly here // We need to load them explicitly here
// TODO: variables are not expanded so if excludesfile is declared like "~/gitignore_global" or "$HOME/gitignore_global", this will fail to open it // TODO: variables are not expanded so if excludesfile is declared like "~/gitignore_global" or "$HOME/gitignore_global", this will fail to open it
globalPatterns, err := gitignore.LoadGlobalPatterns(osfs.New("")) globalPatterns, err := gitignore.LoadGlobalPatterns(osfs.New(""))