Skip to content

Commit

Permalink
Preserve gone state information of RemoteTrackingBranch
Browse files Browse the repository at this point in the history
  • Loading branch information
foriequal0 authored and mergify[bot] committed Aug 20, 2020
1 parent 91446d4 commit 3b98966
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
24 changes: 16 additions & 8 deletions src/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ impl LocalBranch {
&self,
repo: &Repository,
config: &Config,
) -> Result<Option<RemoteTrackingBranch>> {
) -> Result<RemoteTrackingBranchStatus> {
let remote_name = if let Some(remote_name) = config::get_remote_name(config, self)? {
remote_name
} else {
return Ok(None);
return Ok(RemoteTrackingBranchStatus::None);
};
let merge: String = if let Some(merge) = config::get_merge(config, self)? {
merge
} else {
return Ok(None);
return Ok(RemoteTrackingBranchStatus::None);
};

RemoteTrackingBranch::from_remote_branch(
Expand Down Expand Up @@ -99,7 +99,7 @@ impl RemoteTrackingBranch {
pub fn from_remote_branch(
repo: &Repository,
remote_branch: &RemoteBranch,
) -> Result<Option<RemoteTrackingBranch>> {
) -> Result<RemoteTrackingBranchStatus> {
let remote = config::get_remote(repo, &remote_branch.remote)?;
if let Some(remote) = remote {
let refname = if let Some(expanded) = expand_refspec(
Expand All @@ -110,16 +110,18 @@ impl RemoteTrackingBranch {
)? {
expanded
} else {
return Ok(None);
return Ok(RemoteTrackingBranchStatus::None);
};

if repo.find_reference(&refname).is_ok() {
return Ok(Some(RemoteTrackingBranch::new(&refname)));
return Ok(RemoteTrackingBranchStatus::Exists(
RemoteTrackingBranch::new(&refname),
));
} else {
return Ok(None);
return Ok(RemoteTrackingBranchStatus::Gone(refname));
}
}
Ok(None)
Ok(RemoteTrackingBranchStatus::None)
}

pub fn to_remote_branch(
Expand Down Expand Up @@ -173,6 +175,12 @@ impl<'repo> TryFrom<&git2::Reference<'repo>> for RemoteTrackingBranch {
}
}

pub enum RemoteTrackingBranchStatus {
Exists(RemoteTrackingBranch),
Gone(String),
None,
}

#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Hash, Debug)]
pub struct RemoteBranch {
pub remote: String,
Expand Down
6 changes: 4 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use git2::{BranchType, Config as GitConfig, Error, ErrorClass, ErrorCode, Remote
use log::*;

use crate::args::{Args, DeleteFilter, DeleteRange, ScanFilter, ScanRange};
use crate::branch::LocalBranch;
use crate::branch::{LocalBranch, RemoteTrackingBranchStatus};
use std::collections::HashSet;

type GitResult<T> = std::result::Result<T, git2::Error>;
Expand Down Expand Up @@ -106,7 +106,9 @@ fn get_branches_tracks_remote_heads(repo: &Repository, config: &GitConfig) -> Re
let (branch, _) = branch?;
let branch = LocalBranch::try_from(&branch)?;

if let Some(upstream) = branch.fetch_upstream(repo, config)? {
if let RemoteTrackingBranchStatus::Exists(upstream) =
branch.fetch_upstream(repo, config)?
{
if upstream.refname == refname {
result.push(branch.short_name().to_owned());
}
Expand Down
10 changes: 6 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use log::*;
use rayon::prelude::*;

use crate::args::DeleteFilter;
use crate::branch::{LocalBranch, RemoteBranch, RemoteTrackingBranch};
use crate::branch::{LocalBranch, RemoteBranch, RemoteTrackingBranch, RemoteTrackingBranchStatus};
use crate::merge_tracker::{MergeState, MergeTracker};
use crate::subprocess::{self, get_worktrees, RemoteHead};
use crate::util::ForceSendSync;
Expand Down Expand Up @@ -350,7 +350,9 @@ pub fn classify(
branch: &LocalBranch,
) -> Result<Classification> {
let local = merge_tracker.check_and_track(&git.repo, &base.refname, branch)?;
let fetch = if let Some(fetch) = branch.fetch_upstream(&git.repo, &git.config)? {
let fetch = if let RemoteTrackingBranchStatus::Exists(fetch) =
branch.fetch_upstream(&git.repo, &git.config)?
{
Some(merge_tracker.check_and_track(&git.repo, &base.refname, &fetch)?)
} else {
None
Expand Down Expand Up @@ -459,7 +461,7 @@ pub fn get_tracking_branches(
}

let fetch_upstream = branch.fetch_upstream(&git.repo, &git.config)?;
if let Some(upstream) = &fetch_upstream {
if let RemoteTrackingBranchStatus::Exists(upstream) = &fetch_upstream {
if base_upstreams.contains(&upstream) {
debug!("Skip: the branch tracks the base: {:?}", branch);
continue;
Expand Down Expand Up @@ -509,7 +511,7 @@ pub fn get_non_upstream_remote_tracking_branches(
let tracking_branches = get_tracking_branches(git, base_upstreams)?;
for tracking_branch in tracking_branches {
let upstream = tracking_branch.fetch_upstream(&git.repo, &git.config)?;
if let Some(upstream) = upstream {
if let RemoteTrackingBranchStatus::Exists(upstream) = upstream {
upstreams.insert(upstream);
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use glob::Pattern;
use log::*;

use crate::args::{DeleteFilter, ScanFilter};
use crate::branch::RemoteTrackingBranchStatus;
pub use crate::branch::{LocalBranch, RemoteBranch, RemoteBranchError, RemoteTrackingBranch};
use crate::core::{
get_non_tracking_local_branches, get_non_upstream_remote_tracking_branches, get_remote_heads,
Expand Down Expand Up @@ -225,7 +226,9 @@ fn resolve_base_and_upstream_refs(
if reference.is_branch() {
let refname = reference.name().context("non utf-8 base refname")?;
let branch = LocalBranch::new(refname);
if let Some(upstream) = branch.fetch_upstream(repo, config)? {
if let RemoteTrackingBranchStatus::Exists(upstream) =
branch.fetch_upstream(repo, config)?
{
result.insert(upstream.refname);
}
}
Expand Down Expand Up @@ -256,7 +259,9 @@ fn resolve_base_upstreams(
};

if let Ok(branch) = LocalBranch::try_from(&reference) {
if let Some(upstream) = branch.fetch_upstream(repo, config)? {
if let RemoteTrackingBranchStatus::Exists(upstream) =
branch.fetch_upstream(repo, config)?
{
result.push(upstream);
continue;
}
Expand Down Expand Up @@ -314,7 +319,9 @@ fn resolve_protected_refs(
if Pattern::new(protected_branch)?.matches(branch_name) {
let branch = LocalBranch::try_from(&branch)?;
result.insert(branch.refname.to_string());
if let Some(upstream) = branch.fetch_upstream(repo, config)? {
if let RemoteTrackingBranchStatus::Exists(upstream) =
branch.fetch_upstream(repo, config)?
{
result.insert(upstream.refname);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/subprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use anyhow::{Context, Result};
use git2::{Config, Reference, Repository};
use log::*;

use crate::branch::{LocalBranch, RemoteBranch, RemoteTrackingBranch};
use crate::branch::{LocalBranch, RemoteBranch, RemoteTrackingBranch, RemoteTrackingBranchStatus};

fn git(repo: &Repository, args: &[&str], level: log::Level) -> Result<()> {
let workdir = repo.workdir().context("Bare repository is not supported")?;
Expand Down Expand Up @@ -103,8 +103,10 @@ pub fn get_noff_merged_locals(
}
let branch = LocalBranch::new(refname);
let upstream = branch.fetch_upstream(repo, config)?;
if Some(base) == upstream.as_ref() {
continue;
if let RemoteTrackingBranchStatus::Exists(upstream) = upstream {
if base == &upstream {
continue;
}
}
let reference = repo.find_reference(&refname)?;
if reference.symbolic_target().is_some() {
Expand Down

0 comments on commit 3b98966

Please sign in to comment.