diff --git a/pkg/commands/patch/patch.go b/pkg/commands/patch/patch.go index 55dd8f80d..049334727 100644 --- a/pkg/commands/patch/patch.go +++ b/pkg/commands/patch/patch.go @@ -149,3 +149,8 @@ func (self *Patch) LineCount() int { } return count } + +// Returns the number of hunks of the patch +func (self *Patch) HunkCount() int { + return len(self.hunks) +} diff --git a/pkg/gui/patch_exploring/state.go b/pkg/gui/patch_exploring/state.go index 356bff4f4..1c82d59cb 100644 --- a/pkg/gui/patch_exploring/state.go +++ b/pkg/gui/patch_exploring/state.go @@ -143,8 +143,13 @@ func (s *State) CycleHunk(forward bool) { } hunkIdx := s.patch.HunkContainingLine(s.selectedLineIdx) - start := s.patch.HunkStartIdx(hunkIdx + change) - s.selectedLineIdx = s.patch.GetNextChangeIdx(start) + if hunkIdx != -1 { + newHunkIdx := hunkIdx + change + if newHunkIdx >= 0 && newHunkIdx < s.patch.HunkCount() { + start := s.patch.HunkStartIdx(newHunkIdx) + s.selectedLineIdx = s.patch.GetNextChangeIdx(start) + } + } } func (s *State) CycleLine(forward bool) {