summaryrefslogtreecommitdiffstats
path: root/pkg/commands/patch
diff options
context:
space:
mode:
authorRyooooooga <eial5q265e5@gmail.com>2021-08-04 18:43:34 +0900
committerRyooooooga <eial5q265e5@gmail.com>2021-08-04 18:43:34 +0900
commitac609bd37c0b879ce08f0fcff7b1c7ef09d37333 (patch)
treee402ef4ca8af9929d0c9e1f9caa18ea25d34bb18 /pkg/commands/patch
parent67cc65930ae62346fcc522f56da59fba5eef1dbe (diff)
fix backward compatibility
Diffstat (limited to 'pkg/commands/patch')
-rw-r--r--pkg/commands/patch/patch_modifier.go10
-rw-r--r--pkg/commands/patch/patch_modifier_test.go2
2 files changed, 7 insertions, 5 deletions
diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go
index e5c6d061f..3ab13931b 100644
--- a/pkg/commands/patch/patch_modifier.go
+++ b/pkg/commands/patch/patch_modifier.go
@@ -137,17 +137,19 @@ func ModifiedPatchForLines(log *logrus.Entry, filename string, diffText string,
}
// I want to know, given a hunk, what line a given index is on
-func (hunk *PatchHunk) LineNumberOfLine(idx int) (int, error) {
+func (hunk *PatchHunk) LineNumberOfLine(idx int) int {
n := idx - hunk.FirstLineIdx - 1
- if n < 0 || len(hunk.bodyLines) <= n {
- return -1, fmt.Errorf("line index out of range")
+ if n < 0 {
+ n = 0
+ } else if n >= len(hunk.bodyLines) {
+ n = len(hunk.bodyLines) - 1
}
lines := hunk.bodyLines[0:n]
offset := nLinesWithPrefix(lines, []string{"+", " "})
- return hunk.newStart + offset, nil
+ return hunk.newStart + offset
}
func nLinesWithPrefix(lines []string, chars []string) int {
diff --git a/pkg/commands/patch/patch_modifier_test.go b/pkg/commands/patch/patch_modifier_test.go
index 1f1eeb114..8b866019b 100644
--- a/pkg/commands/patch/patch_modifier_test.go
+++ b/pkg/commands/patch/patch_modifier_test.go
@@ -539,7 +539,7 @@ func TestLineNumberOfLine(t *testing.T) {
for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) {
- result, _ := s.hunk.LineNumberOfLine(s.idx)
+ result := s.hunk.LineNumberOfLine(s.idx)
if !assert.Equal(t, s.expected, result) {
fmt.Println(result)
}