Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
33 changes: 31 additions & 2 deletions asyncgit/src/sync/staging/discard_tracked.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{apply_selection, load_file};
use super::{apply_selection, load_file, NEWLINE};
use crate::{
error::Result,
sync::{
Expand Down Expand Up @@ -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::<Vec<_>>();
let old_lines = working_content
.split_terminator(NEWLINE)
.collect::<Vec<_>>();

apply_selection(lines, &hunks, &old_lines, false, true)?
};
Expand Down Expand Up @@ -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);
}
}
37 changes: 35 additions & 2 deletions asyncgit/src/sync/staging/stage_tracked.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::apply_selection;
use super::{apply_selection, NEWLINE};
use crate::{
error::{Error, Result},
sync::{
Expand Down Expand Up @@ -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::<Vec<_>>();
let old_lines = indexed_content
.split_terminator(NEWLINE)
.collect::<Vec<_>>();

apply_selection(lines, &hunks, &old_lines, is_stage, false)?
};
Expand Down Expand Up @@ -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());
}
}