Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sei40kr
Copy link
Contributor

@sei40kr sei40kr commented Dec 13, 2025

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() with String::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 - Use from_utf8_lossy for diff/status/show commands
  • src/git/remote.rs - Use from_utf8_lossy for branch/remote names
  • src/git/parse/status/mod.rs - Use from_utf8_lossy for filename unescaping

Test coverage:

  • src/tests/stage.rs - Added 4 tests for non-UTF8 file handling
    • non_utf8_in_unstaged_file - Display unstaged files with non-UTF8 content
    • non_utf8_in_staged_file - Display staged files with non-UTF8 content
    • non_utf8_in_modified_file - Display modified files with non-UTF8 content
    • non_utf8_stage_and_unstage - Test staging operations on non-UTF8 files
  • src/tests/log.rs - Added 1 test for non-UTF8 in commit display
    • non_utf8_in_show_commit - Display commits containing non-UTF8 content

All 302 tests pass ✅

Test plan

  1. Created a test file with non-UTF8 bytes: printf 'Text with non-UTF8: \xff\xfe\n' > test.txt
  2. Verified gitu no longer crashes and displays the file correctly
  3. Ran full test suite: cargo test --lib - all 302 tests pass

🤖 Generated with Claude Code

Copy link
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.

There's a lot of irrelevant files in this PR

altsem
altsem previously approved these changes Dec 13, 2025
Fixes altsem#458

Replace String::from_utf8() with String::from_utf8_lossy() to handle
non-UTF8 bytes in git diff, status, and show output. Invalid UTF-8
sequences are now replaced with � (replacement character) instead of
causing a crash.

Changes:
- src/git/mod.rs: Use from_utf8_lossy for diff/status/show commands
- src/git/remote.rs: Use from_utf8_lossy for branch/remote names
- src/git/parse/status/mod.rs: Use from_utf8_lossy for filename unescaping
- src/tests/mod.rs: Add test for non-UTF8 diff display

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
@altsem altsem merged commit b6da3a7 into altsem:master Dec 15, 2025
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.

gitu crash on file with ansi symbols inside

2 participants