From 49ca7f6a84b1f713eaba591b773347f1a4709796 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 7 Jan 2025 17:38:46 +0100 Subject: [PATCH 1/4] Bump gocui --- go.mod | 2 +- go.sum | 4 ++-- pkg/gui/gui.go | 2 +- vendor/github.com/jesseduffield/gocui/gui.go | 6 +++--- vendor/modules.txt | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index b676e493821..21cf64fc746 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/integrii/flaggy v1.4.0 github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d - github.com/jesseduffield/gocui v0.3.1-0.20250106080306-164661a92088 + github.com/jesseduffield/gocui v0.3.1-0.20250107151125-716b1eb82fb4 github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e diff --git a/go.sum b/go.sum index dcb1b0bdae0..3f2f028a45c 100644 --- a/go.sum +++ b/go.sum @@ -188,8 +188,8 @@ github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 h1:EQP2Tv8T github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk= github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d h1:bO+OmbreIv91rCe8NmscRwhFSqkDJtzWCPV4Y+SQuXE= github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= -github.com/jesseduffield/gocui v0.3.1-0.20250106080306-164661a92088 h1:yAJ+yFWcv1WRsbgoc4BrGxZVqdLiGVMkz+hEQ1ktgb0= -github.com/jesseduffield/gocui v0.3.1-0.20250106080306-164661a92088/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8= +github.com/jesseduffield/gocui v0.3.1-0.20250107151125-716b1eb82fb4 h1:hSAimLVb4b5ktU3uJRtBsZW0P2dtXECReTcsHYfOy58= +github.com/jesseduffield/gocui v0.3.1-0.20250107151125-716b1eb82fb4/go.mod h1:XtEbqCbn45keRXEu+OMZkjN5gw6AEob59afsgHjokZ8= github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a h1:UDeJ3EBk04bXDLOPvuqM3on8HvyJfISw0+UMqW+0a4g= github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a/go.mod h1:FSWDLKT0NQpntbDd1H3lbz51fhCVlMzy/J0S6nM727Q= github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY= diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 7600c955bbe..427cd465ded 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -358,7 +358,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context return nil }) - gui.g.SetOpenHyperlinkFunc(func(url string) error { + gui.g.SetOpenHyperlinkFunc(func(url string, viewname string) error { if strings.HasPrefix(url, "lazygit-edit:") { re := regexp.MustCompile(`^lazygit-edit://(.+?)(?::(\d+))?$`) matches := re.FindStringSubmatch(url) diff --git a/vendor/github.com/jesseduffield/gocui/gui.go b/vendor/github.com/jesseduffield/gocui/gui.go index 7caa174a286..87cc2832115 100644 --- a/vendor/github.com/jesseduffield/gocui/gui.go +++ b/vendor/github.com/jesseduffield/gocui/gui.go @@ -130,7 +130,7 @@ type Gui struct { managers []Manager keybindings []*keybinding focusHandler func(bool) error - openHyperlink func(string) error + openHyperlink func(string, string) error maxX, maxY int outputMode OutputMode stop chan struct{} @@ -627,7 +627,7 @@ func (g *Gui) SetFocusHandler(handler func(bool) error) { g.focusHandler = handler } -func (g *Gui) SetOpenHyperlinkFunc(openHyperlinkFunc func(string) error) { +func (g *Gui) SetOpenHyperlinkFunc(openHyperlinkFunc func(string, string) error) { g.openHyperlink = openHyperlinkFunc } @@ -1371,7 +1371,7 @@ func (g *Gui) onKey(ev *GocuiEvent) error { if ev.Key == MouseLeft && !v.Editable && g.openHyperlink != nil { if newY >= 0 && newY <= len(v.viewLines)-1 && newX >= 0 && newX <= len(v.viewLines[newY].line)-1 { if link := v.viewLines[newY].line[newX].hyperlink; link != "" { - return g.openHyperlink(link) + return g.openHyperlink(link, v.name) } } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 3deabb0cefe..19ea922ebbd 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -172,7 +172,7 @@ github.com/jesseduffield/go-git/v5/utils/merkletrie/filesystem github.com/jesseduffield/go-git/v5/utils/merkletrie/index github.com/jesseduffield/go-git/v5/utils/merkletrie/internal/frame github.com/jesseduffield/go-git/v5/utils/merkletrie/noder -# github.com/jesseduffield/gocui v0.3.1-0.20250106080306-164661a92088 +# github.com/jesseduffield/gocui v0.3.1-0.20250107151125-716b1eb82fb4 ## explicit; go 1.12 github.com/jesseduffield/gocui # github.com/jesseduffield/kill v0.0.0-20250101124109-e216ddbe133a From 1c5fe8ff173d848125d54b15aebefeae51f37eae Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 15 Dec 2024 18:05:39 +0100 Subject: [PATCH 2/4] Add a test demonstrating the problem When pressing `e` on line 5 in a diff of an older commit, we expect it to take us to line 5 in that file. But we end up on line 2, because the file had further changes both in newer commits, and in the unstaged changes of the working copy. --- .../edit_line_in_patch_building_panel.go | 50 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 51 insertions(+) create mode 100644 pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go diff --git a/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go b/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go new file mode 100644 index 00000000000..19c3a637036 --- /dev/null +++ b/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go @@ -0,0 +1,50 @@ +package patch_building + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var EditLineInPatchBuildingPanel = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Edit a line in the patch building panel; make sure we end up on the right line", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().OS.EditAtLine = "echo {{filename}}:{{line}} > edit-command" + }, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("file.txt", "4\n5\n6\n") + shell.Commit("01") + shell.UpdateFileAndAdd("file.txt", "1\n2a\n2b\n3\n4\n5\n6\n") + shell.Commit("02") + shell.UpdateFile("file.txt", "1\n2\n3\n4\n5\n6\n") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("02").IsSelected(), + Contains("01"), + ). + Press(keys.Universal.NextItem). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Contains("A file.txt").IsSelected(), + ). + PressEnter() + + t.Views().PatchBuilding(). + IsFocused(). + Content(Contains("+4\n+5\n+6")). + NavigateToLine(Contains("+5")). + Press(keys.Universal.Edit) + + /* EXPECTED: + t.FileSystem().FileContent("edit-command", Contains("file.txt:5\n")) + ACTUAL: */ + t.FileSystem().FileContent("edit-command", Contains("file.txt:2\n")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 2f5063822ba..d7ce5e20462 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -257,6 +257,7 @@ var tests = []*components.IntegrationTest{ patch_building.Apply, patch_building.ApplyInReverse, patch_building.ApplyInReverseWithConflict, + patch_building.EditLineInPatchBuildingPanel, patch_building.MoveRangeToIndex, patch_building.MoveToEarlierCommit, patch_building.MoveToEarlierCommitFromAddedFile, From eaaf12323891ee4cd1da8e3d78ff91a959bfe5b8 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 13 Dec 2024 15:40:21 +0100 Subject: [PATCH 3/4] Combine GetPathDiff and GetAllDiff into one command (GetDiff) This makes it more reusable for other purposes. --- pkg/commands/git_commands/diff.go | 34 +++++++++++-------------- pkg/gui/controllers/files_controller.go | 4 +-- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/pkg/commands/git_commands/diff.go b/pkg/commands/git_commands/diff.go index d7121db9925..b3c97faaa8a 100644 --- a/pkg/commands/git_commands/diff.go +++ b/pkg/commands/git_commands/diff.go @@ -16,6 +16,8 @@ func NewDiffCommands(gitCommon *GitCommon) *DiffCommands { } } +// This is for generating diffs to be shown in the UI (e.g. rendering a range +// diff to the main view). It uses a custom pager if one is configured. func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj { extDiffCmd := self.UserConfig().Git.Paging.ExternalDiffCommand useExtDiff := extDiffCmd != "" @@ -36,27 +38,21 @@ func (self *DiffCommands) DiffCmdObj(diffArgs []string) oscommands.ICmdObj { ) } -func (self *DiffCommands) internalDiffCmdObj(diffArgs ...string) *GitCommandBuilder { - return NewGitCmd("diff"). - Config("diff.noprefix=false"). - Arg("--no-ext-diff", "--no-color"). - Arg(diffArgs...). - Dir(self.repoPaths.worktreePath) -} - -func (self *DiffCommands) GetPathDiff(path string, staged bool) (string, error) { +// This is a basic generic diff command that can be used for any diff operation +// (e.g. copying a diff to the clipboard). It will not use a custom pager, and +// does not use user configs such as ignore whitespace. +// If you want to diff specific refs (one or two), you need to add them yourself +// in additionalArgs; it is recommended to also pass `--` after that. If you +// want to restrict the diff to specific paths, pass them in additionalArgs +// after the `--`. +func (self *DiffCommands) GetDiff(staged bool, additionalArgs ...string) (string, error) { return self.cmd.New( - self.internalDiffCmdObj(). - ArgIf(staged, "--staged"). - Arg(path). - ToArgv(), - ).RunWithOutput() -} - -func (self *DiffCommands) GetAllDiff(staged bool) (string, error) { - return self.cmd.New( - self.internalDiffCmdObj(). + NewGitCmd("diff"). + Config("diff.noprefix=false"). + Arg("--no-ext-diff", "--no-color"). ArgIf(staged, "--staged"). + Dir(self.repoPaths.worktreePath). + Arg(additionalArgs...). ToArgv(), ).RunWithOutput() } diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go index baacc8061f9..11414789a84 100644 --- a/pkg/gui/controllers/files_controller.go +++ b/pkg/gui/controllers/files_controller.go @@ -869,7 +869,7 @@ func (self *FilesController) openCopyMenu() error { OnPress: func() error { path := self.context().GetSelectedPath() hasStaged := self.hasPathStagedChanges(node) - diff, err := self.c.Git().Diff.GetPathDiff(path, hasStaged) + diff, err := self.c.Git().Diff.GetDiff(hasStaged, "--", path) if err != nil { return err } @@ -894,7 +894,7 @@ func (self *FilesController) openCopyMenu() error { Tooltip: self.c.Tr.CopyFileDiffTooltip, OnPress: func() error { hasStaged := self.c.Helpers().WorkingTree.AnyStagedFiles() - diff, err := self.c.Git().Diff.GetAllDiff(hasStaged) + diff, err := self.c.Git().Diff.GetDiff(hasStaged, "--") if err != nil { return err } From 64cd7cd9f6c9642c6321495f44e1396e8399f4cc Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 13 Dec 2024 21:20:09 +0100 Subject: [PATCH 4/4] Adjust line number for working copy when editing a line There are two ways to jump to the editor on a specific line: pressing `e` in the staging or patch building panels, or clicking on a hyperlink in a delta diff. In both cases, this works perfectly in the unstaged changes view, but in other views (either staged changes, or an older commit) it can often jump to the wrong line; this happens when there are further changes to the file being viewed in later commits or in unstaged changes. This commit fixes this so that you end up on the right line in these cases. --- pkg/commands/patch/patch.go | 21 +++++++ pkg/commands/patch/patch_test.go | 56 +++++++++++++++++++ pkg/gui/context/branches_context.go | 8 +++ pkg/gui/context/commit_files_context.go | 7 +++ pkg/gui/context/local_commits_context.go | 8 +++ pkg/gui/context/reflog_commits_context.go | 4 ++ pkg/gui/context/remote_branches_context.go | 4 ++ pkg/gui/context/remotes_context.go | 4 ++ pkg/gui/context/stash_context.go | 4 ++ pkg/gui/context/sub_commits_context.go | 8 +++ pkg/gui/context/tags_context.go | 4 ++ pkg/gui/controllers/helpers/diff_helper.go | 43 ++++++++++++++ .../controllers/patch_building_controller.go | 1 + pkg/gui/controllers/staging_controller.go | 1 + pkg/gui/gui.go | 1 + pkg/gui/types/context.go | 8 +++ .../edit_line_in_patch_building_panel.go | 3 - 17 files changed, 182 insertions(+), 3 deletions(-) diff --git a/pkg/commands/patch/patch.go b/pkg/commands/patch/patch.go index 04933472747..d785fe49e27 100644 --- a/pkg/commands/patch/patch.go +++ b/pkg/commands/patch/patch.go @@ -154,3 +154,24 @@ func (self *Patch) LineCount() int { func (self *Patch) HunkCount() int { return len(self.hunks) } + +// Adjust the given line number (one-based) according to the current patch. The +// patch is supposed to be a diff of an old file state against the working +// directory; the line number is a line number in that old file, and the +// function returns the corresponding line number in the working directory file. +func (self *Patch) AdjustLineNumber(lineNumber int) int { + adjustedLineNumber := lineNumber + for _, hunk := range self.hunks { + if hunk.oldStart >= lineNumber { + break + } + + if hunk.oldStart+hunk.oldLength() > lineNumber { + return hunk.newStart + } + + adjustedLineNumber += hunk.newLength() - hunk.oldLength() + } + + return adjustedLineNumber +} diff --git a/pkg/commands/patch/patch_test.go b/pkg/commands/patch/patch_test.go index fc166cbae46..d9d330017f0 100644 --- a/pkg/commands/patch/patch_test.go +++ b/pkg/commands/patch/patch_test.go @@ -639,3 +639,59 @@ func TestGetNextStageableLineIndex(t *testing.T) { }) } } + +func TestAdjustLineNumber(t *testing.T) { + type scenario struct { + oldLineNumbers []int + expectedResults []int + } + scenarios := []scenario{ + { + oldLineNumbers: []int{1, 2, 3, 4, 5, 6, 7}, + expectedResults: []int{1, 2, 2, 3, 4, 7, 8}, + }, + } + + // The following diff was generated from old.txt: + // 1 + // 2a + // 2b + // 3 + // 4 + // 7 + // 8 + // against new.txt: + // 1 + // 2 + // 3 + // 4 + // 5 + // 6 + // 7 + // 8 + + // This test setup makes the test easy to understand, because the resulting + // adjusted line numbers are the same as the content of the lines in new.txt. + + diff := `--- old.txt 2024-12-16 18:04:29 ++++ new.txt 2024-12-16 18:04:27 +@@ -2,2 +2 @@ +-2a +-2b ++2 +@@ -5,0 +5,2 @@ ++5 ++6 +` + + patch := Parse(diff) + + for _, s := range scenarios { + t.Run("TestAdjustLineNumber", func(t *testing.T) { + for idx, oldLineNumber := range s.oldLineNumbers { + result := patch.AdjustLineNumber(oldLineNumber) + assert.Equal(t, s.expectedResults[idx], result) + } + }) + } +} diff --git a/pkg/gui/context/branches_context.go b/pkg/gui/context/branches_context.go index f03c05990a3..faff68ba9ce 100644 --- a/pkg/gui/context/branches_context.go +++ b/pkg/gui/context/branches_context.go @@ -80,6 +80,14 @@ func (self *BranchesContext) GetDiffTerminals() []string { return nil } +func (self *BranchesContext) RefForAdjustingLineNumberInDiff() string { + branch := self.GetSelected() + if branch != nil { + return branch.ID() + } + return "" +} + func (self *BranchesContext) ShowBranchHeadsInSubCommits() bool { return true } diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go index 4e938248129..dc92139bddd 100644 --- a/pkg/gui/context/commit_files_context.go +++ b/pkg/gui/context/commit_files_context.go @@ -77,6 +77,13 @@ func (self *CommitFilesContext) GetDiffTerminals() []string { return []string{self.GetRef().RefName()} } +func (self *CommitFilesContext) RefForAdjustingLineNumberInDiff() string { + if refs := self.GetRefRange(); refs != nil { + return refs.To.RefName() + } + return self.GetRef().RefName() +} + func (self *CommitFilesContext) GetFromAndToForDiff() (string, string) { if refs := self.GetRefRange(); refs != nil { return refs.From.ParentRefName(), refs.To.RefName() diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index 6d1a72aae3f..4f2f797e5da 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -170,6 +170,14 @@ func (self *LocalCommitsContext) GetDiffTerminals() []string { return []string{itemId} } +func (self *LocalCommitsContext) RefForAdjustingLineNumberInDiff() string { + commits, _, _ := self.GetSelectedItems() + if commits == nil { + return "" + } + return commits[0].Hash +} + func (self *LocalCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition { return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr) } diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index db33481e59d..518edeefa88 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -86,6 +86,10 @@ func (self *ReflogCommitsContext) GetDiffTerminals() []string { return []string{itemId} } +func (self *ReflogCommitsContext) RefForAdjustingLineNumberInDiff() string { + return self.GetSelectedItemId() +} + func (self *ReflogCommitsContext) ShowBranchHeadsInSubCommits() bool { return false } diff --git a/pkg/gui/context/remote_branches_context.go b/pkg/gui/context/remote_branches_context.go index 892953f8256..9e9b00eb19b 100644 --- a/pkg/gui/context/remote_branches_context.go +++ b/pkg/gui/context/remote_branches_context.go @@ -78,6 +78,10 @@ func (self *RemoteBranchesContext) GetDiffTerminals() []string { return []string{itemId} } +func (self *RemoteBranchesContext) RefForAdjustingLineNumberInDiff() string { + return self.GetSelectedItemId() +} + func (self *RemoteBranchesContext) ShowBranchHeadsInSubCommits() bool { return true } diff --git a/pkg/gui/context/remotes_context.go b/pkg/gui/context/remotes_context.go index 237783dcad7..4a96bbc1881 100644 --- a/pkg/gui/context/remotes_context.go +++ b/pkg/gui/context/remotes_context.go @@ -53,3 +53,7 @@ func (self *RemotesContext) GetDiffTerminals() []string { return []string{itemId} } + +func (self *RemotesContext) RefForAdjustingLineNumberInDiff() string { + return "" +} diff --git a/pkg/gui/context/stash_context.go b/pkg/gui/context/stash_context.go index 64c7c9fc979..7dc0066d958 100644 --- a/pkg/gui/context/stash_context.go +++ b/pkg/gui/context/stash_context.go @@ -71,3 +71,7 @@ func (self *StashContext) GetDiffTerminals() []string { return []string{itemId} } + +func (self *StashContext) RefForAdjustingLineNumberInDiff() string { + return self.GetSelectedItemId() +} diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index cd19dcae228..b3b3fd1b476 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -217,6 +217,14 @@ func (self *SubCommitsContext) GetDiffTerminals() []string { return []string{itemId} } +func (self *SubCommitsContext) RefForAdjustingLineNumberInDiff() string { + commits, _, _ := self.GetSelectedItems() + if commits == nil { + return "" + } + return commits[0].Hash +} + func (self *SubCommitsContext) ModelSearchResults(searchStr string, caseSensitive bool) []gocui.SearchPosition { return searchModelCommits(caseSensitive, self.GetCommits(), self.ColumnPositions(), searchStr) } diff --git a/pkg/gui/context/tags_context.go b/pkg/gui/context/tags_context.go index 39ae607027e..c77a5292af7 100644 --- a/pkg/gui/context/tags_context.go +++ b/pkg/gui/context/tags_context.go @@ -66,6 +66,10 @@ func (self *TagsContext) GetDiffTerminals() []string { return []string{itemId} } +func (self *TagsContext) RefForAdjustingLineNumberInDiff() string { + return self.GetSelectedItemId() +} + func (self *TagsContext) ShowBranchHeadsInSubCommits() bool { return true } diff --git a/pkg/gui/controllers/helpers/diff_helper.go b/pkg/gui/controllers/helpers/diff_helper.go index 2eda84fc11b..7035849d4ff 100644 --- a/pkg/gui/controllers/helpers/diff_helper.go +++ b/pkg/gui/controllers/helpers/diff_helper.go @@ -5,6 +5,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/commands/git_commands" "github.com/jesseduffield/lazygit/pkg/commands/models" + "github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/gui/context" "github.com/jesseduffield/lazygit/pkg/gui/modes/diffing" "github.com/jesseduffield/lazygit/pkg/gui/style" @@ -164,3 +165,45 @@ func (self *DiffHelper) OpenDiffToolForRef(selectedRef types.Ref) error { })) return err } + +// AdjustLineNumber is used to adjust a line number in the diff that's currently +// being viewed, so that it corresponds to the line number in the actual working +// copy state of the file. It is used when clicking on a delta hyperlink in a +// diff, or when pressing `e` in the staging or patch building panels. It works +// by getting a diff of what's being viewed in the main view against the working +// copy, and then using that diff to adjust the line number. +// path is the file path of the file being viewed +// linenumber is the line number to adjust (one-based) +// viewname is the name of the view that shows the diff. We need to pass it +// because the diff adjustment is slightly different depending on which view is +// showing the diff. +func (self *DiffHelper) AdjustLineNumber(path string, linenumber int, viewname string) int { + switch viewname { + + case "main", "patchBuilding": + if diffableContext, ok := self.c.Context().CurrentSide().(types.DiffableContext); ok { + ref := diffableContext.RefForAdjustingLineNumberInDiff() + if len(ref) != 0 { + return self.adjustLineNumber(linenumber, ref, "--", path) + } + } + // if the type cast to DiffableContext returns false, we are in the + // unstaged changes view of the Files panel; no need to adjust line + // numbers in this case + + case "secondary", "stagingSecondary": + return self.adjustLineNumber(linenumber, "--", path) + } + + return linenumber +} + +func (self *DiffHelper) adjustLineNumber(linenumber int, diffArgs ...string) int { + args := append([]string{"--unified=0"}, diffArgs...) + diff, err := self.c.Git().Diff.GetDiff(false, args...) + if err != nil { + return linenumber + } + patch := patch.Parse(diff) + return patch.AdjustLineNumber(linenumber) +} diff --git a/pkg/gui/controllers/patch_building_controller.go b/pkg/gui/controllers/patch_building_controller.go index 7bc0ffb8320..014b8d7b744 100644 --- a/pkg/gui/controllers/patch_building_controller.go +++ b/pkg/gui/controllers/patch_building_controller.go @@ -107,6 +107,7 @@ func (self *PatchBuildingController) EditFile() error { } lineNumber := self.context().GetState().CurrentLineNumber() + lineNumber = self.c.Helpers().Diff.AdjustLineNumber(path, lineNumber, self.context().GetViewName()) return self.c.Helpers().Files.EditFileAtLine(path, lineNumber) } diff --git a/pkg/gui/controllers/staging_controller.go b/pkg/gui/controllers/staging_controller.go index fbcbc049bdd..c3ea3ca247f 100644 --- a/pkg/gui/controllers/staging_controller.go +++ b/pkg/gui/controllers/staging_controller.go @@ -161,6 +161,7 @@ func (self *StagingController) EditFile() error { } lineNumber := self.context.GetState().CurrentLineNumber() + lineNumber = self.c.Helpers().Diff.AdjustLineNumber(path, lineNumber, self.context.GetViewName()) return self.c.Helpers().Files.EditFileAtLine(path, lineNumber) } diff --git a/pkg/gui/gui.go b/pkg/gui/gui.go index 427cd465ded..0ca43d780e9 100644 --- a/pkg/gui/gui.go +++ b/pkg/gui/gui.go @@ -368,6 +368,7 @@ func (gui *Gui) onNewRepo(startArgs appTypes.StartArgs, contextKey types.Context filepath := matches[1] if matches[2] != "" { lineNumber := utils.MustConvertToInt(matches[2]) + lineNumber = gui.helpers.Diff.AdjustLineNumber(filepath, lineNumber, viewname) return gui.helpers.Files.EditFileAtLine(filepath, lineNumber) } return gui.helpers.Files.EditFiles([]string{filepath}) diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go index a2ee3425f3b..5ad8ff3bbf4 100644 --- a/pkg/gui/types/context.go +++ b/pkg/gui/types/context.go @@ -152,6 +152,14 @@ type DiffableContext interface { // which becomes an option when you bring up the diff menu, but when you're just // flicking through branches it will be using the local branch name. GetDiffTerminals() []string + + // Returns the ref that should be used for creating a diff of what's + // currently shown in the main view against the working directory, in order + // to adjust line numbers in the diff to match the current state of the + // shown file. For example, if the main view shows a range diff of commits, + // we need to pass the first commit of the range. This is used by + // DiffHelper.AdjustLineNumber. + RefForAdjustingLineNumberInDiff() string } type IListContext interface { diff --git a/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go b/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go index 19c3a637036..2be88b7b8d1 100644 --- a/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go +++ b/pkg/integration/tests/patch_building/edit_line_in_patch_building_panel.go @@ -42,9 +42,6 @@ var EditLineInPatchBuildingPanel = NewIntegrationTest(NewIntegrationTestArgs{ NavigateToLine(Contains("+5")). Press(keys.Universal.Edit) - /* EXPECTED: t.FileSystem().FileContent("edit-command", Contains("file.txt:5\n")) - ACTUAL: */ - t.FileSystem().FileContent("edit-command", Contains("file.txt:2\n")) }, })