fix: handle non-UTF8 bytes in git command output #477
Merged
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.
Summary
Fixes #458
This PR fixes a crash that occurs when gitu encounters files with non-UTF8 bytes (e.g., ANSI escape codes or other binary content).
Changes
Replace
String::from_utf8()withString::from_utf8_lossy()throughout the codebase to gracefully handle invalid UTF-8 sequences. Invalid bytes are now replaced with � (replacement character) instead of causing a panic.Modified files:
src/git/mod.rs- Usefrom_utf8_lossyfor diff/status/show commandssrc/git/remote.rs- Usefrom_utf8_lossyfor branch/remote namessrc/git/parse/status/mod.rs- Usefrom_utf8_lossyfor filename unescapingTest coverage:
src/tests/stage.rs- Added 4 tests for non-UTF8 file handlingnon_utf8_in_unstaged_file- Display unstaged files with non-UTF8 contentnon_utf8_in_staged_file- Display staged files with non-UTF8 contentnon_utf8_in_modified_file- Display modified files with non-UTF8 contentnon_utf8_stage_and_unstage- Test staging operations on non-UTF8 filessrc/tests/log.rs- Added 1 test for non-UTF8 in commit displaynon_utf8_in_show_commit- Display commits containing non-UTF8 contentAll 302 tests pass ✅
Test plan
printf 'Text with non-UTF8: \xff\xfe\n' > test.txtcargo test --lib- all 302 tests pass🤖 Generated with Claude Code