fix(asyncgit): use from_utf8_lossy when staging lines from non-UTF-8 files#2977
Open
Noethix55555 wants to merge 1 commit into
Open
fix(asyncgit): use from_utf8_lossy when staging lines from non-UTF-8 files#2977Noethix55555 wants to merge 1 commit into
Noethix55555 wants to merge 1 commit into
Conversation
…files Staging or discarding individual lines from files containing non-UTF-8 bytes (e.g. Latin-1 encoded text) would fail with a UTF-8 decode error because add_from_hunk and the indexed-blob read both used String::from_utf8()?. Switch both call-sites to String::from_utf8_lossy().into_owned(), which never fails and matches the existing behaviour in the diff viewer. Also fix trailing-newline preservation when staging lines from files that have no trailing newline (respect the no-newline-at-EOF hunk markers), and add a regression test for that case.
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.
Problem
Staging or discarding individual lines from files containing non-UTF-8 bytes
(e.g. Latin-1 encoded text files) fails with a UTF-8 decode error.
Two call-sites used
String::from_utf8(...)?:asyncgit/src/sync/staging/mod.rs(add_from_hunk) -- each diff lineasyncgit/src/sync/staging/stage_tracked.rs(stage_lines) -- the indexed blob contentThe diff viewer already uses
String::from_utf8_lossyfor rendering, so thedisplay works fine but the staging action errors out.
Fix
Replace both
String::from_utf8(...)?calls withString::from_utf8_lossy(...).into_owned(), which never fails and isconsistent with how the diff viewer handles the same bytes.
Also fix trailing-newline preservation when staging lines from files without a
trailing newline (the no-newline-at-EOF hunk markers are now respected), and
add a regression test for that case.