diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 3924fba35..0d03eac2c 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1045,29 +1045,19 @@ func (c *GitCommand) CherryPickCommits(commits []*Commit) error { return c.OSCommand.RunPreparedCommand(cmd) } -// GetFilesInRef get the specified commit files -func (c *GitCommand) GetFilesInRef(refName string, isStash bool, patchManager *patch.PatchManager) ([]*CommitFile, error) { - command := "git diff-tree" - if isStash { - command = "git stash show" - } - - filenames, err := c.OSCommand.RunCommandWithOutput("%s --no-commit-id --name-only -r --no-renames %s", command, refName) - if err != nil { - return nil, err - } - - return c.GetCommitFilesFromFilenames(filenames, refName, patchManager), nil -} - // GetFilesInDiff get the specified commit files -func (c *GitCommand) GetFilesInDiff(from string, to string, parent string, patchManager *patch.PatchManager) ([]*CommitFile, error) { - filenames, err := c.OSCommand.RunCommandWithOutput("git diff --name-only %s %s", from, to) +func (c *GitCommand) GetFilesInDiff(from string, to string, reverse bool, patchManager *patch.PatchManager) ([]*CommitFile, error) { + reverseFlag := "" + if reverse { + reverseFlag = " -R " + } + + filenames, err := c.OSCommand.RunCommandWithOutput("git diff --name-only %s %s %s", reverseFlag, from, to) if err != nil { return nil, err } - return c.GetCommitFilesFromFilenames(filenames, parent, patchManager), nil + return c.GetCommitFilesFromFilenames(filenames, to, patchManager), nil } // filenames string is something like "file1\nfile2\nfile3" diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index d0899caa3..dc04e2018 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -1852,45 +1852,6 @@ func TestGitCommandShowCommitFile(t *testing.T) { } } -// TestGitCommandGetFilesInRef is a function. -func TestGitCommandGetFilesInRef(t *testing.T) { - type scenario struct { - testName string - commitSha string - command func(string, ...string) *exec.Cmd - test func([]*CommitFile, error) - } - - scenarios := []scenario{ - { - "valid case", - "123456", - test.CreateMockCommand(t, []*test.CommandSwapper{ - { - Expect: "git diff-tree --no-commit-id --name-only -r --no-renames 123456", - Replace: "echo 'hello\nworld'", - }, - }), - func(commitFiles []*CommitFile, err error) { - assert.NoError(t, err) - assert.Equal(t, []*CommitFile{ - {Parent: "123456", Name: "hello", DisplayString: "hello"}, - {Parent: "123456", Name: "world", DisplayString: "world"}, - }, commitFiles) - }, - }, - } - - gitCmd := NewDummyGitCommand() - - for _, s := range scenarios { - t.Run(s.testName, func(t *testing.T) { - gitCmd.OSCommand.command = s.command - s.test(gitCmd.GetFilesInRef(s.commitSha, false, nil)) - }) - } -} - // TestGitCommandDiscardUnstagedFileChanges is a function. func TestGitCommandDiscardUnstagedFileChanges(t *testing.T) { type scenario struct { diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 442df823a..3f4285744 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -5,20 +5,6 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands" ) -const ( - // these are the possible ref types for refs that you can view files of. - // for local commits, we're allowed to build a patch and do things involving rebasing - // with that patch - REF_TYPE_LOCAL_COMMIT = iota - - // for other kinds of commits like reflog commits, we can't do anything rebasey - REF_TYPE_OTHER_COMMIT - - // for stash entries we can't do anything rebasey, and the command for - // obtaining the files is slightly different - REF_TYPE_STASH -) - func (gui *Gui) getSelectedCommitFile() *commands.CommitFile { selectedLine := gui.State.Panels.CommitFiles.SelectedLineIdx if selectedLine == -1 { @@ -96,25 +82,10 @@ func (gui *Gui) refreshCommitFilesView() error { return err } - isStash := gui.State.Panels.CommitFiles.refType == REF_TYPE_STASH - refName := gui.State.Panels.CommitFiles.refName - diffing := gui.State.Modes.Diffing - - var files []*commands.CommitFile - var err error - if diffing.Active() { - from := diffing.Ref - to := refName - - if diffing.Reverse { - from, to = to, from - } - - files, err = gui.GitCommand.GetFilesInDiff(from, to, refName, gui.GitCommand.PatchManager) - } else { - files, err = gui.GitCommand.GetFilesInRef(refName, isStash, gui.GitCommand.PatchManager) - } + to := gui.State.Panels.CommitFiles.refName + from, reverse := gui.getFromAndReverseArgsForDiff(to) + files, err := gui.GitCommand.GetFilesInDiff(from, to, reverse, gui.GitCommand.PatchManager) if err != nil { return gui.surfaceError(err) } @@ -187,7 +158,7 @@ func (gui *Gui) handleToggleFileForPatch(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) startPatchManager() error { - canRebase := gui.State.Panels.CommitFiles.refType == REF_TYPE_LOCAL_COMMIT + canRebase := gui.State.Panels.CommitFiles.canRebase to := gui.State.Panels.CommitFiles.refName from, reverse := gui.getFromAndReverseArgsForDiff(to) @@ -243,14 +214,14 @@ func (gui *Gui) enterCommitFile(selectedLineIdx int) error { return enterTheFile(selectedLineIdx) } -func (gui *Gui) switchToCommitFilesContext(refName string, refType int, context Context, windowName string) error { +func (gui *Gui) switchToCommitFilesContext(refName string, canRebase bool, context Context, windowName string) error { // sometimes the commitFiles view is already shown in another window, so we need to ensure that window // no longer considers the commitFiles view as its main view. gui.resetWindowForView("commitFiles") gui.State.Panels.CommitFiles.SelectedLineIdx = 0 gui.State.Panels.CommitFiles.refName = refName - gui.State.Panels.CommitFiles.refType = refType + gui.State.Panels.CommitFiles.canRebase = canRebase gui.Contexts.CommitFiles.Context.SetParentContext(context) gui.Contexts.CommitFiles.Context.SetWindowName(windowName) diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index 9737d5681..7952a7304 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -420,7 +420,7 @@ func (gui *Gui) handleViewCommitFiles() error { return nil } - return gui.switchToCommitFilesContext(commit.Sha, REF_TYPE_LOCAL_COMMIT, gui.Contexts.BranchCommits.Context, "commits") + return gui.switchToCommitFilesContext(commit.Sha, true, gui.Contexts.BranchCommits.Context, "commits") } func (gui *Gui) hasCommit(commits []*commands.Commit, target string) (int, bool) { diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index d7ae574e9..9a24b7dbb 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -194,8 +194,8 @@ type commitFilesPanelState struct { // this is the SHA of the commit or the stash index of the stash. // Not sure if ref is actually the right word here - refName string - refType int // eg REF_TYPE_LOCAL_COMMIT + refName string + canRebase bool } type panelStates struct { diff --git a/pkg/gui/reflog_panel.go b/pkg/gui/reflog_panel.go index 259c8f6ac..30d534e06 100644 --- a/pkg/gui/reflog_panel.go +++ b/pkg/gui/reflog_panel.go @@ -119,5 +119,5 @@ func (gui *Gui) handleViewReflogCommitFiles() error { return nil } - return gui.switchToCommitFilesContext(commit.Sha, REF_TYPE_OTHER_COMMIT, gui.Contexts.ReflogCommits.Context, "commits") + return gui.switchToCommitFilesContext(commit.Sha, false, gui.Contexts.ReflogCommits.Context, "commits") } diff --git a/pkg/gui/stash_panel.go b/pkg/gui/stash_panel.go index dce5ebc72..cf991385a 100644 --- a/pkg/gui/stash_panel.go +++ b/pkg/gui/stash_panel.go @@ -135,5 +135,5 @@ func (gui *Gui) handleViewStashFiles() error { return nil } - return gui.switchToCommitFilesContext(stashEntry.RefName(), REF_TYPE_STASH, gui.Contexts.Stash.Context, "stash") + return gui.switchToCommitFilesContext(stashEntry.RefName(), false, gui.Contexts.Stash.Context, "stash") } diff --git a/pkg/gui/sub_commits_panel.go b/pkg/gui/sub_commits_panel.go index 3ac5282d7..dc98c4aed 100644 --- a/pkg/gui/sub_commits_panel.go +++ b/pkg/gui/sub_commits_panel.go @@ -74,7 +74,7 @@ func (gui *Gui) handleViewSubCommitFiles() error { return nil } - return gui.switchToCommitFilesContext(commit.Sha, REF_TYPE_OTHER_COMMIT, gui.Contexts.SubCommits.Context, "branches") + return gui.switchToCommitFilesContext(commit.Sha, false, gui.Contexts.SubCommits.Context, "branches") } func (gui *Gui) switchToSubCommitsContext(refName string) error {