Skip to content

Commit

Permalink
Non-sticky range selection diff (#3869)
Browse files Browse the repository at this point in the history
- **PR Description**

When selecting a range of commits, show the combined diff for them.

Along the way, fix a few minor issues with the current implementation of
diffing mode; see the individual commit messages for details.

Fixes #3862.

- **Please check if the PR fulfills these requirements**

* [x] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [x] Tests have been added/updated (see
[here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md)
for the integration test guide)
* [x] Text is internationalised (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
* [ ] If a new UserConfig entry was added, make sure it can be
hot-reloaded (see
[here](https://github.com/jesseduffield/lazygit/blob/master/docs/dev/Codebase_Guide.md#using-userconfig))
* [ ] Docs have been updated if necessary
* [x] You've read through your own file changes for silly mistakes etc
  • Loading branch information
stefanhaller authored Aug 31, 2024
2 parents 05ae080 + 32fef9a commit 2f01af4
Show file tree
Hide file tree
Showing 26 changed files with 270 additions and 94 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/models/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ func (b *Branch) RefName() string {
return b.Name
}

func (b *Branch) ShortRefName() string {
return b.RefName()
}

func (b *Branch) ParentRefName() string {
return b.RefName() + "^"
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/models/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (c *Commit) RefName() string {
return c.Hash
}

func (c *Commit) ShortRefName() string {
return c.Hash[:7]
}

func (c *Commit) ParentRefName() string {
if c.IsFirstCommit() {
return EmptyTreeCommitHash
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/models/remote_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func (r *RemoteBranch) RefName() string {
return r.FullName()
}

func (r *RemoteBranch) ShortRefName() string {
return r.RefName()
}

func (r *RemoteBranch) ParentRefName() string {
return r.RefName() + "^"
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/models/stash_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func (s *StashEntry) RefName() string {
return fmt.Sprintf("stash@{%d}", s.Index)
}

func (s *StashEntry) ShortRefName() string {
return s.RefName()
}

func (s *StashEntry) ParentRefName() string {
return s.RefName() + "^"
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/models/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func (t *Tag) RefName() string {
return t.Name
}

func (t *Tag) ShortRefName() string {
return t.RefName()
}

func (t *Tag) ParentRefName() string {
return t.RefName() + "^"
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/gui/context/commit_files_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package context

import (
"fmt"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
Expand Down Expand Up @@ -75,6 +77,25 @@ func (self *CommitFilesContext) GetDiffTerminals() []string {
return []string{self.GetRef().RefName()}
}

func (self *CommitFilesContext) GetFromAndToForDiff() (string, string) {
if refs := self.GetRefRange(); refs != nil {
return refs.From.ParentRefName(), refs.To.RefName()
}
ref := self.GetRef()
return ref.ParentRefName(), ref.RefName()
}

func (self *CommitFilesContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition {
return nil
}

func (self *CommitFilesContext) ReInit(ref types.Ref, refRange *types.RefRange) {
self.SetRef(ref)
self.SetRefRange(refRange)
if refRange != nil {
self.SetTitleRef(fmt.Sprintf("%s-%s", refRange.From.ShortRefName(), refRange.To.ShortRefName()))
} else {
self.SetTitleRef(ref.Description())
}
self.GetView().Title = self.Title()
}
13 changes: 13 additions & 0 deletions pkg/gui/context/local_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
return commit
}

func (self *LocalCommitsContext) GetSelectedRefRangeForDiffFiles() *types.RefRange {
commits, startIdx, endIdx := self.GetSelectedItems()
if commits == nil || startIdx == endIdx {
return nil
}
from := commits[len(commits)-1]
to := commits[0]
if from.IsTODO() || to.IsTODO() {
return nil
}
return &types.RefRange{From: from, To: to}
}

// Returns the commit hash of the selected commit, or an empty string if no
// commit is selected
func (self *LocalCommitsContext) GetSelectedCommitHash() string {
Expand Down
5 changes: 5 additions & 0 deletions pkg/gui/context/reflog_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (self *ReflogCommitsContext) GetSelectedRef() types.Ref {
return commit
}

func (self *ReflogCommitsContext) GetSelectedRefRangeForDiffFiles() *types.RefRange {
// It doesn't make much sense to show a range diff between two reflog entries.
return nil
}

func (self *ReflogCommitsContext) GetCommits() []*models.Commit {
return self.getModel()
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/gui/context/stash_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (self *StashContext) GetSelectedRef() types.Ref {
return stash
}

func (self *StashContext) GetSelectedRefRangeForDiffFiles() *types.RefRange {
// It doesn't make much sense to show a range diff between two stash entries.
return nil
}

func (self *StashContext) GetDiffTerminals() []string {
itemId := self.GetSelectedItemId()

Expand Down
13 changes: 13 additions & 0 deletions pkg/gui/context/sub_commits_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ func (self *SubCommitsContext) GetSelectedRef() types.Ref {
return commit
}

func (self *SubCommitsContext) GetSelectedRefRangeForDiffFiles() *types.RefRange {
commits, startIdx, endIdx := self.GetSelectedItems()
if commits == nil || startIdx == endIdx {
return nil
}
from := commits[len(commits)-1]
to := commits[0]
if from.Divergence != to.Divergence {
return nil
}
return &types.RefRange{From: from, To: to}
}

func (self *SubCommitsContext) GetCommits() []*models.Commit {
return self.getModel()
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (gui *Gui) resetHelpersAndControllers() {
gui.State.Contexts.Stash,
} {
controllers.AttachControllers(context, controllers.NewSwitchToDiffFilesController(
common, context, gui.State.Contexts.CommitFiles,
common, context,
))
}

Expand Down
28 changes: 17 additions & 11 deletions pkg/gui/controllers/commits_files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ func (self *CommitFilesController) GetOnRenderToMain() func() error {
return nil
}

ref := self.context().GetRef()
to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
from, to := self.context().GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)

cmdObj := self.c.Git().WorkingTree.ShowFileDiffCmdObj(from, to, reverse, node.GetPath(), false)
task := types.NewRunPtyTask(cmdObj.GetCmd())
Expand Down Expand Up @@ -250,9 +249,8 @@ func (self *CommitFilesController) canEditFiles(nodes []*filetree.CommitFileNode
}

func (self *CommitFilesController) openDiffTool(node *filetree.CommitFileNode) error {
ref := self.context().GetRef()
to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
from, to := self.context().GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)
_, err := self.c.RunSubprocess(self.c.Git().Diff.OpenDiffToolCmdObj(
git_commands.DiffToolCmdOptions{
Filepath: node.GetPath(),
Expand Down Expand Up @@ -307,7 +305,8 @@ func (self *CommitFilesController) toggleForPatch(selectedNodes []*filetree.Comm
})
}

if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {
from, to, reverse := self.currentFromToReverseForPatchBuilding()
if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.NewPatchRequired(from, to, reverse) {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm,
Expand All @@ -330,14 +329,20 @@ func (self *CommitFilesController) startPatchBuilder() error {
commitFilesContext := self.context()

canRebase := commitFilesContext.GetCanRebase()
ref := commitFilesContext.GetRef()
to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
from, to, reverse := self.currentFromToReverseForPatchBuilding()

self.c.Git().Patch.PatchBuilder.Start(from, to, reverse, canRebase)
return nil
}

func (self *CommitFilesController) currentFromToReverseForPatchBuilding() (string, string, bool) {
commitFilesContext := self.context()

from, to := commitFilesContext.GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)
return from, to, reverse
}

func (self *CommitFilesController) enter(node *filetree.CommitFileNode) error {
return self.enterCommitFile(node, types.OnFocusOpts{ClickedWindowName: "", ClickedViewLineIdx: -1})
}
Expand All @@ -357,7 +362,8 @@ func (self *CommitFilesController) enterCommitFile(node *filetree.CommitFileNode
return self.c.Context().Push(self.c.Contexts().CustomPatchBuilder, opts)
}

if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.To != self.context().GetRef().RefName() {
from, to, reverse := self.currentFromToReverseForPatchBuilding()
if self.c.Git().Patch.PatchBuilder.Active() && self.c.Git().Patch.PatchBuilder.NewPatchRequired(from, to, reverse) {
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.DiscardPatch,
Prompt: self.c.Tr.DiscardPatchConfirm,
Expand Down
40 changes: 38 additions & 2 deletions pkg/gui/controllers/helpers/diff_helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package helpers

import (
"strings"

"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
"github.com/jesseduffield/lazygit/pkg/gui/style"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
Expand All @@ -19,7 +23,7 @@ func NewDiffHelper(c *HelperCommon) *DiffHelper {
}

func (self *DiffHelper) DiffArgs() []string {
output := []string{self.c.Modes().Diffing.Ref}
output := []string{"--stat", "-p", self.c.Modes().Diffing.Ref}

right := self.currentDiffTerminal()
if right != "" {
Expand All @@ -46,14 +50,46 @@ func (self *DiffHelper) DiffArgs() []string {
return output
}

// Returns an update task that can be passed to RenderToMainViews to render a
// diff for the selected commit(s). We need to pass both the selected commit
// and the refRange for a range selection. If the refRange is nil (meaning that
// either there's no range, or it can't be diffed for some reason), then we want
// to fall back to rendering the diff for the single commit.
func (self *DiffHelper) GetUpdateTaskForRenderingCommitsDiff(commit *models.Commit, refRange *types.RefRange) types.UpdateTask {
if refRange != nil {
from, to := refRange.From, refRange.To
args := []string{from.ParentRefName(), to.RefName(), "--stat", "-p"}
if self.c.GetAppState().IgnoreWhitespaceInDiffView {
args = append(args, "--ignore-all-space")
}
args = append(args, "--")
if path := self.c.Modes().Filtering.GetPath(); path != "" {
args = append(args, path)
}
cmdObj := self.c.Git().Diff.DiffCmdObj(args)
task := types.NewRunPtyTask(cmdObj.GetCmd())
task.Prefix = style.FgYellow.Sprintf("%s %s-%s\n\n", self.c.Tr.ShowingDiffForRange, from.ShortRefName(), to.ShortRefName())
return task
}

cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Hash, self.c.Modes().Filtering.GetPath())
return types.NewRunPtyTask(cmdObj.GetCmd())
}

func (self *DiffHelper) ExitDiffMode() error {
self.c.Modes().Diffing = diffing.New()
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}

func (self *DiffHelper) RenderDiff() error {
cmdObj := self.c.Git().Diff.DiffCmdObj(self.DiffArgs())
args := self.DiffArgs()
cmdObj := self.c.Git().Diff.DiffCmdObj(args)
task := types.NewRunPtyTask(cmdObj.GetCmd())
task.Prefix = style.FgMagenta.Sprintf(
"%s %s\n\n",
self.c.Tr.ShowingGitDiff,
"git diff "+strings.Join(args, " "),
)

return self.c.RenderToMainViews(types.RefreshMainOpts{
Pair: self.c.MainViewPairs().Normal,
Expand Down
5 changes: 2 additions & 3 deletions pkg/gui/controllers/helpers/patch_building_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ func (self *PatchBuildingHelper) RefreshPatchBuildingPanel(opts types.OnFocusOpt
return nil
}

ref := self.c.Contexts().CommitFiles.CommitFileTreeViewModel.GetRef()
to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
from, to := self.c.Contexts().CommitFiles.GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)
diff, err := self.c.Git().WorkingTree.ShowFileDiff(from, to, reverse, path, true)
if err != nil {
return err
Expand Down
9 changes: 4 additions & 5 deletions pkg/gui/controllers/helpers/refresh_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func (self *RefreshHelper) refreshCommitsAndCommitFiles() {
// For now the awkwardness remains.
commit := self.c.Contexts().LocalCommits.GetSelected()
if commit != nil && commit.RefName() != "" {
self.c.Contexts().CommitFiles.SetRef(commit)
self.c.Contexts().CommitFiles.SetTitleRef(commit.RefName())
refRange := self.c.Contexts().LocalCommits.GetSelectedRefRangeForDiffFiles()
self.c.Contexts().CommitFiles.ReInit(commit, refRange)
_ = self.refreshCommitFilesContext()
}
}
Expand Down Expand Up @@ -387,9 +387,8 @@ func (self *RefreshHelper) RefreshAuthors(commits []*models.Commit) {
}

func (self *RefreshHelper) refreshCommitFilesContext() error {
ref := self.c.Contexts().CommitFiles.GetRef()
to := ref.RefName()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(ref.ParentRefName())
from, to := self.c.Contexts().CommitFiles.GetFromAndToForDiff()
from, reverse := self.c.Modes().Diffing.GetFromAndReverseArgsForDiff(from)

files, err := self.c.Git().Loaders.CommitFileLoader.GetFilesInDiff(from, to, reverse)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/controllers/local_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ func (self *LocalCommitsController) GetOnRenderToMain() func() error {
task = types.NewRenderStringTask(
self.c.Tr.ExecCommandHere + "\n\n" + commit.Name)
} else {
cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Hash, self.c.Modes().Filtering.GetPath())
task = types.NewRunPtyTask(cmdObj.GetCmd())
refRange := self.context().GetSelectedRefRangeForDiffFiles()
task = self.c.Helpers().Diff.GetUpdateTaskForRenderingCommitsDiff(commit, refRange)
}

return self.c.RenderToMainViews(types.RefreshMainOpts{
Expand Down
5 changes: 2 additions & 3 deletions pkg/gui/controllers/sub_commits_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func (self *SubCommitsController) GetOnRenderToMain() func() error {
if commit == nil {
task = types.NewRenderStringTask("No commits")
} else {
cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Hash, self.c.Modes().Filtering.GetPath())

task = types.NewRunPtyTask(cmdObj.GetCmd())
refRange := self.context().GetSelectedRefRangeForDiffFiles()
task = self.c.Helpers().Diff.GetUpdateTaskForRenderingCommitsDiff(commit, refRange)
}

return self.c.RenderToMainViews(types.RefreshMainOpts{
Expand Down
Loading

0 comments on commit 2f01af4

Please sign in to comment.