Skip to content

Commit a34bab2

Browse files
author
Stephan Dilly
committed
show untracked files in stash commit details (closes #130)
1 parent e803cb3 commit a34bab2

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- diisplay non-utf8 commit messages at least partially ([#150](https://github.com/extrawurst/gitui/issues/150))
2121
- hooks ignored when running `gitui` in subfolder of workdir ([#151](https://github.com/extrawurst/gitui/issues/151))
2222
- better scrolling in file-trees [[@tisorlawan](https://github.com/tisorlawan)] ([#144](https://github.com/extrawurst/gitui/issues/144))
23+
- show untracked files in stash commit details [[@MCord](https://github.com/MCord)] ([#130](https://github.com/extrawurst/gitui/issues/130))
2324
- some optimizations in reflog
2425

2526
## [0.7.0] - 2020-06-15

Diff for: asyncgit/src/sync/commit_files.rs

+41-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{stash::is_stash_commit, utils::repo, CommitId};
2-
use crate::{error::Result, StatusItem, StatusItemType, CWD};
2+
use crate::{error::Result, StatusItem, StatusItemType};
33
use git2::{Diff, DiffDelta, DiffOptions, Repository};
44
use scopetime::scope_time;
55

@@ -33,22 +33,6 @@ pub fn get_commit_files(
3333
None,
3434
)?;
3535

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-
if is_stash_commit(repo_path, &id)? {
41-
let commit = repo.find_commit(id.into())?;
42-
let untracked_commit = commit.parent_id(2)?;
43-
44-
let mut untracked_files = get_commit_files(
45-
repo_path,
46-
CommitId::new(untracked_commit),
47-
)?;
48-
49-
res.append(&mut untracked_files);
50-
}
51-
5236
Ok(res)
5337
}
5438

@@ -75,20 +59,25 @@ pub(crate) fn get_commit_diff(
7559
opts
7660
});
7761

78-
let diff = repo.diff_tree_to_tree(
62+
let mut diff = repo.diff_tree_to_tree(
7963
parent.as_ref(),
8064
Some(&commit_tree),
8165
opt.as_mut(),
8266
)?;
8367

84-
if diff.deltas().len() == 0 && is_stash_commit(CWD, &id)? {
68+
if is_stash_commit(
69+
repo.path().to_str().expect("repo path utf8 err"),
70+
&id,
71+
)? {
8572
let untracked_commit = commit.parent_id(2)?;
8673

87-
return get_commit_diff(
74+
let untracked_diff = get_commit_diff(
8875
repo,
8976
CommitId::new(untracked_commit),
9077
pathspec,
91-
);
78+
)?;
79+
80+
diff.merge(&untracked_diff)?;
9281
}
9382

9483
Ok(diff)
@@ -100,7 +89,8 @@ mod tests {
10089
use crate::{
10190
error::Result,
10291
sync::{
103-
commit, stage_add_file, stash_save, tests::repo_init,
92+
commit, stage_add_file, stash_save,
93+
tests::{get_statuses, repo_init},
10494
},
10595
StatusItemType,
10696
};
@@ -140,14 +130,40 @@ mod tests {
140130

141131
let id = stash_save(repo_path, None, true, false)?;
142132

143-
//TODO: https://github.com/extrawurst/gitui/issues/130
144-
// `get_commit_diff` actually needs to merge the regular diff
145-
// and a third parent diff containing the untracked files
146133
let diff = get_commit_files(repo_path, id)?;
147134

148135
assert_eq!(diff.len(), 1);
149136
assert_eq!(diff[0].status, StatusItemType::New);
150137

151138
Ok(())
152139
}
140+
141+
#[test]
142+
fn test_stashed_untracked_and_modified() -> Result<()> {
143+
let file_path1 = Path::new("file1.txt");
144+
let file_path2 = Path::new("file2.txt");
145+
let (_td, repo) = repo_init()?;
146+
let root = repo.path().parent().unwrap();
147+
let repo_path = root.as_os_str().to_str().unwrap();
148+
149+
File::create(&root.join(file_path1))?.write_all(b"test")?;
150+
stage_add_file(repo_path, file_path1)?;
151+
commit(repo_path, "c1")?;
152+
153+
File::create(&root.join(file_path1))?
154+
.write_all(b"modified")?;
155+
File::create(&root.join(file_path2))?.write_all(b"new")?;
156+
157+
assert_eq!(get_statuses(repo_path), (2, 0));
158+
159+
let id = stash_save(repo_path, None, true, false)?;
160+
161+
let diff = get_commit_files(repo_path, id)?;
162+
163+
assert_eq!(diff.len(), 2);
164+
assert_eq!(diff[0].status, StatusItemType::Modified);
165+
assert_eq!(diff[1].status, StatusItemType::New);
166+
167+
Ok(())
168+
}
153169
}

0 commit comments

Comments
 (0)