From 624d63d2fa90ad62998a62d8d17189cf0406864b Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 21:47:48 +0200 Subject: pkg/git : remove panic in SetupGit method --- pkg/commands/git.go | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 704a45c73..68571769e 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -15,6 +15,10 @@ import ( gogit "gopkg.in/src-d/go-git.v4" ) +// ErrGitRepositoryInvalid is emitted when we run a git command in a folder +// to check if we have a valid git repository and we get an error instead +var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") + // GitCommand is our main git interface type GitCommand struct { Log *logrus.Entry @@ -35,13 +39,20 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) } // SetupGit sets git repo up -func (c *GitCommand) SetupGit() { - c.verifyInGitRepo() - c.navigateToRepoRootDirectory() - if err := c.setupWorktree(); err != nil { - c.Log.Error(err) - panic(err) +func (c *GitCommand) SetupGit() error { + fs := []func() error{ + c.verifyInGitRepo, + c.navigateToRepoRootDirectory, + c.setupWorktree, + } + + for _, f := range fs { + if err := f(); err != nil { + return err + } } + + return nil } // GetStashEntries stash entryies @@ -145,11 +156,12 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return append(headResults, tailResults...) } -func (c *GitCommand) verifyInGitRepo() { - if output, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { - fmt.Println(output) - os.Exit(1) +func (c *GitCommand) verifyInGitRepo() error { + if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { + return ErrGitRepositoryInvalid } + + return nil } // GetBranchName branch name @@ -157,12 +169,19 @@ func (c *GitCommand) GetBranchName() (string, error) { return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") } -func (c *GitCommand) navigateToRepoRootDirectory() { - _, err := os.Stat(".git") - for os.IsNotExist(err) { +func (c *GitCommand) navigateToRepoRootDirectory() error { + for { + f, err := os.Stat(".git") + + if err == nil && f.IsDir() { + return nil + } + c.Log.Debug("going up a directory to find the root") - os.Chdir("..") - _, err = os.Stat(".git") + + if err = os.Chdir(".."); err != nil { + return err + } } } -- cgit v1.2.3 From c1984528c8b8ee2d6d9ffdc50f31c1f81c7b97a3 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Wed, 29 Aug 2018 22:55:57 +0200 Subject: pkg/git : add tests for SetupGit --- pkg/commands/git.go | 117 +++++++++++++++++++++++++++++----------------------- 1 file changed, 65 insertions(+), 52 deletions(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 68571769e..101a45515 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -19,21 +19,39 @@ import ( // to check if we have a valid git repository and we get an error instead var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") +func openGitRepositoryAndWorktree() (*gogit.Repository, *gogit.Worktree, error) { + r, err := gogit.PlainOpen(".") + + if err != nil { + return nil, nil, err + } + + w, err := r.Worktree() + + if err != nil { + return nil, nil, err + } + + return r, w, nil +} + // GitCommand is our main git interface type GitCommand struct { - Log *logrus.Entry - OSCommand *OSCommand - Worktree *gogit.Worktree - Repo *gogit.Repository - Tr *i18n.Localizer + Log *logrus.Entry + OSCommand *OSCommand + Worktree *gogit.Worktree + Repo *gogit.Repository + Tr *i18n.Localizer + openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) } // NewGitCommand it runs git commands func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { gitCommand := &GitCommand{ - Log: log, - OSCommand: osCommand, - Tr: tr, + Log: log, + OSCommand: osCommand, + Tr: tr, + openGitRepositoryAndWorktree: openGitRepositoryAndWorktree, } return gitCommand, nil } @@ -43,7 +61,7 @@ func (c *GitCommand) SetupGit() error { fs := []func() error{ c.verifyInGitRepo, c.navigateToRepoRootDirectory, - c.setupWorktree, + c.setupRepositoryAndWorktree, } for _, f := range fs { @@ -55,6 +73,44 @@ func (c *GitCommand) SetupGit() error { return nil } +func (c *GitCommand) verifyInGitRepo() error { + if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { + return ErrGitRepositoryInvalid + } + + return nil +} + +func (c *GitCommand) navigateToRepoRootDirectory() error { + for { + f, err := os.Stat(".git") + + if err == nil && f.IsDir() { + return nil + } + + c.Log.Debug("going up a directory to find the root") + + if err = os.Chdir(".."); err != nil { + return err + } + } +} + +func (c *GitCommand) setupRepositoryAndWorktree() (err error) { + c.Repo, c.Worktree, err = c.openGitRepositoryAndWorktree() + + if err == nil { + return + } + + if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { + return errors.New(c.Tr.SLocalize("GitconfigParseErr")) + } + + return +} + // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") @@ -156,54 +212,11 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File { return append(headResults, tailResults...) } -func (c *GitCommand) verifyInGitRepo() error { - if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { - return ErrGitRepositoryInvalid - } - - return nil -} - // GetBranchName branch name func (c *GitCommand) GetBranchName() (string, error) { return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") } -func (c *GitCommand) navigateToRepoRootDirectory() error { - for { - f, err := os.Stat(".git") - - if err == nil && f.IsDir() { - return nil - } - - c.Log.Debug("going up a directory to find the root") - - if err = os.Chdir(".."); err != nil { - return err - } - } -} - -func (c *GitCommand) setupWorktree() error { - r, err := gogit.PlainOpen(".") - if err != nil { - if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { - errorMessage := c.Tr.SLocalize("GitconfigParseErr") - return errors.New(errorMessage) - } - return err - } - c.Repo = r - - w, err := r.Worktree() - if err != nil { - return err - } - c.Worktree = w - return nil -} - // ResetHard does the equivalent of `git reset --hard HEAD` func (c *GitCommand) ResetHard() error { return c.Worktree.Reset(&gogit.ResetOptions{Mode: gogit.HardReset}) -- cgit v1.2.3 From 9f7775df263a83c5e0e845955a7abcd989f62d2a Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Thu, 30 Aug 2018 00:54:54 +0200 Subject: pkg/git : remove unused Map function --- pkg/commands/git.go | 9 --------- 1 file changed, 9 deletions(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 101a45515..48d51e900 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -466,15 +466,6 @@ func (c *GitCommand) GetBranchGraph(branchName string) (string, error) { return c.OSCommand.RunCommandWithOutput("git log --graph --color --abbrev-commit --decorate --date=relative --pretty=medium -100 " + branchName) } -// Map (from https://gobyexample.com/collection-functions) -func Map(vs []string, f func(string) string) []string { - vsm := make([]string, len(vs)) - for i, v := range vs { - vsm[i] = f(v) - } - return vsm -} - func includesString(list []string, a string) bool { for _, b := range list { if b == a { -- cgit v1.2.3 From 43ad9a81c282022203e45ae3088b93763320bccc Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 2 Sep 2018 17:15:27 +0200 Subject: merge setup in function that create a new git command --- pkg/commands/git.go | 127 +++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 65 deletions(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 48d51e900..6ba5f8a84 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -19,98 +19,95 @@ import ( // to check if we have a valid git repository and we get an error instead var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") -func openGitRepositoryAndWorktree() (*gogit.Repository, *gogit.Worktree, error) { - r, err := gogit.PlainOpen(".") - - if err != nil { - return nil, nil, err - } - - w, err := r.Worktree() - - if err != nil { - return nil, nil, err - } - - return r, w, nil -} - -// GitCommand is our main git interface -type GitCommand struct { - Log *logrus.Entry - OSCommand *OSCommand - Worktree *gogit.Worktree - Repo *gogit.Repository - Tr *i18n.Localizer - openGitRepositoryAndWorktree func() (*gogit.Repository, *gogit.Worktree, error) -} - -// NewGitCommand it runs git commands -func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { - gitCommand := &GitCommand{ - Log: log, - OSCommand: osCommand, - Tr: tr, - openGitRepositoryAndWorktree: openGitRepositoryAndWorktree, - } - return gitCommand, nil -} - -// SetupGit sets git repo up -func (c *GitCommand) SetupGit() error { - fs := []func() error{ - c.verifyInGitRepo, - c.navigateToRepoRootDirectory, - c.setupRepositoryAndWorktree, - } - - for _, f := range fs { - if err := f(); err != nil { - return err - } - } - - return nil -} - -func (c *GitCommand) verifyInGitRepo() error { - if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { +func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { + if _, err := runCmdWithOutput("git status"); err != nil { return ErrGitRepositoryInvalid } return nil } -func (c *GitCommand) navigateToRepoRootDirectory() error { +func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error { for { - f, err := os.Stat(".git") + f, err := stat(".git") if err == nil && f.IsDir() { return nil } - c.Log.Debug("going up a directory to find the root") + if !os.IsNotExist(err) { + return err + } - if err = os.Chdir(".."); err != nil { + if err = chdir(".."); err != nil { return err } } } -func (c *GitCommand) setupRepositoryAndWorktree() (err error) { - c.Repo, c.Worktree, err = c.openGitRepositoryAndWorktree() +func setupRepositoryAndWorktree(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (repository *gogit.Repository, worktree *gogit.Worktree, err error) { + repository, err = openGitRepository(".") + + if err != nil { + if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { + return nil, nil, errors.New(sLocalize("GitconfigParseErr")) + } - if err == nil { return } - if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { - return errors.New(c.Tr.SLocalize("GitconfigParseErr")) + worktree, err = repository.Worktree() + + if err != nil { + return } return } +// GitCommand is our main git interface +type GitCommand struct { + Log *logrus.Entry + OSCommand *OSCommand + Worktree *gogit.Worktree + Repo *gogit.Repository + Tr *i18n.Localizer +} + +// NewGitCommand it runs git commands +func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { + var worktree *gogit.Worktree + var repo *gogit.Repository + + fs := []func() error{ + func() error { + return verifyInGitRepo(osCommand.RunCommandWithOutput) + }, + func() error { + return navigateToRepoRootDirectory(os.Stat, os.Chdir) + }, + func() error { + var err error + repo, worktree, err = setupRepositoryAndWorktree(gogit.PlainOpen, tr.SLocalize) + return err + }, + } + + for _, f := range fs { + if err := f(); err != nil { + return nil, err + } + } + + return &GitCommand{ + Log: log, + OSCommand: osCommand, + Tr: tr, + Worktree: worktree, + Repo: repo, + }, nil +} + // GetStashEntries stash entryies func (c *GitCommand) GetStashEntries() []StashEntry { rawString, _ := c.OSCommand.RunCommandWithOutput("git stash list --pretty='%gs'") -- cgit v1.2.3 From 8c675780639ffb1f214c7fcfaa34c09862ccd092 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Sun, 2 Sep 2018 22:46:37 +0200 Subject: replace fmt with errors --- pkg/commands/git.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 6ba5f8a84..df78a2e3a 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -17,7 +17,7 @@ import ( // ErrGitRepositoryInvalid is emitted when we run a git command in a folder // to check if we have a valid git repository and we get an error instead -var ErrGitRepositoryInvalid = fmt.Errorf("can't find a valid git repository in current directory") +var ErrGitRepositoryInvalid = errors.New("can't find a valid git repository in current directory") func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { if _, err := runCmdWithOutput("git status"); err != nil { -- cgit v1.2.3 From df3e7abd688d17982d477f67e05670cacdff4bb4 Mon Sep 17 00:00:00 2001 From: Anthony HAMON Date: Tue, 4 Sep 2018 06:16:19 +0200 Subject: use RunCommand --- pkg/commands/git.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) (limited to 'pkg/commands/git.go') diff --git a/pkg/commands/git.go b/pkg/commands/git.go index df78a2e3a..61c566780 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -15,16 +15,8 @@ import ( gogit "gopkg.in/src-d/go-git.v4" ) -// ErrGitRepositoryInvalid is emitted when we run a git command in a folder -// to check if we have a valid git repository and we get an error instead -var ErrGitRepositoryInvalid = errors.New("can't find a valid git repository in current directory") - -func verifyInGitRepo(runCmdWithOutput func(string) (string, error)) error { - if _, err := runCmdWithOutput("git status"); err != nil { - return ErrGitRepositoryInvalid - } - - return nil +func verifyInGitRepo(runCmd func(string) error) error { + return runCmd("git status") } func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error { @@ -81,7 +73,7 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) fs := []func() error{ func() error { - return verifyInGitRepo(osCommand.RunCommandWithOutput) + return verifyInGitRepo(osCommand.RunCommand) }, func() error { return navigateToRepoRootDirectory(os.Stat, os.Chdir) -- cgit v1.2.3