use serde::{Deserialize, Serialize};
use super::remote_group::{RemoteRelation, TargetRef, TargetRole};
use super::Oid;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GroupMode {
#[default]
Push,
Pull,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GroupAction {
Push,
FastForwardFrom,
MergeFrom,
RebaseOnto,
ForceWithLease,
Skip,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemoteClass {
pub relation: RemoteRelation,
pub rewritten: bool,
}
impl RemoteClass {
pub fn holds_local(&self, local_unmatched: Option<u32>) -> bool {
match self.relation {
RemoteRelation::UpToDate | RemoteRelation::RemoteAhead(_) => true,
RemoteRelation::Diverged { .. } => local_unmatched == Some(0),
_ => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RelationFacts {
pub ahead_behind: Option<(u32, u32)>,
pub related: bool,
pub remote_unmatched: Option<u32>,
pub remote_tip_in_reflog: bool,
}
pub fn classify(f: &RelationFacts) -> RemoteClass {
let (relation, rewritten) = match f.ahead_behind {
None => (RemoteRelation::Absent, false),
Some((0, 0)) => (RemoteRelation::UpToDate, false),
Some((a, 0)) => (RemoteRelation::LocalAhead(a), false),
Some((0, b)) => (RemoteRelation::RemoteAhead(b), false),
Some((ahead, behind)) => {
if !f.related {
(RemoteRelation::Unrelated, false)
} else {
(
RemoteRelation::Diverged { ahead, behind },
f.remote_unmatched == Some(0) || f.remote_tip_in_reflog,
)
}
}
};
RemoteClass {
relation,
rewritten,
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SuggestContext {
pub mode: GroupMode,
pub prefer_rebase: bool,
pub others_already_have_local: bool,
pub refused: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Suggestion {
pub action: GroupAction,
pub allowed: Vec<GroupAction>,
}
fn pick(action: GroupAction, rest: &[GroupAction]) -> Suggestion {
let mut allowed = vec![action];
allowed.extend(rest.iter().copied().filter(|a| *a != action));
Suggestion { action, allowed }
}
pub fn suggest(class: &RemoteClass, ctx: &SuggestContext) -> Suggestion {
use GroupAction as A;
if ctx.refused {
return pick(A::Skip, &[]);
}
match ctx.mode {
GroupMode::Push => match class.relation {
RemoteRelation::UpToDate | RemoteRelation::LocalAhead(_) | RemoteRelation::Absent => {
pick(A::Push, &[A::Skip])
}
RemoteRelation::RemoteAhead(_) => pick(A::FastForwardFrom, &[A::Skip]),
RemoteRelation::Diverged { .. } if class.rewritten => {
pick(A::ForceWithLease, &[A::MergeFrom, A::RebaseOnto, A::Skip])
}
RemoteRelation::Diverged { .. } => {
let first = if ctx.prefer_rebase && !ctx.others_already_have_local {
A::RebaseOnto
} else {
A::MergeFrom
};
pick(
first,
&[A::MergeFrom, A::RebaseOnto, A::ForceWithLease, A::Skip],
)
}
RemoteRelation::Unrelated => pick(A::Skip, &[A::ForceWithLease]),
},
GroupMode::Pull => match class.relation {
RemoteRelation::UpToDate
| RemoteRelation::LocalAhead(_)
| RemoteRelation::Absent
| RemoteRelation::Unrelated => pick(A::Skip, &[]),
RemoteRelation::RemoteAhead(_) => pick(A::FastForwardFrom, &[A::Skip]),
RemoteRelation::Diverged { .. } => {
let first = if ctx.prefer_rebase {
A::RebaseOnto
} else {
A::MergeFrom
};
pick(first, &[A::MergeFrom, A::RebaseOnto, A::Skip])
}
},
}
}
pub fn normalize_url(url: &str) -> String {
let url = url.trim().replace('\\', "/");
let (scheme, rest) = match url.split_once("://") {
Some((s, r)) => (Some(s.to_ascii_lowercase()), r.to_string()),
None => (None, url.clone()),
};
let local = scheme.as_deref() == Some("file") || (scheme.is_none() && !looks_scp(&rest));
let mut out = if local {
if scheme.is_some() && !rest.starts_with('/') {
format!("/{rest}")
} else {
rest
}
} else {
let (authority, path) = if scheme.is_none() {
let (a, p) = rest.split_once(':').unwrap_or((&rest, ""));
(a.to_string(), p.trim_start_matches('/').to_string())
} else {
let (a, p) = rest.split_once('/').unwrap_or((&rest, ""));
(a.to_string(), p.to_string())
};
let host = authority.rsplit('@').next().unwrap_or(&authority);
let host = if scheme.is_some() {
host.split(':').next().unwrap_or(host)
} else {
host
};
format!("{}/{path}", host.to_ascii_lowercase())
};
while out.ends_with('/') {
out.pop();
}
if let Some(s) = out.strip_suffix(".git") {
out = s.to_string();
}
out
}
fn looks_scp(s: &str) -> bool {
if s.starts_with('/') {
return false;
}
match s.split_once(':') {
Some((host, _)) => host.len() > 1 && !host.contains('/'),
None => false,
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlannedTarget {
pub remote: String,
pub branch: String,
pub role: TargetRole,
pub relation: RemoteRelation,
#[serde(default)]
pub rewritten: bool,
pub ahead: u32,
pub behind: u32,
#[serde(default)]
pub local_unmatched: Option<u32>,
#[serde(default)]
pub remote_unmatched: Option<u32>,
pub suggested: GroupAction,
pub allowed: Vec<GroupAction>,
#[serde(default)]
pub lease: Option<Oid>,
#[serde(default)]
pub conflicts: Vec<String>,
#[serde(default)]
pub same_url_as: Option<String>,
#[serde(default)]
pub checked: bool,
#[serde(default)]
pub fetched: bool,
#[serde(default)]
pub error: Option<String>,
#[serde(default)]
pub missing: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemoteGroupPlan {
pub branch: String,
pub mode: GroupMode,
pub local: Oid,
pub preflight: bool,
pub targets: Vec<PlannedTarget>,
}
fn default_true() -> bool {
true
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RemoteGroupPlanRequest {
pub branch: String,
#[serde(default)]
pub mode: GroupMode,
#[serde(default)]
pub targets: Option<Vec<TargetRef>>,
#[serde(default = "default_true")]
pub preflight: bool,
#[serde(default)]
pub prefer_rebase: bool,
#[serde(default)]
pub refused: Vec<TargetRef>,
}
#[cfg(test)]
mod tests {
use super::*;
use GroupAction as A;
fn facts(ab: Option<(u32, u32)>) -> RelationFacts {
RelationFacts {
ahead_behind: ab,
related: true,
remote_unmatched: None,
remote_tip_in_reflog: false,
}
}
fn class(f: RelationFacts) -> RemoteClass {
classify(&f)
}
fn ctx() -> SuggestContext {
SuggestContext::default()
}
#[test]
fn classification_covers_every_relation() {
assert_eq!(class(facts(None)).relation, RemoteRelation::Absent);
assert_eq!(
class(facts(Some((0, 0)))).relation,
RemoteRelation::UpToDate
);
assert_eq!(
class(facts(Some((2, 0)))).relation,
RemoteRelation::LocalAhead(2)
);
assert_eq!(
class(facts(Some((0, 3)))).relation,
RemoteRelation::RemoteAhead(3)
);
let d = class(facts(Some((2, 1))));
assert_eq!(
d.relation,
RemoteRelation::Diverged {
ahead: 2,
behind: 1
}
);
assert!(!d.rewritten);
let mut u = facts(Some((2, 1)));
u.related = false;
assert_eq!(class(u).relation, RemoteRelation::Unrelated);
}
#[test]
fn rewritten_by_cherry_counts_or_reflog() {
let mut f = facts(Some((2, 2)));
f.remote_unmatched = Some(0);
assert!(class(f).rewritten);
let mut f = facts(Some((2, 2)));
f.remote_unmatched = Some(1);
assert!(!class(f).rewritten);
f.remote_tip_in_reflog = true;
assert!(class(f).rewritten);
let mut f = facts(Some((0, 2)));
f.remote_tip_in_reflog = true;
assert!(!class(f).rewritten);
}
#[test]
fn table_up_to_date_and_ahead_push() {
for ab in [Some((0, 0)), Some((3, 0)), None] {
let s = suggest(&class(facts(ab)), &ctx());
assert_eq!(s.action, A::Push, "{ab:?}");
assert_eq!(s.allowed, vec![A::Push, A::Skip]);
}
}
#[test]
fn table_remote_ahead_fast_forwards() {
let s = suggest(&class(facts(Some((0, 2)))), &ctx());
assert_eq!(s.action, A::FastForwardFrom);
assert_eq!(s.allowed, vec![A::FastForwardFrom, A::Skip]);
}
#[test]
fn table_rewritten_diverged_forces_with_lease() {
let mut f = facts(Some((2, 2)));
f.remote_unmatched = Some(0);
let s = suggest(&class(f), &ctx());
assert_eq!(s.action, A::ForceWithLease);
assert_eq!(
s.allowed,
vec![A::ForceWithLease, A::MergeFrom, A::RebaseOnto, A::Skip]
);
}
#[test]
fn table_real_divergence_merges_or_rebases() {
let mut f = facts(Some((2, 1)));
f.remote_unmatched = Some(1);
let c = class(f);
let s = suggest(&c, &ctx());
assert_eq!(s.action, A::MergeFrom);
assert_eq!(
s.allowed,
vec![A::MergeFrom, A::RebaseOnto, A::ForceWithLease, A::Skip]
);
let rebase = SuggestContext {
prefer_rebase: true,
..ctx()
};
let s = suggest(&c, &rebase);
assert_eq!(s.action, A::RebaseOnto);
assert_eq!(s.allowed[1], A::MergeFrom);
assert_eq!(s.allowed.len(), 4);
let held = SuggestContext {
prefer_rebase: true,
others_already_have_local: true,
..ctx()
};
let s = suggest(&c, &held);
assert_eq!(s.action, A::MergeFrom);
assert!(s.allowed.contains(&A::RebaseOnto));
}
#[test]
fn table_unrelated_skips_but_offers_force() {
let mut f = facts(Some((2, 2)));
f.related = false;
let s = suggest(&class(f), &ctx());
assert_eq!(s.action, A::Skip);
assert_eq!(s.allowed, vec![A::Skip, A::ForceWithLease]);
}
#[test]
fn table_refused_rows_skip_and_never_offer_force() {
let refused = SuggestContext {
refused: true,
..ctx()
};
for ab in [Some((0, 0)), Some((2, 2)), Some((0, 1)), None] {
let mut f = facts(ab);
f.remote_unmatched = Some(0);
let s = suggest(&class(f), &refused);
assert_eq!(s.action, A::Skip);
assert_eq!(s.allowed, vec![A::Skip]);
}
}
#[test]
fn pull_mode_follows_the_pull_mode_and_never_forces() {
let pull = SuggestContext {
mode: GroupMode::Pull,
..ctx()
};
assert_eq!(
suggest(&class(facts(Some((0, 2)))), &pull).action,
A::FastForwardFrom
);
assert_eq!(suggest(&class(facts(Some((2, 0)))), &pull).action, A::Skip);
assert_eq!(suggest(&class(facts(Some((0, 0)))), &pull).action, A::Skip);
assert_eq!(suggest(&class(facts(None)), &pull).action, A::Skip);
let d = class(facts(Some((1, 1))));
assert_eq!(suggest(&d, &pull).action, A::MergeFrom);
let rebase = SuggestContext {
prefer_rebase: true,
..pull
};
let s = suggest(&d, &rebase);
assert_eq!(s.action, A::RebaseOnto);
assert!(!s.allowed.contains(&A::ForceWithLease));
}
#[test]
fn holds_local_covers_equal_ahead_and_rebased_copies() {
let c = |a, b| class(facts(Some((a, b))));
assert!(c(0, 0).holds_local(None));
assert!(c(0, 2).holds_local(None));
assert!(!c(2, 0).holds_local(None));
assert!(c(2, 2).holds_local(Some(0)));
assert!(!c(2, 2).holds_local(Some(1)));
assert!(!class(facts(None)).holds_local(None));
}
#[test]
fn url_normalisation_detects_twins() {
let same = [
"https://github.com/o/r.git",
"https://user:tok@GitHub.com/o/r",
"git@github.com:o/r.git",
"ssh://git@github.com:22/o/r.git/",
];
let first = normalize_url(same[0]);
for u in same {
assert_eq!(normalize_url(u), first, "{u}");
}
assert_ne!(normalize_url("https://github.com/o/R"), first);
assert_ne!(normalize_url("https://gitlab.com/o/r.git"), first);
assert_eq!(normalize_url("/srv/git/a.git"), "/srv/git/a");
assert_eq!(normalize_url("file:///srv/git/a.git/"), "/srv/git/a");
assert_eq!(normalize_url("C:/x/a.git"), "C:/x/a");
}
}