diff --git a/pkg/commands/git.go b/pkg/commands/git.go index ddb19f98a..57121955d 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -1052,14 +1052,29 @@ func (c *GitCommand) GetFilesInRef(parent string, isStash bool, patchManager *pa command = "git stash show" } - files, err := c.OSCommand.RunCommandWithOutput("%s --no-commit-id --name-only -r --no-renames %s", command, parent) + filenames, err := c.OSCommand.RunCommandWithOutput("%s --no-commit-id --name-only -r --no-renames %s", command, parent) if err != nil { return nil, err } + return c.GetCommitFilesFromFilenames(filenames, parent, 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) + if err != nil { + return nil, err + } + + return c.GetCommitFilesFromFilenames(filenames, parent, patchManager), nil +} + +// filenames string is something like "file1\nfile2\nfile3" +func (c *GitCommand) GetCommitFilesFromFilenames(filenames string, parent string, patchManager *patch.PatchManager) []*CommitFile { commitFiles := make([]*CommitFile, 0) - for _, file := range strings.Split(strings.TrimRight(files, "\n"), "\n") { + for _, file := range strings.Split(strings.TrimRight(filenames, "\n"), "\n") { status := patch.UNSELECTED if patchManager != nil && patchManager.Parent == parent { status = patchManager.GetFileStatus(file) @@ -1073,7 +1088,7 @@ func (c *GitCommand) GetFilesInRef(parent string, isStash bool, patchManager *pa }) } - return commitFiles, nil + return commitFiles } // ShowCommitFile get the diff of specified commit file diff --git a/pkg/gui/commit_files_panel.go b/pkg/gui/commit_files_panel.go index 6e3cd4b1f..c5f898b4e 100644 --- a/pkg/gui/commit_files_panel.go +++ b/pkg/gui/commit_files_panel.go @@ -94,7 +94,24 @@ func (gui *Gui) refreshCommitFilesView() error { } isStash := gui.State.Panels.CommitFiles.refType == REF_TYPE_STASH - files, err := gui.GitCommand.GetFilesInRef(gui.State.Panels.CommitFiles.refName, isStash, gui.GitCommand.PatchManager) + 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) + } + if err != nil { return gui.surfaceError(err) } diff --git a/pkg/gui/diffing.go b/pkg/gui/diffing.go index 968887b30..b334fe768 100644 --- a/pkg/gui/diffing.go +++ b/pkg/gui/diffing.go @@ -40,9 +40,10 @@ func (gui *Gui) currentDiffTerminals() []string { switch gui.currentContextKey() { case "": return nil - case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY: - // not supporting these for now because I'm not sure how it would actually work - return nil + case FILES_CONTEXT_KEY: + return []string{""} + case COMMIT_FILES_CONTEXT_KEY: + return []string{gui.State.Panels.CommitFiles.refName} case LOCAL_BRANCHES_CONTEXT_KEY: // for our local branches we want to include both the branch and its upstream branch := gui.getSelectedBranch() @@ -75,6 +76,19 @@ func (gui *Gui) currentDiffTerminal() string { return names[0] } +func (gui *Gui) currentlySelectedFilename() string { + switch gui.currentContextKey() { + case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY: + item := gui.getSideContextSelectedItem() + if item == nil { + return "" + } + return item.ID() + default: + return "" + } +} + func (gui *Gui) diffStr() string { output := gui.State.Modes.Diffing.Ref @@ -82,9 +96,16 @@ func (gui *Gui) diffStr() string { if right != "" { output += " " + right } + if gui.State.Modes.Diffing.Reverse { output += " -R" } + + file := gui.currentlySelectedFilename() + if file != "" { + output += " -- " + file + } + return output }