summaryrefslogtreecommitdiffstats
path: root/pkg/commands
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2021-03-30 22:17:42 +1100
committerJesse Duffield <jessedduffield@gmail.com>2021-04-01 09:13:29 +1100
commit8af3fe3b4ab46373440e5ec934964f05004f6727 (patch)
tree7d521b41af6bb9a327ed40cb7e93b01a43230073 /pkg/commands
parent3103247e8f6b24bc2c29739d052036f98c11859e (diff)
faster startup
Diffstat (limited to 'pkg/commands')
-rw-r--r--pkg/commands/git.go24
-rw-r--r--pkg/commands/git_test.go39
2 files changed, 17 insertions, 46 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 0fc6e8739..0db74254e 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -54,10 +54,6 @@ func NewGitCommand(log *logrus.Entry, osCommand *oscommands.OSCommand, tr *i18n.
pushToCurrent = strings.TrimSpace(output) == "current"
}
- if err := verifyInGitRepo(osCommand.RunCommand); err != nil {
- return nil, err
- }
-
if err := navigateToRepoRootDirectory(os.Stat, os.Chdir); err != nil {
return nil, err
}
@@ -88,10 +84,6 @@ func NewGitCommand(log *logrus.Entry, osCommand *oscommands.OSCommand, tr *i18n.
return gitCommand, nil
}
-func verifyInGitRepo(runCmd func(string, ...interface{}) error) error {
- return runCmd("git status")
-}
-
func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error {
gitDir := env.GetGitDirEnv()
if gitDir != "" {
@@ -120,6 +112,18 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f
if err = chdir(".."); err != nil {
return utils.WrapError(err)
}
+
+ currentPath, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ atRoot := currentPath == filepath.Dir(currentPath)
+ if atRoot {
+ // we should never really land here: the code that creates GitCommand should
+ // verify we're in a git directory
+ return errors.New("Must open lazygit in a git repository")
+ }
}
}
@@ -189,3 +193,7 @@ func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filenam
}
return strings.TrimSpace(strings.TrimPrefix(fileContent, "gitdir: ")), nil
}
+
+func VerifyInGitRepo(osCommand *oscommands.OSCommand) error {
+ return osCommand.RunCommand("git rev-parse --git-dir")
+}
diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go
index 90c32c7dd..ab0f9c5ac 100644
--- a/pkg/commands/git_test.go
+++ b/pkg/commands/git_test.go
@@ -61,43 +61,6 @@ func (f fileInfoMock) Sys() interface{} {
return f.sys
}
-// TestVerifyInGitRepo is a function.
-func TestVerifyInGitRepo(t *testing.T) {
- type scenario struct {
- testName string
- runCmd func(string, ...interface{}) error
- test func(error)
- }
-
- scenarios := []scenario{
- {
- "Valid git repository",
- func(string, ...interface{}) error {
- return nil
- },
- func(err error) {
- assert.NoError(t, err)
- },
- },
- {
- "Not a valid git repository",
- func(string, ...interface{}) error {
- return fmt.Errorf("fatal: Not a git repository (or any of the parent directories): .git")
- },
- func(err error) {
- assert.Error(t, err)
- assert.Regexp(t, `fatal: .ot a git repository \(or any of the parent directories\s?\/?\): \.git`, err.Error())
- },
- },
- }
-
- for _, s := range scenarios {
- t.Run(s.testName, func(t *testing.T) {
- s.test(verifyInGitRepo(s.runCmd))
- })
- }
-}
-
// TestNavigateToRepoRootDirectory is a function.
func TestNavigateToRepoRootDirectory(t *testing.T) {
type scenario struct {
@@ -233,7 +196,7 @@ func TestNewGitCommand(t *testing.T) {
},
func(gitCmd *GitCommand, err error) {
assert.Error(t, err)
- assert.Regexp(t, `fatal: .ot a git repository ((\(or any of the parent directories\): \.git)|(\(or any parent up to mount point \/\)))`, err.Error())
+ assert.Regexp(t, `Must open lazygit in a git repository`, err.Error())
},
},
{