From 1be46b4fd7a78ce418765ef5467823b7ea5e60eb Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 12 Feb 2026 16:19:29 +0000 Subject: fix: handle existing local branch that is behind when checking out PR When a PR branch already exists locally, the previous code would silently move the branch pointer without checking for tracking or fast-forward safety. Now: - If branch has tracking: checkout and warn user to git pull - If no tracking and fast-forward: safely move pointer - If no tracking and diverged: show copy-paste commands for reset/rebase - If commit not found locally: suggest fetching Uses console crate for yellow output instead of hardcoded ANSI codes. --- src/lib/git/mod.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src/lib') diff --git a/src/lib/git/mod.rs b/src/lib/git/mod.rs index b9711ae..516d9e2 100644 --- a/src/lib/git/mod.rs +++ b/src/lib/git/mod.rs @@ -91,6 +91,7 @@ pub trait RepoActions { ) -> Result; fn parse_starting_commits(&self, starting_commits: &str) -> Result>; fn ancestor_of(&self, decendant: &Sha1Hash, ancestor: &Sha1Hash) -> Result; + fn get_upstream_for_branch(&self, branch_name: &str) -> Result>; fn get_git_config_item(&self, item: &str, global: Option) -> Result>; fn save_git_config_item(&self, item: &str, value: &str, global: bool) -> Result<()>; fn remove_git_config_item(&self, item: &str, global: bool) -> Result; @@ -734,6 +735,21 @@ impl RepoActions for Repo { } } + fn get_upstream_for_branch(&self, branch_name: &str) -> Result> { + let branch = self + .git_repo + .find_branch(branch_name, git2::BranchType::Local) + .context(format!("failed to find local branch {branch_name}"))?; + let upstream = branch.upstream(); + match upstream { + Ok(upstream_branch) => { + let name = upstream_branch.name()?.map(|s| s.to_string()); + Ok(name) + } + Err(_) => Ok(None), + } + } + /// setting global to None will suppliment local config with global items /// not in local fn get_git_config_item(&self, item: &str, global: Option) -> Result> { -- cgit v1.2.3