summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/commands/git.go')
-rw-r--r--pkg/commands/git.go126
1 files changed, 69 insertions, 57 deletions
diff --git a/pkg/commands/git.go b/pkg/commands/git.go
index 704a45c73..61c566780 100644
--- a/pkg/commands/git.go
+++ b/pkg/commands/git.go
@@ -15,6 +15,48 @@ import (
gogit "gopkg.in/src-d/go-git.v4"
)
+func verifyInGitRepo(runCmd func(string) error) error {
+ return runCmd("git status")
+}
+
+func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error {
+ for {
+ f, err := stat(".git")
+
+ if err == nil && f.IsDir() {
+ return nil
+ }
+
+ if !os.IsNotExist(err) {
+ return err
+ }
+
+ if err = chdir(".."); err != nil {
+ return err
+ }
+ }
+}
+
+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"))
+ }
+
+ return
+ }
+
+ worktree, err = repository.Worktree()
+
+ if err != nil {
+ return
+ }
+
+ return
+}
+
// GitCommand is our main git interface
type GitCommand struct {
Log *logrus.Entry
@@ -26,22 +68,36 @@ type GitCommand struct {
// 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,
+ var worktree *gogit.Worktree
+ var repo *gogit.Repository
+
+ fs := []func() error{
+ func() error {
+ return verifyInGitRepo(osCommand.RunCommand)
+ },
+ func() error {
+ return navigateToRepoRootDirectory(os.Stat, os.Chdir)
+ },
+ func() error {
+ var err error
+ repo, worktree, err = setupRepositoryAndWorktree(gogit.PlainOpen, tr.SLocalize)
+ return err
+ },
}
- return gitCommand, nil
-}
-// 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)
+ 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
@@ -145,46 +201,11 @@ 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)
- }
-}
-
// GetBranchName branch name
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) {
- c.Log.Debug("going up a directory to find the root")
- os.Chdir("..")
- _, err = os.Stat(".git")
- }
-}
-
-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})
@@ -434,15 +455,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 {