Skip to content

Commit c5acad7

Browse files
committed
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.
1 parent fc3b725 commit c5acad7

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

pkg/commands/patch/patch.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,17 @@ func (self *Patch) AdjustLineNumber(lineNumber int) int {
186186

187187
return adjustedLineNumber
188188
}
189+
190+
func (self *Patch) IsSingleHunkForWholeFile() bool {
191+
if len(self.hunks) != 1 {
192+
return false
193+
}
194+
195+
// We consider a patch to be a single hunk for the whole file if it has only additions or
196+
// deletions but not both, and no context lines. This not quite correct, because it will also
197+
// return true for a block of added or deleted lines if the diff context size is 0, but in this
198+
// case you wouldn't be able to stage things anyway, so it doesn't matter.
199+
bodyLines := self.hunks[0].bodyLines
200+
return nLinesWithKind(bodyLines, []PatchLineKind{DELETION, CONTEXT}) == 0 ||
201+
nLinesWithKind(bodyLines, []PatchLineKind{ADDITION, CONTEXT}) == 0
202+
}

pkg/commands/patch/patch_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,3 +710,64 @@ func TestAdjustLineNumber(t *testing.T) {
710710
})
711711
}
712712
}
713+
714+
func TestIsSingleHunkForWholeFile(t *testing.T) {
715+
scenarios := []struct {
716+
testName string
717+
patchStr string
718+
expectedResult bool
719+
}{
720+
{
721+
testName: "simpleDiff",
722+
patchStr: simpleDiff,
723+
expectedResult: false,
724+
},
725+
{
726+
testName: "addNewlineToEndOfFile",
727+
patchStr: addNewlineToEndOfFile,
728+
expectedResult: false,
729+
},
730+
{
731+
testName: "removeNewlinefromEndOfFile",
732+
patchStr: removeNewlinefromEndOfFile,
733+
expectedResult: false,
734+
},
735+
{
736+
testName: "twoHunks",
737+
patchStr: twoHunks,
738+
expectedResult: false,
739+
},
740+
{
741+
testName: "twoChangesInOneHunk",
742+
patchStr: twoChangesInOneHunk,
743+
expectedResult: false,
744+
},
745+
{
746+
testName: "newFile",
747+
patchStr: newFile,
748+
expectedResult: true,
749+
},
750+
{
751+
testName: "deletedFile",
752+
patchStr: deletedFile,
753+
expectedResult: true,
754+
},
755+
{
756+
testName: "addNewlineToPreviouslyEmptyFile",
757+
patchStr: addNewlineToPreviouslyEmptyFile,
758+
expectedResult: true,
759+
},
760+
{
761+
testName: "exampleHunk",
762+
patchStr: exampleHunk,
763+
expectedResult: false,
764+
},
765+
}
766+
767+
for _, s := range scenarios {
768+
t.Run(s.testName, func(t *testing.T) {
769+
patch := Parse(s.patchStr)
770+
assert.Equal(t, s.expectedResult, patch.IsSingleHunkForWholeFile())
771+
})
772+
}
773+
}

pkg/gui/patch_exploring/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewState(diff string, selectedLineIdx int, view *gocui.View, oldState *Stat
6767
}
6868

6969
selectMode := LINE
70-
if useHunkModeByDefault {
70+
if useHunkModeByDefault && !patch.IsSingleHunkForWholeFile() {
7171
selectMode = HUNK
7272
}
7373

0 commit comments

Comments
 (0)