diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2023-02-24 21:42:27 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2023-02-25 11:35:41 +1100 |
commit | db011d8e34b9fef0bd01badc4b856ec401831a27 (patch) | |
tree | 77ca03c95c2cf08953fc77f50808d5e61e46d2ac /pkg/commands | |
parent | 752526c8807550173b1a2d994aa845e51315ee76 (diff) |
Improve staging panel integration tests
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/patch/patch_modifier.go | 8 | ||||
-rw-r--r-- | pkg/commands/patch/patch_parser.go | 4 |
2 files changed, 10 insertions, 2 deletions
diff --git a/pkg/commands/patch/patch_modifier.go b/pkg/commands/patch/patch_modifier.go index 2d060ec18..fe0a896b1 100644 --- a/pkg/commands/patch/patch_modifier.go +++ b/pkg/commands/patch/patch_modifier.go @@ -27,7 +27,9 @@ func GetHunksFromDiff(diff string) []*PatchHunk { var hunkLines []string //nolint:prealloc pastDiffHeader := false - for lineIdx, line := range strings.SplitAfter(diff, "\n") { + lines := strings.SplitAfter(diff, "\n") + + for lineIdx, line := range lines { isHunkHeader := strings.HasPrefix(line, "@@ -") if isHunkHeader { @@ -44,6 +46,10 @@ func GetHunksFromDiff(diff string) []*PatchHunk { continue } + if lineIdx == len(lines)-1 && line == "" { // skip the trailing newline + continue + } + hunkLines = append(hunkLines, line) } diff --git a/pkg/commands/patch/patch_parser.go b/pkg/commands/patch/patch_parser.go index 1fd3c107a..3f191f40a 100644 --- a/pkg/commands/patch/patch_parser.go +++ b/pkg/commands/patch/patch_parser.go @@ -134,7 +134,8 @@ func coloredString(textStyle style.TextStyle, str string, selected bool, include } func parsePatch(patch string) ([]int, []int, []*PatchLine) { - lines := strings.Split(patch, "\n") + // ignore trailing newline. + lines := strings.Split(strings.TrimSuffix(patch, "\n"), "\n") hunkStarts := []int{} stageableLines := []int{} pastFirstHunkHeader := false @@ -179,6 +180,7 @@ func parsePatch(patch string) ([]int, []int, []*PatchLine) { } patchLines[index] = &PatchLine{Kind: lineKind, Content: line} } + return hunkStarts, stageableLines, patchLines } |