diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ca0eaac2..b2294f37f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895)) * when staging the last file in a directory, the first item after the directory is no longer skipped [[@Tillerino](https://github.com/Tillerino)] ([#2748](https://github.com/gitui-org/gitui/issues/2748)) * index-out-of-bounds panic when unstaging lines near the end of a diff ([#2953](https://github.com/gitui-org/gitui/issues/2953)) +* staging or discarding single lines no longer converts CRLF line endings in the rest of the file to LF ## [0.28.1] - 2026-03-21 diff --git a/asyncgit/src/sync/staging/discard_tracked.rs b/asyncgit/src/sync/staging/discard_tracked.rs index 83b0646b5c..bb8f5c8e5c 100644 --- a/asyncgit/src/sync/staging/discard_tracked.rs +++ b/asyncgit/src/sync/staging/discard_tracked.rs @@ -1,4 +1,4 @@ -use super::{apply_selection, load_file}; +use super::{apply_selection, load_file, NEWLINE}; use crate::{ error::Result, sync::{ @@ -32,7 +32,9 @@ pub fn discard_lines( let hunks = patch_get_hunklines(&patch)?; let working_content = load_file(&repo, file_path)?; - let old_lines = working_content.lines().collect::>(); + let old_lines = working_content + .split_terminator(NEWLINE) + .collect::>(); apply_selection(lines, &hunks, &old_lines, false, true)? }; @@ -343,4 +345,31 @@ end assert_eq!(result_file.as_str(), FILE_3); } + + #[test] + fn test_discard_keeps_crlf() { + static FILE_1: &str = "a\r\nb\r\nc\r\n"; + static FILE_2: &str = "a\r\nb\r\nc\r\nX\r\n"; + + let (path, repo) = repo_init().unwrap(); + let path: &RepoPath = &path.path().to_str().unwrap().into(); + + write_commit_file(&repo, "test.txt", FILE_1, "c1"); + + repo_write_file(&repo, "test.txt", FILE_2).unwrap(); + + discard_lines( + path, + "test.txt", + &[DiffLinePosition { + old_lineno: None, + new_lineno: Some(4), + }], + ) + .unwrap(); + + let result_file = load_file(&repo, "test.txt").unwrap(); + + assert_eq!(result_file.as_str(), FILE_1); + } } diff --git a/asyncgit/src/sync/staging/stage_tracked.rs b/asyncgit/src/sync/staging/stage_tracked.rs index 891f61bef1..c5be040f72 100644 --- a/asyncgit/src/sync/staging/stage_tracked.rs +++ b/asyncgit/src/sync/staging/stage_tracked.rs @@ -1,4 +1,4 @@ -use super::apply_selection; +use super::{apply_selection, NEWLINE}; use crate::{ error::{Error, Result}, sync::{ @@ -42,7 +42,9 @@ pub fn stage_lines( get_file_diff_patch(&repo, file_path, is_stage, false)?; let hunks = patch_get_hunklines(&patch)?; - let old_lines = indexed_content.lines().collect::>(); + let old_lines = indexed_content + .split_terminator(NEWLINE) + .collect::>(); apply_selection(lines, &hunks, &old_lines, is_stage, false)? }; @@ -188,4 +190,35 @@ c = 4"; assert_eq!(diff.lines, 4); } + + #[test] + fn test_stage_keeps_crlf() { + static FILE_1: &str = "a\r\nb\r\nc\r\n"; + static FILE_2: &str = "a\r\nb\r\nc\r\nX\r\n"; + + let (path, repo) = repo_init().unwrap(); + let path: &RepoPath = &path.path().to_str().unwrap().into(); + + write_commit_file(&repo, "test.txt", FILE_1, "c1"); + + repo_write_file(&repo, "test.txt", FILE_2).unwrap(); + + stage_lines( + path, + "test.txt", + false, + &[DiffLinePosition { + old_lineno: None, + new_lineno: Some(4), + }], + ) + .unwrap(); + + let mut index = repo.index().unwrap(); + index.read(true).unwrap(); + let entry = index.get_path(Path::new("test.txt"), 0).unwrap(); + let blob = repo.find_blob(entry.id).unwrap(); + + assert_eq!(blob.content(), FILE_2.as_bytes()); + } }