From ba96c39bd2d0a9b2d00a38db0b5d71bf02781315 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 15:36:04 +1000 Subject: [PATCH 1/5] support bare repositories --- main.go | 32 +++++++++++++-- pkg/app/app.go | 16 +++++++- pkg/commands/git.go | 85 ++++++++++++++++++++++++---------------- pkg/commands/git_test.go | 26 ++++-------- 4 files changed, 102 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index 697129f8089..534285afa6f 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "path/filepath" "runtime" "github.com/go-errors/errors" @@ -22,8 +23,8 @@ var ( func main() { flaggy.DefaultParser.ShowVersionWithVersionFlag = false - repoPath := "." - flaggy.String(&repoPath, "p", "path", "Path of git repo") + repoPath := "" + flaggy.String(&repoPath, "p", "path", "Path of git repo. (Deprecated: use --git-dir for git directory and --work-tree for work tree directory)") filterPath := "" flaggy.String(&filterPath, "f", "filter", "Path to filter on in `git log -- `. When in filter mode, the commits, reflog, and stash are filtered based on the given path, and some operations are restricted") @@ -44,8 +45,31 @@ func main() { configFlag := false flaggy.Bool(&configFlag, "c", "config", "Print the default config") + workTree := "" + flaggy.String(&workTree, "w", "work-tree", "equivalent of the --work-tree git argument") + + gitDir := "" + flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument") + flaggy.Parse() + if repoPath != "" { + if workTree != "" || gitDir != "" { + log.Fatal("--path option is incompatible with the --work-tree and --git-dir options") + } + + workTree = repoPath + gitDir = filepath.Join(repoPath, ".git") + } + + if workTree != "" { + os.Setenv("GIT_WORK_TREE", workTree) + } + + if gitDir != "" { + os.Setenv("GIT_DIR", gitDir) + } + if versionFlag { fmt.Printf("commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n", commit, date, buildSource, version, runtime.GOOS, runtime.GOARCH) os.Exit(0) @@ -61,8 +85,8 @@ func main() { os.Exit(0) } - if repoPath != "." { - if err := os.Chdir(repoPath); err != nil { + if workTree != "" { + if err := os.Chdir(workTree); err != nil { log.Fatal(err.Error()) } } diff --git a/pkg/app/app.go b/pkg/app/app.go index 31144cc1144..6446d8acd5f 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -121,6 +121,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { if err != nil { return app, err } + app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater, filterPath, showRecentRepos) if err != nil { return app, err @@ -169,6 +170,11 @@ func (app *App) setupRepo() (bool, error) { return false, err } + if os.Getenv("GIT_DIR") != "" { + // we've been given the git dir directly. We'll verify this dir when initializing our GitCommand object + return false, nil + } + // if we are not in a git repo, we ask if we want to `git init` if err := app.OSCommand.RunCommand("git status"); err != nil { cwd, err := os.Getwd() @@ -219,6 +225,14 @@ func (app *App) Run() error { return err } +func gitDir() string { + dir := os.Getenv("GIT_DIR") + if dir == "" { + return ".git" + } + return dir +} + // Rebase contains logic for when we've been run in demon mode, meaning we've // given lazygit as a command for git to call e.g. to edit a file func (app *App) Rebase() error { @@ -230,7 +244,7 @@ func (app *App) Rebase() error { return err } - } else if strings.HasSuffix(os.Args[1], ".git/COMMIT_EDITMSG") { + } else if strings.HasSuffix(os.Args[1], filepath.Join(gitDir(), "COMMIT_EDITMSG")) { // TODO: test // if we are rebasing and squashing, we'll see a COMMIT_EDITMSG // but in this case we don't need to edit it, so we'll just return } else { diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 8a7abb55bc4..da4324d4da1 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -35,6 +35,16 @@ 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") != "" { + // we've been given the git directory explicitly so no need to navigate to it + _, err := stat(os.Getenv("GIT_DIR")) + if err != nil { + return WrapError(err) + } + + return nil + } + for { _, err := stat(".git") @@ -52,31 +62,29 @@ func navigateToRepoRootDirectory(stat func(string) (os.FileInfo, error), chdir f } } -func setupRepositoryAndWorktree(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (repository *gogit.Repository, worktree *gogit.Worktree, err error) { - repository, err = openGitRepository(".") +func setupRepository(openGitRepository func(string) (*gogit.Repository, error), sLocalize func(string) string) (*gogit.Repository, error) { + path := os.Getenv("GIT_DIR") + if path == "" { + path = "." + } + + repository, err := openGitRepository(path) if err != nil { if strings.Contains(err.Error(), `unquoted '\' must be followed by new line`) { - return nil, nil, errors.New(sLocalize("GitconfigParseErr")) + return nil, errors.New(sLocalize("GitconfigParseErr")) } - return - } - - worktree, err = repository.Worktree() - - if err != nil { - return + return nil, err } - return + return repository, err } // GitCommand is our main git interface type GitCommand struct { Log *logrus.Entry OSCommand *OSCommand - Worktree *gogit.Worktree Repo *gogit.Repository Tr *i18n.Localizer Config config.AppConfigurer @@ -93,7 +101,7 @@ type GitCommand struct { // NewGitCommand it runs git commands func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*GitCommand, error) { - var worktree *gogit.Worktree + // var worktree *gogit.Worktree var repo *gogit.Repository // see what our default push behaviour is @@ -105,24 +113,16 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, pushToCurrent = strings.TrimSpace(output) == "current" } - 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 - }, + if err := verifyInGitRepo(osCommand.RunCommand); err != nil { + return nil, err } - for _, f := range fs { - if err := f(); err != nil { - return nil, err - } + if err := navigateToRepoRootDirectory(os.Stat, os.Chdir); err != nil { + return nil, err + } + + if repo, err = setupRepository(gogit.PlainOpen, tr.SLocalize); err != nil { + return nil, err } dotGitDir, err := findDotGitDir(os.Stat, ioutil.ReadFile) @@ -134,7 +134,6 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, Log: log, OSCommand: osCommand, Tr: tr, - Worktree: worktree, Repo: repo, Config: config, getGlobalGitConfig: gitconfig.Global, @@ -150,6 +149,10 @@ 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 + } + f, err := stat(".git") if err != nil { return "", err @@ -236,8 +239,22 @@ type GetStatusFileOptions struct { NoRenames bool } +func (c *GitCommand) GetConfigValue(key string) string { + output, _ := c.OSCommand.RunCommandWithOutput("git config --get %s", key) + // looks like this returns an error if there is no matching value which we're okay with + return strings.TrimSpace(output) +} + func (c *GitCommand) GetStatusFiles(opts GetStatusFileOptions) []*File { - statusOutput, err := c.GitStatus(GitStatusOptions{NoRenames: opts.NoRenames}) + // check if config wants us ignoring untracked files + untrackedFilesSetting := c.GetConfigValue("status.showUntrackedFiles") + + if untrackedFilesSetting == "" { + untrackedFilesSetting = "all" + } + untrackedFilesArg := fmt.Sprintf("--untracked-files=%s", untrackedFilesSetting) + + statusOutput, err := c.GitStatus(GitStatusOptions{NoRenames: opts.NoRenames, UntrackedFilesArg: untrackedFilesArg}) if err != nil { c.Log.Error(err) } @@ -582,7 +599,8 @@ func (c *GitCommand) UnStageFile(fileName string, tracked bool) error { // GitStatus returns the plaintext short status of the repo type GitStatusOptions struct { - NoRenames bool + NoRenames bool + UntrackedFilesArg string } func (c *GitCommand) GitStatus(opts GitStatusOptions) (string, error) { @@ -590,7 +608,8 @@ func (c *GitCommand) GitStatus(opts GitStatusOptions) (string, error) { if opts.NoRenames { noRenamesFlag = "--no-renames" } - return c.OSCommand.RunCommandWithOutput("git status --untracked-files=all --porcelain %s", noRenamesFlag) + + return c.OSCommand.RunCommandWithOutput("git status %s --porcelain %s", opts.UntrackedFilesArg, noRenamesFlag) } // IsInMergeState states whether we are still mid-merge diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index d6761148bf2..b2a20fe0887 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -150,13 +150,13 @@ func TestNavigateToRepoRootDirectory(t *testing.T) { } } -// TestSetupRepositoryAndWorktree is a function. -func TestSetupRepositoryAndWorktree(t *testing.T) { +// TestSetupRepository is a function. +func TestSetupRepository(t *testing.T) { type scenario struct { testName string openGitRepository func(string) (*gogit.Repository, error) sLocalize func(string) string - test func(*gogit.Repository, *gogit.Worktree, error) + test func(*gogit.Repository, error) } scenarios := []scenario{ @@ -168,7 +168,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) { func(string) string { return "error translated" }, - func(r *gogit.Repository, w *gogit.Worktree, err error) { + func(r *gogit.Repository, err error) { assert.Error(t, err) assert.EqualError(t, err, "error translated") }, @@ -179,22 +179,11 @@ func TestSetupRepositoryAndWorktree(t *testing.T) { return nil, fmt.Errorf("Error from inside gogit") }, func(string) string { return "" }, - func(r *gogit.Repository, w *gogit.Worktree, err error) { + func(r *gogit.Repository, err error) { assert.Error(t, err) assert.EqualError(t, err, "Error from inside gogit") }, }, - { - "An error occurred cause git repository is a bare repository", - func(string) (*gogit.Repository, error) { - return &gogit.Repository{}, nil - }, - func(string) string { return "" }, - func(r *gogit.Repository, w *gogit.Worktree, err error) { - assert.Error(t, err) - assert.Equal(t, gogit.ErrIsBareRepository, err) - }, - }, { "Setup done properly", func(string) (*gogit.Repository, error) { @@ -204,9 +193,8 @@ func TestSetupRepositoryAndWorktree(t *testing.T) { return r, nil }, func(string) string { return "" }, - func(r *gogit.Repository, w *gogit.Worktree, err error) { + func(r *gogit.Repository, err error) { assert.NoError(t, err) - assert.NotNil(t, w) assert.NotNil(t, r) }, }, @@ -214,7 +202,7 @@ func TestSetupRepositoryAndWorktree(t *testing.T) { for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { - s.test(setupRepositoryAndWorktree(s.openGitRepository, s.sLocalize)) + s.test(setupRepository(s.openGitRepository, s.sLocalize)) }) } } From 917d6d72be5672a94a9c702add11f28e871c5f2d Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 15:52:28 +1000 Subject: [PATCH 2/5] unset GIT_WORK_TREE and GIT_DIR when switching repos --- pkg/gui/recent_repos_panel.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index d9174e0d13a..dba8ac26064 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -23,6 +23,8 @@ func (gui *Gui) handleCreateRecentReposMenu() error { yellow.Sprint(innerPath), }, onPress: func() error { + os.Unsetenv("GIT_WORK_TREE") + os.Unsetenv("GIT_DIR") if err := os.Chdir(innerPath); err != nil { return err } From 65e79de7fd6085b3ea9b2907760135e71ecb6edf Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 16:02:20 +1000 Subject: [PATCH 3/5] do not include bare repos in recent repos list --- main.go | 2 +- pkg/commands/git.go | 6 ++++++ pkg/gui/recent_repos_panel.go | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 534285afa6f..96665782c9e 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,7 @@ func main() { flaggy.DefaultParser.ShowVersionWithVersionFlag = false repoPath := "" - flaggy.String(&repoPath, "p", "path", "Path of git repo. (Deprecated: use --git-dir for git directory and --work-tree for work tree directory)") + flaggy.String(&repoPath, "p", "path", "Path of git repo. (equivalent to --work-tree= --git-dir=/.git/)") filterPath := "" flaggy.String(&filterPath, "f", "filter", "Path to filter on in `git log -- `. When in filter mode, the commits, reflog, and stash are filtered based on the given path, and some operations are restricted") diff --git a/pkg/commands/git.go b/pkg/commands/git.go index da4324d4da1..dcd1729af59 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1427,3 +1427,9 @@ func (c *GitCommand) WorkingTreeState() string { } return "normal" } + +func (c *GitCommand) IsBareRepo() bool { + // note: could use `git rev-parse --is-bare-repository` if we wanna drop go-git + _, err := c.Repo.Worktree() + return err == gogit.ErrIsBareRepository +} diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index dba8ac26064..6d9e535a3dd 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -45,6 +45,14 @@ func (gui *Gui) handleCreateRecentReposMenu() error { // updateRecentRepoList registers the fact that we opened lazygit in this repo, // so that we can open the same repo via the 'recent repos' menu func (gui *Gui) updateRecentRepoList() error { + if gui.GitCommand.IsBareRepo() { + // we could totally do this but it would require storing both the git-dir and the + // worktree in our recent repos list, which is a change that would need to be + // backwards compatible + gui.Log.Info("Not appending bare repo to recent repo list") + return nil + } + recentRepos := gui.Config.GetAppState().RecentRepos currentRepo, err := os.Getwd() if err != nil { From 03b5c8876a048472e3e792aba4ff8332db1accff Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 16:17:26 +1000 Subject: [PATCH 4/5] 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 96665782c9e..78134544325 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 6446d8acd5f..d200e3f157d 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 ec988ec62ec..8ba457d3032 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 dcd1729af59..013e1b39c68 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 00000000000..9c0f4816f21 --- /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 6d9e535a3dd..677836172a9 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 } From 23687120ea02e11a43e661c497e3d6fe19263a6f Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 27 Sep 2020 16:21:28 +1000 Subject: [PATCH 5/5] missed a spot --- pkg/commands/git.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 013e1b39c68..41133424d6c 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -105,7 +105,6 @@ type GitCommand struct { // NewGitCommand it runs git commands func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*GitCommand, error) { - // var worktree *gogit.Worktree var repo *gogit.Repository // see what our default push behaviour is