diff options
author | Ryooooooga <eial5q265e5@gmail.com> | 2021-08-03 22:00:28 +0900 |
---|---|---|
committer | Ryooooooga <eial5q265e5@gmail.com> | 2021-08-03 22:00:28 +0900 |
commit | 67cc65930ae62346fcc522f56da59fba5eef1dbe (patch) | |
tree | 9f4edf288485252c718988c0031e7d23077ace6b /pkg/commands/patch | |
parent | 4f66093335e5a0d370cf43b3fc637c0795376334 (diff) |
fix out of range error
Diffstat (limited to 'pkg/commands/patch')
-rw-r--r-- | pkg/commands/patch/patch_modifier.go | 11 | ||||
-rw-r--r-- | pkg/commands/patch/patch_modifier_test.go | 2 |
2 files changed, 9 insertions, 4 deletions
diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go index c2bbc60f6..e5c6d061f 100644 --- a/pkg/commands/patch/patch_modifier.go +++ b/pkg/commands/patch/patch_modifier.go @@ -137,12 +137,17 @@ 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 { - lines := hunk.bodyLines[0 : idx-hunk.FirstLineIdx-1] +func (hunk *PatchHunk) LineNumberOfLine(idx int) (int, error) { + n := idx - hunk.FirstLineIdx - 1 + if n < 0 || len(hunk.bodyLines) <= n { + return -1, fmt.Errorf("line index out of range") + } + + lines := hunk.bodyLines[0:n] offset := nLinesWithPrefix(lines, []string{"+", " "}) - return hunk.newStart + offset + return hunk.newStart + offset, nil } 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 8b866019b..1f1eeb114 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) } |