Skip to content

Commit 2bd6881

Browse files
authored
Don't use hunk mode for added or deleted files even when useHunkModeInStagingView config is on (#4758)
- **PR Description** 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.
2 parents 16231a1 + c5acad7 commit 2bd6881

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ index 0000000..4e680cc
115115
+grape
116116
`
117117

118+
const deletedFile = `diff --git a/newfile b/newfile
119+
deleted file mode 100644
120+
index 4e680cc1f..000000000
121+
--- a/newfile
122+
+++ /dev/null
123+
@@ -1,3 +0,0 @@
124+
-apple
125+
-orange
126+
-grape
127+
`
128+
118129
const addNewlineToPreviouslyEmptyFile = `diff --git a/newfile b/newfile
119130
index e69de29..c6568ea 100644
120131
--- a/newfile
@@ -554,6 +565,10 @@ func TestParseAndFormatPlain(t *testing.T) {
554565
testName: "newFile",
555566
patchStr: newFile,
556567
},
568+
{
569+
testName: "deletedFile",
570+
patchStr: deletedFile,
571+
},
557572
{
558573
testName: "addNewlineToPreviouslyEmptyFile",
559574
patchStr: addNewlineToPreviouslyEmptyFile,
@@ -695,3 +710,64 @@ func TestAdjustLineNumber(t *testing.T) {
695710
})
696711
}
697712
}
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)