From a343a11ddb8e3d3755590d1a3786831ccc18888b Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Wed, 17 Aug 2016 16:13:36 -0600 Subject: [PATCH 1/3] check the git version is ok in some key commands --- commands/command_clone.go | 1 + commands/command_install.go | 5 ++++- commands/command_pre_push.go | 2 ++ commands/command_pull.go | 1 + commands/command_push.go | 2 ++ commands/command_track.go | 2 ++ commands/command_update.go | 1 + commands/commands.go | 38 ++++++++++++++++++++++-------------- git/git.go | 16 ++++++++++++++- 9 files changed, 51 insertions(+), 17 deletions(-) diff --git a/commands/command_clone.go b/commands/command_clone.go index dd0a37bb..5729b586 100644 --- a/commands/command_clone.go +++ b/commands/command_clone.go @@ -19,6 +19,7 @@ var ( ) func cloneCommand(cmd *cobra.Command, args []string) { + requireGitVersion() // We pass all args to git clone err := git.CloneWithoutFilters(cloneFlags, args) diff --git a/commands/command_install.go b/commands/command_install.go index 01e7519b..95dc42dd 100644 --- a/commands/command_install.go +++ b/commands/command_install.go @@ -1,9 +1,10 @@ package commands import ( + "os" + "github.com/github/git-lfs/lfs" "github.com/spf13/cobra" - "os" ) var ( @@ -14,6 +15,8 @@ var ( ) func installCommand(cmd *cobra.Command, args []string) { + requireGitVersion() + if localInstall { requireInRepo() } diff --git a/commands/command_pre_push.go b/commands/command_pre_push.go index fafb96c8..af147349 100644 --- a/commands/command_pre_push.go +++ b/commands/command_pre_push.go @@ -43,6 +43,8 @@ func prePushCommand(cmd *cobra.Command, args []string) { os.Exit(1) } + requireGitVersion() + // Remote is first arg if err := git.ValidateRemote(args[0]); err != nil { Exit("Invalid remote name %q", args[0]) diff --git a/commands/command_pull.go b/commands/command_pull.go index 036686d3..2f862960 100644 --- a/commands/command_pull.go +++ b/commands/command_pull.go @@ -8,6 +8,7 @@ import ( ) func pullCommand(cmd *cobra.Command, args []string) { + requireGitVersion() requireInRepo() if len(args) > 0 { diff --git a/commands/command_push.go b/commands/command_push.go index 0754385d..36bc1dc9 100644 --- a/commands/command_push.go +++ b/commands/command_push.go @@ -113,6 +113,8 @@ func pushCommand(cmd *cobra.Command, args []string) { os.Exit(1) } + requireGitVersion() + // Remote is first arg if err := git.ValidateRemote(args[0]); err != nil { Exit("Invalid remote name %q", args[0]) diff --git a/commands/command_track.go b/commands/command_track.go index f6df1923..a5ae0196 100644 --- a/commands/command_track.go +++ b/commands/command_track.go @@ -26,6 +26,8 @@ var ( ) func trackCommand(cmd *cobra.Command, args []string) { + requireGitVersion() + if config.LocalGitDir == "" { Print("Not a git repository.") os.Exit(128) diff --git a/commands/command_update.go b/commands/command_update.go index 8a683260..3f1ab78e 100644 --- a/commands/command_update.go +++ b/commands/command_update.go @@ -16,6 +16,7 @@ var ( // updateCommand is used for updating parts of Git LFS that reside under // .git/lfs. func updateCommand(cmd *cobra.Command, args []string) { + requireGitVersion() requireInRepo() lfsAccessRE := regexp.MustCompile(`\Alfs\.(.*)\.access\z`) diff --git a/commands/commands.go b/commands/commands.go index fe7bb070..e87447d1 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -118,23 +118,23 @@ func FullError(err error) { } func errorWith(err error, fatalErrFn func(error, string, ...interface{}), errFn func(string, ...interface{})) { - var innermsg string - if inner := errutil.GetInnerError(err); inner != nil { - innermsg = inner.Error() - } - - errmsg := err.Error() - if errmsg != innermsg { - Error(innermsg) - } - - if Debugging || errutil.IsFatalError(err) { - fatalErrFn(err, errmsg) - } else { - errFn(errmsg) - } + var innermsg string + if inner := errutil.GetInnerError(err); inner != nil { + innermsg = inner.Error() } + errmsg := err.Error() + if errmsg != innermsg { + Error(innermsg) + } + + if Debugging || errutil.IsFatalError(err) { + fatalErrFn(err, errmsg) + } else { + errFn(errmsg) + } +} + // Debug prints a formatted message if debugging is enabled. The formatted // message also shows up in the panic log, if created. func Debug(format string, args ...interface{}) { @@ -334,6 +334,14 @@ func isCommandEnabled(cfg *config.Configuration, cmd string) bool { return cfg.Os.Bool(fmt.Sprintf("GITLFS%sENABLED", strings.ToUpper(cmd)), false) } +func requireGitVersion() { + minimumGit := "1.8.2" + gitver, _ := git.Config.Version() + if !git.Config.IsGitVersionAtLeast(minimumGit) { + Exit(fmt.Sprintf("git version >= %s is required for Git LFS, your version: %s", minimumGit, gitver)) + } +} + func init() { log.SetOutput(ErrorWriter) } diff --git a/git/git.go b/git/git.go index e4a10ca0..14322f24 100644 --- a/git/git.go +++ b/git/git.go @@ -14,6 +14,7 @@ import ( "regexp" "strconv" "strings" + "sync" "time" "github.com/github/git-lfs/subprocess" @@ -306,6 +307,8 @@ func UpdateIndex(file string) error { } type gitConfig struct { + gitVersion string + mu sync.Mutex } var Config = &gitConfig{} @@ -398,7 +401,18 @@ func (c *gitConfig) ListFromFile(f string) (string, error) { // Version returns the git version func (c *gitConfig) Version() (string, error) { - return subprocess.SimpleExec("git", "version") + c.mu.Lock() + defer c.mu.Unlock() + + if len(c.gitVersion) == 0 { + v, err := subprocess.SimpleExec("git", "version") + if err != nil { + return v, err + } + c.gitVersion = v + } + + return c.gitVersion, nil } // IsVersionAtLeast returns whether the git version is the one specified or higher From 3d5b99df30379586910eb5fdd8104684e6db8c12 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Wed, 17 Aug 2016 16:17:35 -0600 Subject: [PATCH 2/3] don't ignore a 'git version' error --- commands/commands.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index e87447d1..1a18aa9d 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -336,9 +336,13 @@ func isCommandEnabled(cfg *config.Configuration, cmd string) bool { func requireGitVersion() { minimumGit := "1.8.2" - gitver, _ := git.Config.Version() + gitver, err := git.Config.Version() + if err != nil { + Exit("Error getting git version: %s", err) + } + if !git.Config.IsGitVersionAtLeast(minimumGit) { - Exit(fmt.Sprintf("git version >= %s is required for Git LFS, your version: %s", minimumGit, gitver)) + Exit("git version >= %s is required for Git LFS, your version: %s", minimumGit, gitver) } } From 18f863f2188a276341b32b46c8aba377ec944d99 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Wed, 17 Aug 2016 16:21:00 -0600 Subject: [PATCH 3/3] only re-run Version() if they don't match --- commands/commands.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 1a18aa9d..15bdf06c 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -336,12 +336,12 @@ func isCommandEnabled(cfg *config.Configuration, cmd string) bool { func requireGitVersion() { minimumGit := "1.8.2" - gitver, err := git.Config.Version() - if err != nil { - Exit("Error getting git version: %s", err) - } if !git.Config.IsGitVersionAtLeast(minimumGit) { + gitver, err := git.Config.Version() + if err != nil { + Exit("Error getting git version: %s", err) + } Exit("git version >= %s is required for Git LFS, your version: %s", minimumGit, gitver) } }