mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-08-06 22:33:07 +02:00
Don't use hunk mode for added or deleted files
When entering staging (or patch building) for an added or deleted file, it doesn't make sense to use hunk mode, because pressing space would stage/unstage the entire file, and if the user wanted to do that, they would have pressed space in the Files panel. So always use line mode for added/deleted files by default, even if the useHunkModeInStagingView user config is on.
This commit is contained in:
@ -186,3 +186,17 @@ func (self *Patch) AdjustLineNumber(lineNumber int) int {
|
|||||||
|
|
||||||
return adjustedLineNumber
|
return adjustedLineNumber
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *Patch) IsSingleHunkForWholeFile() bool {
|
||||||
|
if len(self.hunks) != 1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// We consider a patch to be a single hunk for the whole file if it has only additions or
|
||||||
|
// deletions but not both, and no context lines. This not quite correct, because it will also
|
||||||
|
// return true for a block of added or deleted lines if the diff context size is 0, but in this
|
||||||
|
// case you wouldn't be able to stage things anyway, so it doesn't matter.
|
||||||
|
bodyLines := self.hunks[0].bodyLines
|
||||||
|
return nLinesWithKind(bodyLines, []PatchLineKind{DELETION, CONTEXT}) == 0 ||
|
||||||
|
nLinesWithKind(bodyLines, []PatchLineKind{ADDITION, CONTEXT}) == 0
|
||||||
|
}
|
||||||
|
@ -710,3 +710,64 @@ func TestAdjustLineNumber(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsSingleHunkForWholeFile(t *testing.T) {
|
||||||
|
scenarios := []struct {
|
||||||
|
testName string
|
||||||
|
patchStr string
|
||||||
|
expectedResult bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
testName: "simpleDiff",
|
||||||
|
patchStr: simpleDiff,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "addNewlineToEndOfFile",
|
||||||
|
patchStr: addNewlineToEndOfFile,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "removeNewlinefromEndOfFile",
|
||||||
|
patchStr: removeNewlinefromEndOfFile,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "twoHunks",
|
||||||
|
patchStr: twoHunks,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "twoChangesInOneHunk",
|
||||||
|
patchStr: twoChangesInOneHunk,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "newFile",
|
||||||
|
patchStr: newFile,
|
||||||
|
expectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "deletedFile",
|
||||||
|
patchStr: deletedFile,
|
||||||
|
expectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "addNewlineToPreviouslyEmptyFile",
|
||||||
|
patchStr: addNewlineToPreviouslyEmptyFile,
|
||||||
|
expectedResult: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
testName: "exampleHunk",
|
||||||
|
patchStr: exampleHunk,
|
||||||
|
expectedResult: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range scenarios {
|
||||||
|
t.Run(s.testName, func(t *testing.T) {
|
||||||
|
patch := Parse(s.patchStr)
|
||||||
|
assert.Equal(t, s.expectedResult, patch.IsSingleHunkForWholeFile())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -67,7 +67,7 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
|
|||||||
}
|
}
|
||||||
|
|
||||||
selectMode := LINE
|
selectMode := LINE
|
||||||
if useHunkModeByDefault {
|
if useHunkModeByDefault && !patch.IsSingleHunkForWholeFile() {
|
||||||
selectMode = HUNK
|
selectMode = HUNK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user