Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 77 additions & 37 deletions pkg/gui/controllers/commit_diff_actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"errors"
"fmt"

"github.com/jesseduffield/generics/set"
Expand All @@ -9,6 +10,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -104,6 +106,10 @@ func (self *CommitDiffActions) PrimaryAction(pane types.DiffPaneContext, firstBu
if len(lines) == 0 {
return nil
}
selection, err := self.selectionByFile(lines)
if err != nil {
return err
}

patchBuilder := self.c.Git().Patch.PatchBuilder
from, reverse := self.patchEndpoints(target)
Expand All @@ -121,14 +127,16 @@ func (self *CommitDiffActions) PrimaryAction(pane types.DiffPaneContext, firstBu
patchBuilder.Start(from, target.to, reverse, target.canRebase)
}

if err := self.togglePatchLines(lines); err != nil {
return err
}
err := self.togglePatchLines(selection)
// Taking the last line back out ends the patch rather than leaving an empty
// one, so that the pane previewing it and the marks over the diff go with it.
// The same goes for a patch just started that no line could be put into.
if patchBuilder.IsEmpty() {
patchBuilder.Reset()
}
if err != nil {
return err
}

// The diff on screen is the one the marks belong to, so they can be brought up
// to date at once rather than waiting for the render below.
Expand Down Expand Up @@ -167,11 +175,15 @@ func (self *CommitDiffActions) removePatchLines(
}

patchBuilder := self.c.Git().Patch.PatchBuilder
files := self.filesInDiff()
files, err := self.filesInDiff()
if err != nil {
return err
}
indicesByFilename := map[string][]int{}
for path, ordinals := range self.c.Helpers().DiffLine.ChangeLineOrdinals(self.customPatchDiff(), lines) {
filename := self.patchBuilderPath(path)
if filename == "" {
continue
return diffLinesFileNotFoundError(self.c, path)
}
included := patchBuilder.IncludedChangeLineIndices(filename)
indices := []int{}
Expand All @@ -181,8 +193,11 @@ func (self *CommitDiffActions) removePatchLines(
}
}
if len(indices) == 0 {
continue
return diffLinesNotFoundError(self.c, filename)
}
indicesByFilename[filename] = indices
}
for filename, indices := range indicesByFilename {
if err := patchBuilder.RemoveFileLineRange(filename, files.previousPath(filename), indices); err != nil {
return err
}
Expand Down Expand Up @@ -223,7 +238,12 @@ func (self *CommitDiffActions) DiscardSelection(pane types.DiffPaneContext, firs
}
commitIndex := self.indexOfTargetCommit(target)
if commitIndex == -1 {
return nil
return errors.New(utils.ResolvePlaceholderString(self.c.Tr.CommitNotInCommitsPanel,
map[string]string{"hash": utils.ShortHash(target.to)}))
}
selection, err := self.selectionByFile(lines)
if err != nil {
return err
}

patchBuilder := self.c.Git().Patch.PatchBuilder
Expand All @@ -238,7 +258,10 @@ func (self *CommitDiffActions) DiscardSelection(pane types.DiffPaneContext, firs
from, reverse := self.patchEndpoints(target)
patchBuilder.Reset()
patchBuilder.Start(from, target.to, reverse, target.canRebase)
if err := self.togglePatchLines(lines); err != nil {
if err := self.togglePatchLines(selection); err != nil {
// This patch exists only to be removed from the commit, so don't leave it
// behind.
patchBuilder.Reset()
return err
}
if patchBuilder.IsEmpty() {
Expand Down Expand Up @@ -318,41 +341,61 @@ func (self *CommitDiffActions) PatchInclusion() func(types.DiffLineInfo) bool {
}
}

// togglePatchLines takes the given lines of the commit's diff into the custom patch, or
// out of it. The first line of the selection decides which of the two happens, once for
// the whole selection: pointing at a line that is already in the patch takes the whole
// selection out of it, as toggling a selection of files in the commit files panel does.
func (self *CommitDiffActions) togglePatchLines(lines []types.DiffLineInfo) error {
patchBuilder := self.c.Git().Patch.PatchBuilder
// commitDiffSelection is the selected lines of a commit's diff, by the file they belong
// to: a patch is built a file at a time, while a selection can span several of them.
type commitDiffSelection struct {
// What the diff says about each of the files it covers.
files commitDiffFiles

// The files the selection covers, in the order the diff shows them, and per file the
// lines of it that are selected: a patch is built a file at a time, while a selection
// can span several of them.
paths := []string{}
linesByPath := map[string][]patch.LineIdentity{}
// The files the selection covers, in the order the diff shows them.
paths []string

// Per file, the lines of it that are selected.
linesByPath map[string][]patch.LineIdentity
}

// selectionByFile groups the given lines of the commit's diff by the file they belong
// to. If a line belongs to no file of the diff, the diff on screen and the one the
// patch is built from disagree, so this fails and we build nothing.
func (self *CommitDiffActions) selectionByFile(lines []types.DiffLineInfo) (commitDiffSelection, error) {
files, err := self.filesInDiff()
if err != nil {
return commitDiffSelection{}, err
}
selection := commitDiffSelection{files: files, linesByPath: map[string][]patch.LineIdentity{}}
for _, line := range lines {
path := self.patchBuilderPath(line.Path)
if path == "" {
continue
if _, ok := files[path]; !ok {
return commitDiffSelection{}, diffLinesFileNotFoundError(self.c, line.Path)
}
if _, seen := linesByPath[path]; !seen {
paths = append(paths, path)
if _, seen := selection.linesByPath[path]; !seen {
selection.paths = append(selection.paths, path)
}
linesByPath[path] = append(linesByPath[path], line.PatchLineIdentity())
}
if len(paths) == 0 {
return nil
selection.linesByPath[path] = append(selection.linesByPath[path], line.PatchLineIdentity())
}
return selection, nil
}

// togglePatchLines takes the selected lines of the commit's diff into the custom patch,
// or out of it. The first line of the selection decides which of the two happens, once
// for the whole selection: pointing at a line that is already in the patch takes the
// whole selection out of it, as toggling a selection of files in the commit files panel
// does.
func (self *CommitDiffActions) togglePatchLines(selection commitDiffSelection) error {
patchBuilder := self.c.Git().Patch.PatchBuilder
files, paths := selection.files, selection.paths

files := self.filesInDiff()
indicesByPath := map[string][]int{}
wholeFileByPath := map[string]bool{}
for _, path := range paths {
indices, everyChange, err := patchBuilder.PatchLineIndicesForLines(
path, files.previousPath(path), linesByPath[path])
path, files.previousPath(path), selection.linesByPath[path])
if err != nil {
return err
}
if len(indices) == 0 {
return diffLinesNotFoundError(self.c, path)
}
indicesByPath[path] = indices

// Selecting every change of a file the commit adds or deletes is selecting the
Expand All @@ -365,12 +408,9 @@ func (self *CommitDiffActions) togglePatchLines(lines []types.DiffLineInfo) erro
if err != nil {
return err
}
removing := len(indicesByPath[paths[0]]) > 0 && lo.Contains(included, indicesByPath[paths[0]][0])
removing := lo.Contains(included, indicesByPath[paths[0]][0])

for _, path := range paths {
if len(indicesByPath[path]) == 0 {
continue
}
previousPath := files.previousPath(path)
var err error
switch {
Expand Down Expand Up @@ -415,20 +455,20 @@ func (self commitDiffFiles) isWholeFileOperation(path string) bool {

// filesInDiff asks git which files the diff a patch is being built from covers, and
// what it does to each.
func (self *CommitDiffActions) filesInDiff() commitDiffFiles {
func (self *CommitDiffActions) filesInDiff() (commitDiffFiles, error) {
target := self.target()
if target == nil {
return nil
return nil, nil
}
from, reverse := self.patchEndpoints(target)
files, err := self.c.Git().Loaders.CommitFileLoader.GetFilesInDiff(from, target.to, reverse)
if err != nil {
return nil
return nil, err
}

return lo.SliceToMap(files, func(file *models.CommitFile) (string, *models.CommitFile) {
return file.Path, file
})
}), nil
}

// patchEndpoints gives the two ends of the diff a patch is built from. They are the ends
Expand Down
21 changes: 21 additions & 0 deletions pkg/gui/controllers/diff_paths.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package controllers

import (
"errors"
"path"
"path/filepath"
"strings"

"github.com/jesseduffield/lazygit/pkg/gui/filetree"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)

Expand Down Expand Up @@ -132,6 +134,25 @@ func isInDir(path string, dir string) bool {
return dir == "." || strings.HasPrefix(path, dir+"/")
}

// diffLinesFileNotFoundError reports that the file selected diff lines belong to can't
// be found. It names the file by its path in the repo, or by the path the diff line
// carries for one outside of it.
func diffLinesFileNotFoundError(c *ControllerCommon, absolutePath string) error {
path := repoRelativePath(c.Git().RepoPaths.WorktreePath(), absolutePath)
if path == "" {
path = absolutePath
}
return errors.New(utils.ResolvePlaceholderString(c.Tr.DiffLinesFileNotFound,
map[string]string{"path": path}))
}

// diffLinesNotFoundError reports that selected diff lines can't be found in the diff of
// the file at the given path in the repo.
func diffLinesNotFoundError(c *ControllerCommon, path string) error {
return errors.New(utils.ResolvePlaceholderString(c.Tr.DiffLinesNotFound,
map[string]string{"path": path}))
}

// repoRelativePath turns the absolute path a diff line carries into the one git speaks
// of the file: relative to the worktree, with forward slashes. It is "" for a path that
// is no file of this repo, which a diff renderer's own naming of a line can produce.
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/controllers/main_view_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ func (self *MainViewController) openPullRequestAtSelectedLine() error {
}
relativePath := repoRelativePath(self.c.Git().RepoPaths.WorktreePath(), info.Path)
if relativePath == "" {
return nil
return diffLinesFileNotFoundError(self.c, info.Path)
}

self.c.LogAction(self.c.Tr.Actions.OpenPullRequest)
Expand Down
Loading
Loading