Skip to content

fix(asyncgit): use from_utf8_lossy when staging lines from non-UTF-8 files#2977

Open
Noethix55555 wants to merge 1 commit into
gitui-org:masterfrom
Noethix55555:fix/staging-non-utf8
Open

fix(asyncgit): use from_utf8_lossy when staging lines from non-UTF-8 files#2977
Noethix55555 wants to merge 1 commit into
gitui-org:masterfrom
Noethix55555:fix/staging-non-utf8

Conversation

@Noethix55555

Copy link
Copy Markdown

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 line
  • asyncgit/src/sync/staging/stage_tracked.rs (stage_lines) -- the indexed blob content

The diff viewer already uses String::from_utf8_lossy for rendering, so the
display works fine but the staging action errors out.

Fix

Replace both String::from_utf8(...)? calls with
String::from_utf8_lossy(...).into_owned(), which never fails and is
consistent 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.

…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.
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.

1 participant