Skip to content

Comments

fix: resolve panic in diff command when no commit history exists#62

Merged
genedna merged 3 commits intoweb3infra-foundation:mainfrom
hikarukimi:main
Nov 23, 2025
Merged

fix: resolve panic in diff command when no commit history exists#62
genedna merged 3 commits intoweb3infra-foundation:mainfrom
hikarukimi:main

Conversation

@hikarukimi
Copy link
Contributor

此 PR 完成了 r2cn 测试任务 #61 ,bug修复,commit历史为空时调用libra diff时触发panic。
修复方式为添加处理分支,针对commit历史为空的情况进行提前return

Signed-off-by: hikarukimi <759012788@qq.com>
Signed-off-by: hikarukimi <759012788@qq.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a panic in the libra diff command when invoked on a repository with no commit history (e.g., immediately after libra init). The fix adds proper handling for the None case when Head::current_commit() returns no commit, implementing an early return to avoid dereferencing a null value.

Key Changes

  • Added null-safety check for empty repository scenario in diff command
  • Added test case to verify the fix prevents panic on empty repositories
  • Cleaned up imports by removing unused status module reference

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/command/diff.rs Added match statement to handle None case from Head::current_commit(), implementing early return; cleaned up imports to only include changes_to_be_committed function
tests/command/diff_test.rs Added test_diff_after_init to verify diff command doesn't panic on empty repository
Comments suppressed due to low confidence (1)

src/command/diff.rs:102

  • The edge case handling is incomplete. When changes_to_be_committed() is NOT empty but there's no HEAD commit (empty repository with staged files), the code at line 99-101 will execute and try to get file blobs from staged changes. However, this scenario should also be handled gracefully.

Consider adding a test case that stages files in an empty repository and then runs diff to ensure this path works correctly. The current fix only handles the case where both staged changes AND HEAD are empty.

            } else {
                let changes = changes_to_be_committed().await;
                // diff didn't show untracked or deleted files
                get_files_blobs(&changes.modified, &index, IgnorePolicy::Respect)
            }

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@genedna genedna merged commit 9dcb5c0 into web3infra-foundation:main Nov 23, 2025
4 of 5 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.

2 participants