You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use git2::{Diff,DiffDelta,DiffOptions,Repository};
4
4
use scopetime::scope_time;
5
5
@@ -33,6 +33,22 @@ pub fn get_commit_files(
33
33
None,
34
34
)?;
35
35
36
+
// stash commits have parent commits containing untracked files and if we want to show
37
+
// these files as if they were actually in the stash commit we have to have some specific
38
+
// handling regarding these special stash commits.
39
+
// more info can be found at https://stackoverflow.com/questions/51275777/why-does-git-stash-pop-say-that-it-could-not-restore-untracked-files-from-stash/51276389#51276389
40
+
ifis_stash_commit(repo_path,&id)? {
41
+
let commit = repo.find_commit(id.into())?;
42
+
let untracked_commit = commit.parent_id(2)?;
43
+
44
+
letmut untracked_files = get_commit_files(
45
+
repo_path,
46
+
CommitId::new(untracked_commit),
47
+
)?;
48
+
49
+
res.append(&mut untracked_files);
50
+
}
51
+
36
52
Ok(res)
37
53
}
38
54
@@ -52,7 +68,7 @@ pub(crate) fn get_commit_diff(
52
68
None
53
69
};
54
70
55
-
letmut opt = pathspec.map(|p| {
71
+
letmut opt = pathspec.clone().map(|p| {
56
72
letmut opts = DiffOptions::new();
57
73
opts.pathspec(p);
58
74
opts.show_binary(true);
@@ -65,6 +81,16 @@ pub(crate) fn get_commit_diff(
65
81
opt.as_mut(),
66
82
)?;
67
83
84
+
if diff.deltas().len() == 0 && is_stash_commit(CWD,&id)? {
85
+
let untracked_commit = commit.parent_id(2)?;
86
+
87
+
returnget_commit_diff(
88
+
repo,
89
+
CommitId::new(untracked_commit),
90
+
(&pathspec).clone(),
91
+
);
92
+
}
93
+
68
94
Ok(diff)
69
95
}
70
96
@@ -81,31 +107,31 @@ mod tests {
81
107
use std::{fs::File, io::Write, path::Path};
82
108
83
109
#[test]
84
-
fntest_smoke(){
110
+
fntest_smoke()-> Result<()>{
85
111
let file_path = Path::new("file1.txt");
86
-
let(_td, repo) = repo_init().unwrap();
112
+
let(_td, repo) = repo_init()?;
87
113
let root = repo.path().parent().unwrap();
88
114
let repo_path = root.as_os_str().to_str().unwrap();
89
115
90
-
File::create(&root.join(file_path))
91
-
.unwrap()
92
-
.write_all(b"test file1 content")
93
-
.unwrap();
116
+
File::create(&root.join(file_path))?
117
+
.write_all(b"test file1 content")?;
94
118
95
-
stage_add_file(repo_path, file_path).unwrap();
119
+
stage_add_file(repo_path, file_path)?;
96
120
97
-
let id = commit(repo_path,"commit msg").unwrap();
121
+
let id = commit(repo_path,"commit msg")?;
98
122
99
-
let diff = get_commit_files(repo_path, id).unwrap();
123
+
let diff = get_commit_files(repo_path, id)?;
100
124
101
125
assert_eq!(diff.len(),1);
102
126
assert_eq!(diff[0].status,StatusItemType::New);
127
+
128
+
Ok(())
103
129
}
104
130
105
131
#[test]
106
132
fntest_stashed_untracked() -> Result<()>{
107
133
let file_path = Path::new("file1.txt");
108
-
let(_td, repo) = repo_init().unwrap();
134
+
let(_td, repo) = repo_init()?;
109
135
let root = repo.path().parent().unwrap();
110
136
let repo_path = root.as_os_str().to_str().unwrap();
0 commit comments