fix: resolve panic in diff command when no commit history exists#62
Merged
genedna merged 3 commits intoweb3infra-foundation:mainfrom Nov 23, 2025
Merged
fix: resolve panic in diff command when no commit history exists#62genedna merged 3 commits intoweb3infra-foundation:mainfrom
genedna merged 3 commits intoweb3infra-foundation:mainfrom
Conversation
Signed-off-by: hikarukimi <759012788@qq.com>
Signed-off-by: hikarukimi <759012788@qq.com>
Contributor
There was a problem hiding this comment.
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
statusmodule 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
approved these changes
Nov 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
此 PR 完成了 r2cn 测试任务 #61 ,bug修复,commit历史为空时调用libra diff时触发panic。
修复方式为添加处理分支,针对commit历史为空的情况进行提前return