mirror of
https://github.com/grdl/git-get.git
synced 2026-02-15 05:15:12 +00:00
Add a --fetch flag to first fetch from remotes before printing repos status
This commit is contained in:
@@ -10,8 +10,11 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Gitconfig section name and env var prefix
|
||||
const GitgetPrefix = "gitget"
|
||||
|
||||
// Flag keys and default values
|
||||
const (
|
||||
GitgetPrefix = "gitget"
|
||||
KeyReposRoot = "reposRoot"
|
||||
DefReposRoot = "repositories"
|
||||
KeyDefaultHost = "defaultHost"
|
||||
@@ -20,8 +23,11 @@ const (
|
||||
DefPrivateKey = "id_rsa"
|
||||
KeyOutput = "out"
|
||||
DefOutput = OutFlat
|
||||
KeyFetch = "fetch"
|
||||
KeyList = "list"
|
||||
)
|
||||
|
||||
// Allowed values for the --out flag
|
||||
const (
|
||||
OutFlat = "flat"
|
||||
OutSmart = "smart"
|
||||
|
||||
12
cmd/main.go
12
cmd/main.go
@@ -28,13 +28,15 @@ var cmd = &cobra.Command{
|
||||
Version: fmt.Sprintf("%s - %s, build at %s", version, commit, date),
|
||||
}
|
||||
|
||||
var list bool
|
||||
|
||||
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.KeyPrivateKey, "p", "", "SSH private key path")
|
||||
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.KeyPrivateKey, cmd.PersistentFlags().Lookup(cfg.KeyReposRoot))
|
||||
viper.BindPFlag(cfg.KeyOutput, cmd.PersistentFlags().Lookup(cfg.KeyOutput))
|
||||
@@ -44,7 +46,7 @@ func Run(cmd *cobra.Command, args []string) {
|
||||
cfg.InitConfig()
|
||||
|
||||
root := viper.GetString(cfg.KeyReposRoot)
|
||||
if list {
|
||||
if viper.GetBool(cfg.KeyList) {
|
||||
// TODO: move it to OpenAll and don't export
|
||||
paths, err := path.FindRepos()
|
||||
exitIfError(err)
|
||||
@@ -61,7 +63,7 @@ func Run(cmd *cobra.Command, args []string) {
|
||||
case cfg.OutSmart:
|
||||
printer = &print.SmartTreePrinter{}
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"git-get/cfg"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"github.com/go-git/go-billy/v5/osfs"
|
||||
|
||||
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
||||
@@ -39,12 +42,20 @@ type BranchStatus struct {
|
||||
}
|
||||
|
||||
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()
|
||||
if err != nil {
|
||||
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
|
||||
// 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(""))
|
||||
|
||||
Reference in New Issue
Block a user