1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-08-07 22:02:56 +03:00

Show diff for range selection in commits and sub-commits panel

In other views that show lists of commits (reflog and stash) it doesn't make
sense to show a range diff of selected entries because they don't form a linear
sequence, so we keep the previous behavior of showing the diff for the free end
of the selection range in those view.

The same applies to the commits view if the selection range includes rebasing
todos; these can have an arbitrary order, and a range diff doesn't make sense
for those.
This commit is contained in:
Stefan Haller
2024-08-21 08:31:35 +02:00
parent ac335907ae
commit 442592a149
7 changed files with 64 additions and 5 deletions

View File

@@ -128,6 +128,19 @@ func (self *LocalCommitsContext) GetSelectedRef() types.Ref {
return commit 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 // Returns the commit hash of the selected commit, or an empty string if no
// commit is selected // commit is selected
func (self *LocalCommitsContext) GetSelectedCommitHash() string { func (self *LocalCommitsContext) GetSelectedCommitHash() string {

View File

@@ -186,6 +186,19 @@ func (self *SubCommitsContext) GetSelectedRef() types.Ref {
return commit 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 { func (self *SubCommitsContext) GetCommits() []*models.Commit {
return self.getModel() return self.getModel()
} }

View File

@@ -4,6 +4,7 @@ import (
"strings" "strings"
"github.com/jesseduffield/lazygit/pkg/commands/git_commands" "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/context"
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing" "github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
"github.com/jesseduffield/lazygit/pkg/gui/style" "github.com/jesseduffield/lazygit/pkg/gui/style"
@@ -49,6 +50,32 @@ func (self *DiffHelper) DiffArgs() []string {
return output 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 { func (self *DiffHelper) ExitDiffMode() error {
self.c.Modes().Diffing = diffing.New() self.c.Modes().Diffing = diffing.New()
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})

View File

@@ -290,8 +290,8 @@ func (self *LocalCommitsController) GetOnRenderToMain() func() error {
task = types.NewRenderStringTask( task = types.NewRenderStringTask(
self.c.Tr.ExecCommandHere + "\n\n" + commit.Name) self.c.Tr.ExecCommandHere + "\n\n" + commit.Name)
} else { } else {
cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Hash, self.c.Modes().Filtering.GetPath()) refRange := self.context().GetSelectedRefRangeForDiffFiles()
task = types.NewRunPtyTask(cmdObj.GetCmd()) task = self.c.Helpers().Diff.GetUpdateTaskForRenderingCommitsDiff(commit, refRange)
} }
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{

View File

@@ -46,9 +46,8 @@ func (self *SubCommitsController) GetOnRenderToMain() func() error {
if commit == nil { if commit == nil {
task = types.NewRenderStringTask("No commits") task = types.NewRenderStringTask("No commits")
} else { } else {
cmdObj := self.c.Git().Commit.ShowCmdObj(commit.Hash, self.c.Modes().Filtering.GetPath()) refRange := self.context().GetSelectedRefRangeForDiffFiles()
task = self.c.Helpers().Diff.GetUpdateTaskForRenderingCommitsDiff(commit, refRange)
task = types.NewRunPtyTask(cmdObj.GetCmd())
} }
return self.c.RenderToMainViews(types.RefreshMainOpts{ return self.c.RenderToMainViews(types.RefreshMainOpts{

View File

@@ -7,3 +7,8 @@ type Ref interface {
ParentRefName() string ParentRefName() string
Description() string Description() string
} }
type RefRange struct {
From Ref
To Ref
}

View File

@@ -581,6 +581,7 @@ type TranslationSet struct {
OpenCommandLogMenu string OpenCommandLogMenu string
OpenCommandLogMenuTooltip string OpenCommandLogMenuTooltip string
ShowingGitDiff string ShowingGitDiff string
ShowingDiffForRange string
CommitDiff string CommitDiff string
CopyCommitHashToClipboard string CopyCommitHashToClipboard string
CommitHash string CommitHash string
@@ -1569,6 +1570,7 @@ func EnglishTranslationSet() *TranslationSet {
OpenCommandLogMenu: "View command log options", OpenCommandLogMenu: "View command log options",
OpenCommandLogMenuTooltip: "View options for the command log e.g. show/hide the command log and focus the command log.", OpenCommandLogMenuTooltip: "View options for the command log e.g. show/hide the command log and focus the command log.",
ShowingGitDiff: "Showing output for:", ShowingGitDiff: "Showing output for:",
ShowingDiffForRange: "Showing diff for range",
CommitDiff: "Commit diff", CommitDiff: "Commit diff",
CopyCommitHashToClipboard: "Copy commit hash to clipboard", CopyCommitHashToClipboard: "Copy commit hash to clipboard",
CommitHash: "Commit hash", CommitHash: "Commit hash",