Skip to content

Commit

Permalink
show untracked files in stash commit details (closes #130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jul 2, 2020
1 parent e803cb3 commit a34bab2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
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
- diisplay non-utf8 commit messages at least partially ([#150](https://github.com/extrawurst/gitui/issues/150))
- hooks ignored when running `gitui` in subfolder of workdir ([#151](https://github.com/extrawurst/gitui/issues/151))
- better scrolling in file-trees [[@tisorlawan](https://github.com/tisorlawan)] ([#144](https://github.com/extrawurst/gitui/issues/144))
- show untracked files in stash commit details [[@MCord](https://github.com/MCord)] ([#130](https://github.com/extrawurst/gitui/issues/130))
- some optimizations in reflog

## [0.7.0] - 2020-06-15
Expand Down
66 changes: 41 additions & 25 deletions asyncgit/src/sync/commit_files.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{stash::is_stash_commit, utils::repo, CommitId};
use crate::{error::Result, StatusItem, StatusItemType, CWD};
use crate::{error::Result, StatusItem, StatusItemType};
use git2::{Diff, DiffDelta, DiffOptions, Repository};
use scopetime::scope_time;

Expand Down Expand Up @@ -33,22 +33,6 @@ pub fn get_commit_files(
None,
)?;

// stash commits have parent commits containing untracked files and if we want to show
// these files as if they were actually in the stash commit we have to have some specific
// handling regarding these special stash commits.
// 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
if is_stash_commit(repo_path, &id)? {
let commit = repo.find_commit(id.into())?;
let untracked_commit = commit.parent_id(2)?;

let mut untracked_files = get_commit_files(
repo_path,
CommitId::new(untracked_commit),
)?;

res.append(&mut untracked_files);
}

Ok(res)
}

Expand All @@ -75,20 +59,25 @@ pub(crate) fn get_commit_diff(
opts
});

let diff = repo.diff_tree_to_tree(
let mut diff = repo.diff_tree_to_tree(
parent.as_ref(),
Some(&commit_tree),
opt.as_mut(),
)?;

if diff.deltas().len() == 0 && is_stash_commit(CWD, &id)? {
if is_stash_commit(
repo.path().to_str().expect("repo path utf8 err"),
&id,
)? {
let untracked_commit = commit.parent_id(2)?;

return get_commit_diff(
let untracked_diff = get_commit_diff(
repo,
CommitId::new(untracked_commit),
pathspec,
);
)?;

diff.merge(&untracked_diff)?;
}

Ok(diff)
Expand All @@ -100,7 +89,8 @@ mod tests {
use crate::{
error::Result,
sync::{
commit, stage_add_file, stash_save, tests::repo_init,
commit, stage_add_file, stash_save,
tests::{get_statuses, repo_init},
},
StatusItemType,
};
Expand Down Expand Up @@ -140,14 +130,40 @@ mod tests {

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

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

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

Ok(())
}

#[test]
fn test_stashed_untracked_and_modified() -> Result<()> {
let file_path1 = Path::new("file1.txt");
let file_path2 = Path::new("file2.txt");
let (_td, repo) = repo_init()?;
let root = repo.path().parent().unwrap();
let repo_path = root.as_os_str().to_str().unwrap();

File::create(&root.join(file_path1))?.write_all(b"test")?;
stage_add_file(repo_path, file_path1)?;
commit(repo_path, "c1")?;

File::create(&root.join(file_path1))?
.write_all(b"modified")?;
File::create(&root.join(file_path2))?.write_all(b"new")?;

assert_eq!(get_statuses(repo_path), (2, 0));

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

let diff = get_commit_files(repo_path, id)?;

assert_eq!(diff.len(), 2);
assert_eq!(diff[0].status, StatusItemType::Modified);
assert_eq!(diff[1].status, StatusItemType::New);

Ok(())
}
}

0 comments on commit a34bab2

Please sign in to comment.