fix panic on staging lines involving missing newline eof

This commit is contained in:
Stephan Dilly 2021-04-03 13:08:09 +02:00
parent eae99eadc3
commit 2596c8b1e3
2 changed files with 46 additions and 1 deletions

View file

@ -121,7 +121,9 @@ pub(crate) fn apply_selection(
.trim()
);
if hunk_line.origin() == '<' {
if hunk_line.origin() == '<'
|| hunk_line.origin() == '>'
{
break;
}

View file

@ -107,6 +107,49 @@ mod test {
);
}
#[test]
fn test_panic_stage_no_newline() {
static FILE_1: &str = r"a = 1
b = 2";
static FILE_2: &str = r"a = 2
b = 3
c = 4";
let (path, repo) = repo_init().unwrap();
let path = path.path().to_str().unwrap();
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: Some(1),
new_lineno: None,
},
DiffLinePosition {
old_lineno: Some(2),
new_lineno: None,
},
],
)
.unwrap();
let diff =
get_diff(path, String::from("test.txt"), true).unwrap();
assert_eq!(diff.lines, 5);
assert_eq!(
diff.hunks[0].lines[0].content,
String::from("@@ -1,2 +1 @@\n")
);
}
#[test]
fn test_unstage() {
static FILE_1: &str = r"0