Skip to content

Commit f33e96f

Browse files
kpbaksextrawurst
authored andcommitted
fix: disable blame and history popup for untracked files (#2489)
* fix: disable blame and history popup for untracked files An untracked file does not have any history data. Right now when you press `B` for the blame popup or the `H` for the history popup you get an empty popup where the title spins endlessly trying to find the file in the commit history, and show relevant information. This commit disables the two actions in the `StatusTreeComponent`, when the selected item is a file which is not tracked by git. --------- Co-authored-by: extrawurst <[email protected]>
1 parent 453c474 commit f33e96f

File tree

2 files changed

+60
-28
lines changed

2 files changed

+60
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
* yanking commit ranges no longer generates incorrect dotted range notations, but lists each individual commit [[@naseschwarz](https://github.com/naseschwarz)] (https://github.com/gitui-org/gitui/issues/2576)
3636
* print slightly nicer errors when failing to create a directory [[@linkmauve](https://github.com/linkmauve)] (https://github.com/gitui-org/gitui/pull/2728)
3737
* When the terminal is insufficient to display all the commands, the cmdbar_bg configuration color does not fully take effect. ([#2347](https://github.com/extrawurst/gitui/issues/2347))
38+
* disable blame and history popup keybinds for untracked files [[@kpbaks](https://github.com/kpbaks)] ([#2489](https://github.com/gitui-org/gitui/pull/2489))
3839

3940
## [0.27.0] - 2024-01-14
4041

src/components/status_tree.rs

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,25 @@ impl StatusTreeComponent {
308308
}
309309
}
310310
}
311+
312+
fn open_history(&mut self) {
313+
match self.selection_file() {
314+
Some(status_item)
315+
if !matches!(
316+
status_item.status,
317+
StatusItemType::New
318+
) =>
319+
{
320+
self.hide();
321+
self.queue.push(InternalEvent::OpenPopup(
322+
StackablePopupOpen::FileRevlog(FileRevOpen::new(
323+
status_item.path,
324+
)),
325+
));
326+
}
327+
_ => {}
328+
}
329+
}
311330
}
312331

313332
/// Used for drawing the `FileTreeComponent`
@@ -395,20 +414,27 @@ impl Component for StatusTreeComponent {
395414
out: &mut Vec<CommandInfo>,
396415
force_all: bool,
397416
) -> CommandBlocking {
417+
let available = self.focused || force_all;
418+
let selection = self.selection_file();
419+
let selected_is_file = selection.is_some();
420+
let tracked = selection.is_some_and(|s| {
421+
!matches!(s.status, StatusItemType::New)
422+
});
423+
398424
out.push(
399425
CommandInfo::new(
400426
strings::commands::navigate_tree(&self.key_config),
401427
!self.is_empty(),
402-
self.focused || force_all,
428+
available,
403429
)
404430
.order(order::NAV),
405431
);
406432

407433
out.push(
408434
CommandInfo::new(
409435
strings::commands::blame_file(&self.key_config),
410-
self.selection_file().is_some(),
411-
self.focused || force_all,
436+
selected_is_file && tracked,
437+
available,
412438
)
413439
.order(order::RARE_ACTION),
414440
);
@@ -418,62 +444,67 @@ impl Component for StatusTreeComponent {
418444
strings::commands::open_file_history(
419445
&self.key_config,
420446
),
421-
self.selection_file().is_some(),
422-
self.focused || force_all,
447+
selected_is_file && tracked,
448+
available,
423449
)
424450
.order(order::RARE_ACTION),
425451
);
426452

427453
out.push(
428454
CommandInfo::new(
429455
strings::commands::edit_item(&self.key_config),
430-
self.selection_file().is_some(),
431-
self.focused || force_all,
456+
selected_is_file,
457+
available,
432458
)
433459
.order(order::RARE_ACTION),
434460
);
435461

436462
out.push(
437463
CommandInfo::new(
438464
strings::commands::copy_path(&self.key_config),
439-
self.selection_file().is_some(),
440-
self.focused || force_all,
465+
selected_is_file,
466+
available,
441467
)
442468
.order(order::RARE_ACTION),
443469
);
444470

445471
CommandBlocking::PassingOn
446472
}
447473

474+
#[expect(clippy::cognitive_complexity)]
448475
fn event(&mut self, ev: &Event) -> Result<EventState> {
449476
if self.focused {
450477
if let Event::Key(e) = ev {
451478
return if key_match(e, self.key_config.keys.blame) {
452-
if let Some(status_item) = self.selection_file() {
453-
self.hide();
454-
self.queue.push(InternalEvent::OpenPopup(
455-
StackablePopupOpen::BlameFile(
456-
BlameFileOpen {
457-
file_path: status_item.path,
458-
commit_id: self.revision,
459-
selection: None,
460-
},
461-
),
462-
));
479+
match self.selection_file() {
480+
Some(status_item)
481+
if !matches!(
482+
status_item.status,
483+
StatusItemType::New
484+
) =>
485+
{
486+
self.hide();
487+
self.queue.push(
488+
InternalEvent::OpenPopup(
489+
StackablePopupOpen::BlameFile(
490+
BlameFileOpen {
491+
file_path: status_item
492+
.path,
493+
commit_id: self.revision,
494+
selection: None,
495+
},
496+
),
497+
),
498+
);
499+
}
500+
_ => {}
463501
}
464502
Ok(EventState::Consumed)
465503
} else if key_match(
466504
e,
467505
self.key_config.keys.file_history,
468506
) {
469-
if let Some(status_item) = self.selection_file() {
470-
self.hide();
471-
self.queue.push(InternalEvent::OpenPopup(
472-
StackablePopupOpen::FileRevlog(
473-
FileRevOpen::new(status_item.path),
474-
),
475-
));
476-
}
507+
self.open_history();
477508
Ok(EventState::Consumed)
478509
} else if key_match(e, self.key_config.keys.edit_file)
479510
{

0 commit comments

Comments
 (0)