Skip to content

Commit 02bd22d

Browse files
author
Stephan Dilly
committed
fix crash diffing stash created on cmd line (closes #178)
1 parent 59d93d7 commit 02bd22d

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010
- switch deprecated transitive dependency `net2`->`socket2` [in `crossterm`->`mio`] ([#66](https://github.com/extrawurst/gitui/issues/66))
11+
- crash diffing stash created on command line ([#178](https://github.com/extrawurst/gitui/issues/178))
1112

1213
## [0.8.0] - 2020-07-06
1314

asyncgit/src/sync/commit_files.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ pub(crate) fn get_commit_diff(
6969
repo.path().to_str().expect("repo path utf8 err"),
7070
&id,
7171
)? {
72-
let untracked_commit = commit.parent_id(2)?;
73-
74-
let untracked_diff = get_commit_diff(
75-
repo,
76-
CommitId::new(untracked_commit),
77-
pathspec,
78-
)?;
79-
80-
diff.merge(&untracked_diff)?;
72+
if let Ok(untracked_commit) = commit.parent_id(2) {
73+
let untracked_diff = get_commit_diff(
74+
repo,
75+
CommitId::new(untracked_commit),
76+
pathspec,
77+
)?;
78+
79+
diff.merge(&untracked_diff)?;
80+
}
8181
}
8282

8383
Ok(diff)

asyncgit/src/sync/stash.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,10 @@ pub fn stash_save(
108108
mod tests {
109109
use super::*;
110110
use crate::sync::{
111-
get_commits_info,
112-
tests::{get_statuses, repo_init},
111+
commit, get_commit_files, get_commits_info, stage_add_file,
112+
tests::{debug_cmd_print, get_statuses, repo_init},
113113
};
114-
use std::{fs::File, io::Write};
114+
use std::{fs::File, io::Write, path::Path};
115115

116116
#[test]
117117
fn test_smoke() {
@@ -183,4 +183,32 @@ mod tests {
183183

184184
Ok(())
185185
}
186+
187+
#[test]
188+
fn test_stash_without_2nd_parent() -> Result<()> {
189+
let file_path1 = Path::new("file1.txt");
190+
let (_td, repo) = repo_init()?;
191+
let root = repo.path().parent().unwrap();
192+
let repo_path = root.as_os_str().to_str().unwrap();
193+
194+
File::create(&root.join(file_path1))?.write_all(b"test")?;
195+
stage_add_file(repo_path, file_path1)?;
196+
commit(repo_path, "c1")?;
197+
198+
File::create(&root.join(file_path1))?
199+
.write_all(b"modified")?;
200+
201+
//NOTE: apparently `libgit2` works differently to git stash in
202+
//always creating the third parent for untracked files while the
203+
//cli skips that step when no new files exist
204+
debug_cmd_print(repo_path, "git stash");
205+
206+
let stash = get_stashes(repo_path)?[0];
207+
208+
let diff = get_commit_files(repo_path, stash)?;
209+
210+
assert_eq!(diff.len(), 1);
211+
212+
Ok(())
213+
}
186214
}

0 commit comments

Comments
 (0)