From 75598ea2a1a561b6cdba88d9763e134412a2eb04 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 16:17:26 +1000 Subject: [PATCH] move git dir env stuff into a centralised package --- main.go | 5 +++-- pkg/app/app.go | 5 +++-- pkg/commands/commit_list_builder.go | 6 +++--- pkg/commands/git.go | 24 ++++++++++++++---------- pkg/env/env.go | 24 ++++++++++++++++++++++++ pkg/gui/recent_repos_panel.go | 4 ++-- 6 files changed, 49 insertions(+), 19 deletions(-) create mode 100644 pkg/env/env.go diff --git a/main.go b/main.go index 96665782c..781345443 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/integrii/flaggy" "github.com/jesseduffield/lazygit/pkg/app" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/env" ) var ( @@ -63,11 +64,11 @@ func main() { } if workTree != "" { - os.Setenv("GIT_WORK_TREE", workTree) + env.SetGitWorkTreeEnv(workTree) } if gitDir != "" { - os.Setenv("GIT_DIR", gitDir) + env.SetGitDirEnv(gitDir) } if versionFlag { diff --git a/pkg/app/app.go b/pkg/app/app.go index 6446d8acd..d200e3f15 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -17,6 +17,7 @@ import ( "github.com/aybabtme/humanlog" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/gui" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/updates" @@ -170,7 +171,7 @@ func (app *App) setupRepo() (bool, error) { return false, err } - if os.Getenv("GIT_DIR") != "" { + if env.GetGitDirEnv() != "" { // we've been given the git dir directly. We'll verify this dir when initializing our GitCommand object return false, nil } @@ -226,7 +227,7 @@ func (app *App) Run() error { } func gitDir() string { - dir := os.Getenv("GIT_DIR") + dir := env.GetGitDirEnv() if dir == "" { return ".git" } diff --git a/pkg/commands/commit_list_builder.go b/pkg/commands/commit_list_builder.go index ec988ec62..8ba457d30 100644 --- a/pkg/commands/commit_list_builder.go +++ b/pkg/commands/commit_list_builder.go @@ -194,7 +194,7 @@ func (c *CommitListBuilder) getRebasingCommits(rebaseMode string) ([]*Commit, er func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) { rewrittenCount := 0 - bytesContent, err := ioutil.ReadFile(fmt.Sprintf("%s/rebase-apply/rewritten", c.GitCommand.DotGitDir)) + bytesContent, err := ioutil.ReadFile(filepath.Join(c.GitCommand.DotGitDir, "rebase-apply/rewritten")) if err == nil { content := string(bytesContent) rewrittenCount = len(strings.Split(content, "\n")) @@ -202,7 +202,7 @@ func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) { // we know we're rebasing, so lets get all the files whose names have numbers commits := []*Commit{} - err = filepath.Walk(fmt.Sprintf("%s/rebase-apply", c.GitCommand.DotGitDir), func(path string, f os.FileInfo, err error) error { + err = filepath.Walk(filepath.Join(c.GitCommand.DotGitDir, "rebase-apply"), func(path string, f os.FileInfo, err error) error { if rewrittenCount > 0 { rewrittenCount-- return nil @@ -246,7 +246,7 @@ func (c *CommitListBuilder) getNormalRebasingCommits() ([]*Commit, error) { // and extracts out the sha and names of commits that we still have to go // in the rebase: func (c *CommitListBuilder) getInteractiveRebasingCommits() ([]*Commit, error) { - bytesContent, err := ioutil.ReadFile(fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.GitCommand.DotGitDir)) + bytesContent, err := ioutil.ReadFile(filepath.Join(c.GitCommand.DotGitDir, "rebase-merge/git-rebase-todo")) if err != nil { c.Log.Error(fmt.Sprintf("error occurred reading git-rebase-todo: %s", err.Error())) // we assume an error means the file doesn't exist so we just return diff --git a/pkg/commands/git.go b/pkg/commands/git.go index dcd1729af..013e1b39c 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -18,6 +18,7 @@ import ( gogit "github.com/go-git/go-git/v5" "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/config" + "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" @@ -35,9 +36,10 @@ func verifyInGitRepo(runCmd func(string, ...interface{}) error) error { } func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir func(string) error) error { - if os.Getenv("GIT_DIR") != "" { + gitDir := env.GetGitDirEnv() + if gitDir != "" { // we've been given the git directory explicitly so no need to navigate to it - _, err := stat(os.Getenv("GIT_DIR")) + _, err := stat(gitDir) if err != nil { return WrapError(err) } @@ -45,6 +47,8 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f return nil } + // we haven't been given the git dir explicitly so we assume it's in the current working directory as `.git/` (or an ancestor directory) + for { _, err := stat(".git") @@ -63,7 +67,7 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f } func setupRepository(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (*gogit.Repository, error) { - path := os.Getenv("GIT_DIR") + path := env.GetGitDirEnv() if path == "" { path = "." } @@ -149,8 +153,8 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, } func findDotGitDir(stat func(string) (os.FileInfo, error), readFile func(filename string) ([]byte, error)) (string, error) { - if os.Getenv("GIT_DIR") != "" { - return os.Getenv("GIT_DIR"), nil + if env.GetGitDirEnv() != "" { + return env.GetGitDirEnv(), nil } f, err := stat(".git") @@ -614,20 +618,20 @@ func (c *GitCommand) GitStatus(opts GitStatusOptions) (string, error) { // IsInMergeState states whether we are still mid-merge func (c *GitCommand) IsInMergeState() (bool, error) { - return c.OSCommand.FileExists(fmt.Sprintf("%s/MERGE_HEAD", c.DotGitDir)) + return c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "MERGE_HEAD")) } // RebaseMode returns "" for non-rebase mode, "normal" for normal rebase // and "interactive" for interactive rebase func (c *GitCommand) RebaseMode() (string, error) { - exists, err := c.OSCommand.FileExists(fmt.Sprintf("%s/rebase-apply", c.DotGitDir)) + exists, err := c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-apply")) if err != nil { return "", err } if exists { return "normal", nil } - exists, err = c.OSCommand.FileExists(fmt.Sprintf("%s/rebase-merge", c.DotGitDir)) + exists, err = c.OSCommand.FileExists(filepath.Join(c.DotGitDir, "rebase-merge")) if exists { return "interactive", err } else { @@ -1006,7 +1010,7 @@ func (c *GitCommand) AmendTo(sha string) error { // EditRebaseTodo sets the action at a given index in the git-rebase-todo file func (c *GitCommand) EditRebaseTodo(index int, action string) error { - fileName := fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.DotGitDir) + fileName := filepath.Join(c.DotGitDir, "rebase-merge/git-rebase-todo") bytes, err := ioutil.ReadFile(fileName) if err != nil { return err @@ -1038,7 +1042,7 @@ func (c *GitCommand) getTodoCommitCount(content []string) int { // MoveTodoDown moves a rebase todo item down by one position func (c *GitCommand) MoveTodoDown(index int) error { - fileName := fmt.Sprintf("%s/rebase-merge/git-rebase-todo", c.DotGitDir) + fileName := filepath.Join(c.DotGitDir, "rebase-merge/git-rebase-todo") bytes, err := ioutil.ReadFile(fileName) if err != nil { return err diff --git a/pkg/env/env.go b/pkg/env/env.go new file mode 100644 index 000000000..9c0f4816f --- /dev/null +++ b/pkg/env/env.go @@ -0,0 +1,24 @@ +package env + +import "os" + +func GetGitDirEnv() string { + return os.Getenv("GIT_DIR") +} + +func GetGitWorkTreeEnv() string { + return os.Getenv("GIT_WORK_TREE") +} + +func SetGitDirEnv(value string) { + os.Setenv("GIT_DIR", value) +} + +func SetGitWorkTreeEnv(value string) { + os.Setenv("GIT_WORK_TREE", value) +} + +func UnsetGitDirEnvs() { + _ = os.Unsetenv("GIT_DIR") + _ = os.Unsetenv("GIT_WORK_TREE") +} diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index 6d9e535a3..677836172 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -6,6 +6,7 @@ import ( "github.com/fatih/color" "github.com/jesseduffield/lazygit/pkg/commands" + "github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -23,8 +24,7 @@ func (gui *Gui) handleCreateRecentReposMenu() error { yellow.Sprint(innerPath), }, onPress: func() error { - os.Unsetenv("GIT_WORK_TREE") - os.Unsetenv("GIT_DIR") + env.UnsetGitDirEnvs() if err := os.Chdir(innerPath); err != nil { return err }