Skip to content

fix(log): avoid panic when showing log of a branch with no commits#535

Merged
altsem merged 1 commit into
altsem:masterfrom
StressTestor:fix/log-empty-branch-panic
Jun 5, 2026
Merged

fix(log): avoid panic when showing log of a branch with no commits#535
altsem merged 1 commit into
altsem:masterfrom
StressTestor:fix/log-empty-branch-panic

Conversation

@StressTestor
Copy link
Copy Markdown
Contributor

Problem

Opening the log of a branch with no commits panics (fixes #262):

mkdir example && cd example && git init
gitu      # press `l l` to show the log of the current branch
panicked at src/screen/mod.rs:99: index out of bounds: the len is 0 but the index is 0

Root cause

The log screen for a branch with no commits has no items, so line_index is empty. But Screen::newupdate()update_cursor() still tries to position the cursor: move_from_unselectable calls nav_filter(self.cursor)at_line(0), which does &self.items[self.line_index[0]] and panics on the empty line_index.

Fix

Guard update_cursor to reset the cursor when nothing is selectable, instead of positioning it into an empty line_index. This matches the existing empty-checks the other cursor/scroll helpers already use (scroll_fit_start, scroll_fit_end, move_cursor_to_screen_line).

I kept the fix to the single chokepoint on the panic path rather than changing get_selected_item() / at_line() to return Option, since get_selected_item() has ~12 call sites across the crate — that would be a large ripple for this bug.

Test

Added log_empty_branch in src/tests/log.rs (same empty-repo setup as the existing fresh_init test, then l l). Before the fix it panics; after, it renders the empty log. The snapshot is blank because a branch with no commits has nothing to show — the test's purpose is that a regression would panic before reaching the assertion.

make test passes locally (cargo insta test --unreferenced reject, cargo clippy -- -Dwarnings, cargo fmt --check, cargo bench --no-run); full suite is green.

@StressTestor StressTestor force-pushed the fix/log-empty-branch-panic branch from 30b5386 to c7356cc Compare June 1, 2026 08:01
Opening the log of a branch with no commits panicked with "index out of
bounds: the len is 0 but the index is 0". The log screen had no items, so
`line_index` was empty, but `update_cursor` still tried to position the
cursor and indexed `line_index[0]` via `at_line`.

Guard `update_cursor` to reset the cursor when nothing is selectable,
matching the existing empty-checks in the other cursor/scroll helpers.

Closes altsem#262
@StressTestor StressTestor force-pushed the fix/log-empty-branch-panic branch from c7356cc to 9dacc27 Compare June 1, 2026 16:31
Copy link
Copy Markdown
Owner

@altsem altsem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

@altsem altsem merged commit 433ee48 into altsem:master Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic when displaying log of an empty branch

2 participants