mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-01-08 04:04:22 +02:00
Warn when there are hunks with only added lines
The algorithm works by blaming the deleted lines, so if a hunk contains only added lines, we can only hope that it also belongs in the same commit. Warn the user about this. Note: the warning might be overly agressive, we'll have to see if this is annoying. The reason is that it depends on the diff context size whether added lines go into their own hunk or are grouped together with other added or deleted lines into one hunk. However, our algorithm uses a diff context size of 0, because that makes it easiest to parse the diff; this results in hunks having only added lines more often than what the user sees. For example, moving a line of code down by two lines will likely result in a single hunk for the user, but in two hunks for our algorithm. On the other hand, being this strict makes the warning consistent. We could consider using the user's diff context size in the algorithm, but then it would depend on the current context size whether the warning appears, which could be confusing. Plus, it would make the algorithm quite a bit more complicated.
This commit is contained in:
parent
8ca78412ac
commit
b35f8776e1
@ -39,7 +39,7 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
|
||||
return self.c.ErrorMsg(self.c.Tr.NoChangedFiles)
|
||||
}
|
||||
|
||||
deletedLineInfos := self.parseDiff(diff)
|
||||
deletedLineInfos, hasHunksWithOnlyAddedLines := self.parseDiff(diff)
|
||||
if len(deletedLineInfos) == 0 {
|
||||
return self.c.ErrorMsg(self.c.Tr.NoDeletedLinesInDiff)
|
||||
}
|
||||
@ -79,15 +79,29 @@ func (self *FixupHelper) HandleFindBaseCommitForFixupPress() error {
|
||||
return self.c.ErrorMsg(self.c.Tr.BaseCommitIsAlreadyOnMainBranch)
|
||||
}
|
||||
|
||||
if !useIndex {
|
||||
if err := self.c.Git().WorkingTree.StageAll(); err != nil {
|
||||
return err
|
||||
doIt := func() error {
|
||||
if !hasStagedChanges {
|
||||
if err := self.c.Git().WorkingTree.StageAll(); err != nil {
|
||||
return err
|
||||
}
|
||||
_ = self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.FILES}})
|
||||
}
|
||||
_ = self.c.Refresh(types.RefreshOptions{Mode: types.SYNC, Scope: []types.RefreshableView{types.FILES}})
|
||||
|
||||
self.c.Contexts().LocalCommits.SetSelectedLineIdx(index)
|
||||
return self.c.PushContext(self.c.Contexts().LocalCommits)
|
||||
}
|
||||
|
||||
self.c.Contexts().LocalCommits.SetSelectedLineIdx(index)
|
||||
return self.c.PushContext(self.c.Contexts().LocalCommits)
|
||||
if hasHunksWithOnlyAddedLines {
|
||||
return self.c.Confirm(types.ConfirmOpts{
|
||||
Title: self.c.Tr.FindBaseCommitForFixup,
|
||||
Prompt: self.c.Tr.HunksWithOnlyAddedLinesWarning,
|
||||
HandleConfirm: func() error {
|
||||
return doIt()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return doIt()
|
||||
}
|
||||
|
||||
func (self *FixupHelper) getDiff() (string, bool, error) {
|
||||
@ -106,18 +120,23 @@ func (self *FixupHelper) getDiff() (string, bool, error) {
|
||||
return diff, hasStagedChanges, err
|
||||
}
|
||||
|
||||
func (self *FixupHelper) parseDiff(diff string) []*deletedLineInfo {
|
||||
func (self *FixupHelper) parseDiff(diff string) ([]*deletedLineInfo, bool) {
|
||||
lines := strings.Split(strings.TrimSuffix(diff, "\n"), "\n")
|
||||
|
||||
deletedLineInfos := []*deletedLineInfo{}
|
||||
hasHunksWithOnlyAddedLines := false
|
||||
|
||||
hunkHeaderRegexp := regexp.MustCompile(`@@ -(\d+)(?:,\d+)? \+\d+(?:,\d+)? @@`)
|
||||
|
||||
var filename string
|
||||
var currentLineInfo *deletedLineInfo
|
||||
finishHunk := func() {
|
||||
if currentLineInfo != nil && currentLineInfo.numLines > 0 {
|
||||
deletedLineInfos = append(deletedLineInfos, currentLineInfo)
|
||||
if currentLineInfo != nil {
|
||||
if currentLineInfo.numLines > 0 {
|
||||
deletedLineInfos = append(deletedLineInfos, currentLineInfo)
|
||||
} else {
|
||||
hasHunksWithOnlyAddedLines = true
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, line := range lines {
|
||||
@ -139,7 +158,7 @@ func (self *FixupHelper) parseDiff(diff string) []*deletedLineInfo {
|
||||
}
|
||||
finishHunk()
|
||||
|
||||
return deletedLineInfos
|
||||
return deletedLineInfos, hasHunksWithOnlyAddedLines
|
||||
}
|
||||
|
||||
// returns the list of commit hashes that introduced the lines which have now been deleted
|
||||
|
@ -47,6 +47,7 @@ type TranslationSet struct {
|
||||
MultipleBaseCommitsFoundUnstaged string
|
||||
BaseCommitIsAlreadyOnMainBranch string
|
||||
BaseCommitIsNotInCurrentView string
|
||||
HunksWithOnlyAddedLinesWarning string
|
||||
StatusTitle string
|
||||
GlobalTitle string
|
||||
Menu string
|
||||
@ -874,6 +875,7 @@ func EnglishTranslationSet() TranslationSet {
|
||||
MultipleBaseCommitsFoundUnstaged: "Multiple base commits found. (Try staging some of the changes)",
|
||||
BaseCommitIsAlreadyOnMainBranch: "The base commit for this change is already on the main branch",
|
||||
BaseCommitIsNotInCurrentView: "Base commit is not in current view",
|
||||
HunksWithOnlyAddedLinesWarning: "There are ranges of only added lines in the diff; be careful to check that these belong in the found base commit.\n\nProceed?",
|
||||
StatusTitle: "Status",
|
||||
Menu: "Menu",
|
||||
Execute: "Execute",
|
||||
|
@ -0,0 +1,48 @@
|
||||
package commit
|
||||
|
||||
import (
|
||||
"github.com/jesseduffield/lazygit/pkg/config"
|
||||
. "github.com/jesseduffield/lazygit/pkg/integration/components"
|
||||
)
|
||||
|
||||
var FindBaseCommitForFixupWarningForAddedLines = NewIntegrationTest(NewIntegrationTestArgs{
|
||||
Description: "Finds the base commit to create a fixup for, and warns that there are hunks with only added lines",
|
||||
ExtraCmdArgs: []string{},
|
||||
Skip: false,
|
||||
SetupConfig: func(config *config.AppConfig) {},
|
||||
SetupRepo: func(shell *Shell) {
|
||||
shell.NewBranch("mybranch").
|
||||
EmptyCommit("1st commit").
|
||||
CreateFileAndAdd("file1", "file1 content\n").
|
||||
Commit("2nd commit").
|
||||
CreateFileAndAdd("file2", "file2 content\n").
|
||||
Commit("3rd commit").
|
||||
UpdateFile("file1", "file1 changed content").
|
||||
UpdateFile("file2", "file2 content\nadded content")
|
||||
},
|
||||
Run: func(t *TestDriver, keys config.KeybindingConfig) {
|
||||
t.Views().Commits().
|
||||
Lines(
|
||||
Contains("3rd commit").IsSelected(),
|
||||
Contains("2nd commit"),
|
||||
Contains("1st commit"),
|
||||
)
|
||||
|
||||
t.Views().Files().
|
||||
Focus().
|
||||
Press(keys.Files.FindBaseCommitForFixup)
|
||||
|
||||
t.ExpectPopup().Confirmation().
|
||||
Title(Equals("Find base commit for fixup")).
|
||||
Content(Contains("There are ranges of only added lines in the diff; be careful to check that these belong in the found base commit.")).
|
||||
Confirm()
|
||||
|
||||
t.Views().Commits().
|
||||
IsFocused().
|
||||
Lines(
|
||||
Contains("3rd commit"),
|
||||
Contains("2nd commit").IsSelected(),
|
||||
Contains("1st commit"),
|
||||
)
|
||||
},
|
||||
})
|
@ -71,6 +71,7 @@ var tests = []*components.IntegrationTest{
|
||||
commit.CreateTag,
|
||||
commit.DiscardOldFileChange,
|
||||
commit.FindBaseCommitForFixup,
|
||||
commit.FindBaseCommitForFixupWarningForAddedLines,
|
||||
commit.Highlight,
|
||||
commit.History,
|
||||
commit.HistoryComplex,
|
||||
|
Loading…
Reference in New Issue
Block a user