1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-19 12:12:42 +02:00

pkg/git : remove panic in SetupGit method

This commit is contained in:
Anthony HAMON 2018-08-29 21:47:48 +02:00
parent 19a8029795
commit 624d63d2fa
2 changed files with 41 additions and 16 deletions

View File

@ -48,6 +48,12 @@ func main() {
app.Log.Error(err.Error()) app.Log.Error(err.Error())
panic(err) panic(err)
} }
app.GitCommand.SetupGit()
if err := app.GitCommand.SetupGit(); err != nil {
app.Log.Error(err.Error())
fmt.Println(err)
os.Exit(1)
}
app.Gui.RunWithSubprocesses() app.Gui.RunWithSubprocesses()
} }

View File

@ -15,6 +15,10 @@ import (
gogit "gopkg.in/src-d/go-git.v4" 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 // GitCommand is our main git interface
type GitCommand struct { type GitCommand struct {
Log *logrus.Entry Log *logrus.Entry
@ -35,13 +39,20 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer)
} }
// SetupGit sets git repo up // SetupGit sets git repo up
func (c *GitCommand) SetupGit() { func (c *GitCommand) SetupGit() error {
c.verifyInGitRepo() fs := []func() error{
c.navigateToRepoRootDirectory() c.verifyInGitRepo,
if err := c.setupWorktree(); err != nil { c.navigateToRepoRootDirectory,
c.Log.Error(err) c.setupWorktree,
panic(err)
} }
for _, f := range fs {
if err := f(); err != nil {
return err
}
}
return nil
} }
// GetStashEntries stash entryies // GetStashEntries stash entryies
@ -145,11 +156,12 @@ func (c *GitCommand) MergeStatusFiles(oldFiles, newFiles []File) []File {
return append(headResults, tailResults...) return append(headResults, tailResults...)
} }
func (c *GitCommand) verifyInGitRepo() { func (c *GitCommand) verifyInGitRepo() error {
if output, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil { if _, err := c.OSCommand.RunCommandWithOutput("git status"); err != nil {
fmt.Println(output) return ErrGitRepositoryInvalid
os.Exit(1)
} }
return nil
} }
// GetBranchName branch name // GetBranchName branch name
@ -157,12 +169,19 @@ func (c *GitCommand) GetBranchName() (string, error) {
return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD") return c.OSCommand.RunCommandWithOutput("git symbolic-ref --short HEAD")
} }
func (c *GitCommand) navigateToRepoRootDirectory() { func (c *GitCommand) navigateToRepoRootDirectory() error {
_, err := os.Stat(".git") for {
for os.IsNotExist(err) { f, err := os.Stat(".git")
if err == nil && f.IsDir() {
return nil
}
c.Log.Debug("going up a directory to find the root") 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
}
} }
} }