Skip to content

Commit bec1fb9

Browse files
author
Stephan Dilly
committed
show error instead of app close when staging fails (#108)
update changelog
1 parent 415d511 commit bec1fb9

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- `arrow-up`/`down` on bottom/top of status file list switches focus ([#105](https://github.com/extrawurst/gitui/issues/105))
1212
- New `Stage all [a]`/`Unstage all [a]` in changes lists ([#82](https://github.com/extrawurst/gitui/issues/82))
1313

14+
### Fixed
15+
- app closes when staging invalid file/path ([#108](https://github.com/extrawurst/gitui/issues/108))
16+
1417
## [0.5.0] - 2020-06-01
1518

1619
### Changed

asyncgit/src/sync/utils.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ mod tests {
130130
use super::*;
131131
use crate::sync::{
132132
status::{get_status, StatusType},
133-
tests::{get_statuses, repo_init, repo_init_empty},
133+
tests::{
134+
debug_cmd_print, get_statuses, repo_init, repo_init_empty,
135+
},
134136
};
135137
use std::{
136138
fs::{self, remove_file, File},
@@ -284,4 +286,34 @@ mod tests {
284286
assert_eq!(status_count(StatusType::WorkingDir), 0);
285287
assert_eq!(status_count(StatusType::Stage), 1);
286288
}
289+
290+
// see https://github.com/extrawurst/gitui/issues/108
291+
#[test]
292+
fn test_staging_sub_git_folder() -> Result<()> {
293+
let (_td, repo) = repo_init().unwrap();
294+
let root = repo.path().parent().unwrap();
295+
let repo_path = root.as_os_str().to_str().unwrap();
296+
297+
let status_count = |s: StatusType| -> usize {
298+
get_status(repo_path, s).unwrap().len()
299+
};
300+
301+
let sub = &root.join("sub");
302+
303+
fs::create_dir_all(sub)?;
304+
305+
debug_cmd_print(sub.to_str().unwrap(), "git init subgit");
306+
307+
File::create(sub.join("subgit/foo.txt"))
308+
.unwrap()
309+
.write_all(b"content")
310+
.unwrap();
311+
312+
assert_eq!(status_count(StatusType::WorkingDir), 1);
313+
314+
//expect to fail
315+
assert!(stage_add_all(repo_path, "sub").is_err());
316+
317+
Ok(())
318+
}
287319
}

src/components/changes.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ use std::path::Path;
1717
use strings::commands;
1818
use tui::{backend::Backend, layout::Rect, Frame};
1919

20+
/// macro to simplify running code that might return Err.
21+
/// It will show a popup in that case
22+
#[macro_export]
23+
macro_rules! try_or_popup {
24+
($self:ident, $msg:literal, $e:expr) => {
25+
if let Err(err) = $e {
26+
$self.queue.borrow_mut().push_back(
27+
InternalEvent::ShowErrorMsg(format!(
28+
"{}\n{}",
29+
$msg, err
30+
)),
31+
);
32+
}
33+
};
34+
}
35+
2036
///
2137
pub struct ChangesComponent {
2238
files: FileTreeComponent,
@@ -241,19 +257,26 @@ impl Component for ChangesComponent {
241257
Ok(true)
242258
}
243259
keys::STATUS_STAGE_FILE => {
244-
if self.index_add_remove()? {
245-
self.queue.borrow_mut().push_back(
246-
InternalEvent::Update(
247-
NeedsUpdate::ALL,
248-
),
249-
);
250-
}
260+
try_or_popup!(
261+
self,
262+
"staging error:",
263+
self.index_add_remove()
264+
);
265+
266+
self.queue.borrow_mut().push_back(
267+
InternalEvent::Update(NeedsUpdate::ALL),
268+
);
269+
251270
Ok(true)
252271
}
253272

254273
keys::STATUS_STAGE_ALL if !self.is_empty() => {
255274
if self.is_working_dir {
256-
self.index_add_all()?;
275+
try_or_popup!(
276+
self,
277+
"staging error:",
278+
self.index_add_all()
279+
);
257280
} else {
258281
self.stage_remove_all()?;
259282
}

0 commit comments

Comments
 (0)