From e7f15ae4572d80e47ae588a821abc961ad9ab88f Mon Sep 17 00:00:00 2001 From: uniqueNullptr2 <88722711+uniqueNullptr2@users.noreply.github.com> Date: Sat, 25 Dec 2021 21:37:30 +0100 Subject: [PATCH] Disable fetch,pull,push when no remote defined (#1051) --- CHANGELOG.md | 1 + src/components/branchlist.rs | 19 +++++++++++++++++-- src/tabs/status.rs | 22 ++++++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7fd21507..17ba2d10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Keep commit message when pre-commit hook fails ([#1035](https://github.com/extrawurst/gitui/issues/1035)) - honor `pushurl` when checking credentials for pushing ([#953](https://github.com/extrawurst/gitui/issues/953)) - use git-path instead of workdir finding hooks ([#1046](https://github.com/extrawurst/gitui/issues/1046)) +- only enable remote actions (fetch/pull/push) if there are remote branches ([#1047](https://github.com/extrawurst/gitui/issues/1047)) ### Key binding notes - added `gg`/`G` vim bindings to `vim_style_key_config.ron` ([#1039](https://github.com/extrawurst/gitui/issues/1039)) diff --git a/src/components/branchlist.rs b/src/components/branchlist.rs index 4da9231a..ff77062e 100644 --- a/src/components/branchlist.rs +++ b/src/components/branchlist.rs @@ -42,6 +42,7 @@ pub struct BranchListComponent { repo: RepoPathRef, branches: Vec, local: bool, + has_remotes: bool, visible: bool, selection: u16, scroll: VerticalScroll, @@ -200,7 +201,7 @@ impl Component for BranchListComponent { out.push(CommandInfo::new( strings::commands::fetch_remotes(&self.key_config), - true, + self.has_remotes, !self.local, )); } @@ -243,6 +244,7 @@ impl Component for BranchListComponent { .map(Into::into); } else if e == self.key_config.keys.tab_toggle { self.local = !self.local; + self.check_remotes(); self.update_branches()?; } else if e == self.key_config.keys.enter { try_or_popup!( @@ -297,7 +299,9 @@ impl Component for BranchListComponent { self.queue .push(InternalEvent::CompareCommits(b, None)); } - } else if e == self.key_config.keys.pull && !self.local { + } else if e == self.key_config.keys.pull + && !self.local && self.has_remotes + { self.queue.push(InternalEvent::FetchRemotes); } else if e == self.key_config.keys.cmd_bar_toggle { //do not consume if its the more key @@ -333,6 +337,7 @@ impl BranchListComponent { Self { branches: Vec::new(), local: true, + has_remotes: false, visible: false, selection: 0, scroll: VerticalScroll::new(), @@ -352,9 +357,19 @@ impl BranchListComponent { Ok(()) } + fn check_remotes(&mut self) { + if !self.local { + self.has_remotes = + get_branches_info(&self.repo.borrow(), false) + .map(|branches| !branches.is_empty()) + .unwrap_or(false); + } + } + /// fetch list of branches pub fn update_branches(&mut self) -> Result<()> { if self.is_visible() { + self.check_remotes(); self.branches = get_branches_info(&self.repo.borrow(), self.local)?; //remove remote branch called `HEAD` diff --git a/src/tabs/status.rs b/src/tabs/status.rs index 65dcd84a..df9a3516 100644 --- a/src/tabs/status.rs +++ b/src/tabs/status.rs @@ -15,7 +15,8 @@ use anyhow::Result; use asyncgit::{ cached, sync::{ - self, status::StatusType, RepoPath, RepoPathRef, RepoState, + self, get_branches_info, status::StatusType, RepoPath, + RepoPathRef, RepoState, }, sync::{BranchCompare, CommitId}, AsyncDiff, AsyncGitNotification, AsyncStatus, DiffParams, @@ -554,9 +555,17 @@ impl Status { } } + fn has_remotes(&self) -> bool { + get_branches_info(&self.repo.borrow(), false) + .map(|l| !l.is_empty()) + .unwrap_or(false) + } + fn pull(&self) { - if let Some(branch) = self.git_branch_name.last() { - self.queue.push(InternalEvent::Pull(branch)); + if self.has_remotes() { + if let Some(branch) = self.git_branch_name.last() { + self.queue.push(InternalEvent::Pull(branch)); + } } } @@ -583,6 +592,11 @@ impl Status { self.git_branch_state .as_ref() .map_or(true, |state| state.ahead > 0) + && self.has_remotes() + } + + fn can_pull(&self) -> bool { + self.has_remotes() } fn can_abort_merge(&self) -> bool { @@ -718,7 +732,7 @@ impl Component for Status { )); out.push(CommandInfo::new( strings::commands::status_pull(&self.key_config), - true, + self.can_pull(), !focus_on_diff, ));