merge setup in function that create a new git command

This commit is contained in:
Anthony HAMON 2018-09-02 17:15:27 +02:00
parent 9f7775df26
commit 43ad9a81c2
3 changed files with 211 additions and 99 deletions

View file

@ -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'")