Skip to content

Commit 53e4533

Browse files
author
Stephan Dilly
committed
fix diff of newfile when cwd in subdir (fixes #25)
1 parent e4b00ad commit 53e4533

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
118118
scope_time!("get_diff");
119119

120120
let repo = utils::repo(repo_path);
121+
let repo_path = repo.path().parent().unwrap();
121122

122123
let (diff, mut opt) = get_diff_raw(&repo, &p, stage, false);
123124

@@ -168,7 +169,6 @@ pub fn get_diff(repo_path: &str, p: String, stage: bool) -> FileDiff {
168169
let delta: DiffDelta = diff.deltas().next().unwrap();
169170

170171
if delta.status() == Delta::Untracked {
171-
let repo_path = Path::new(repo_path);
172172
let newfile_path =
173173
repo_path.join(delta.new_file().path().unwrap());
174174

@@ -373,4 +373,27 @@ mod tests {
373373

374374
assert_eq!(res.hunks.len(), 2)
375375
}
376+
377+
#[test]
378+
fn test_diff_newfile_in_sub_dir_current_dir() {
379+
let file_path = Path::new("foo/foo.txt");
380+
let (_td, repo) = repo_init_empty();
381+
let root = repo.path().parent().unwrap();
382+
383+
let sub_path = root.join("foo/");
384+
385+
fs::create_dir_all(&sub_path).unwrap();
386+
File::create(&root.join(file_path))
387+
.unwrap()
388+
.write_all(b"test")
389+
.unwrap();
390+
391+
let diff = get_diff(
392+
sub_path.to_str().unwrap(),
393+
String::from(file_path.to_str().unwrap()),
394+
false,
395+
);
396+
397+
assert_eq!(diff.hunks[0].lines[1].content, "test");
398+
}
376399
}

0 commit comments

Comments
 (0)