use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use super::{PushState, RefChange, RunId};
use crate::domain::Oid;
pub const PUSHED_FILE: &str = "pushed.json";
pub const DEFAULT_PROTECTED_BRANCHES: &[&str] = &["main", "master", "develop", "release/*"];
fn yes() -> bool {
true
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushRewritten {
pub run: RunId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub remote: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub include_protected: Vec<String>,
#[serde(default)]
pub include_all_protected: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub refs: Option<Vec<String>>,
#[serde(default)]
pub dry_run: bool,
#[serde(default = "yes")]
pub atomic: bool,
}
impl PushRewritten {
pub fn new(run: RunId) -> Self {
Self {
run,
remote: None,
include_protected: Vec::new(),
include_all_protected: false,
refs: None,
dry_run: false,
atomic: true,
}
}
pub fn selects(&self, name: &str) -> bool {
let short = name
.strip_prefix("refs/heads/")
.or_else(|| name.strip_prefix("refs/tags/"))
.unwrap_or(name);
match &self.refs {
None => true,
Some(only) => only.iter().any(|n| n == name || n == short),
}
}
pub fn includes(&self, name: &str) -> bool {
let short = name.strip_prefix("refs/heads/").unwrap_or(name);
self.include_all_protected
|| self
.include_protected
.iter()
.any(|n| n == name || n == short)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PushRefStatus {
Pushed,
WouldPush,
UpToDate,
RejectedRemoteMoved,
RejectedProtectedByHost,
RejectedOther,
SkippedProtected,
NotPushed,
}
impl PushRefStatus {
pub fn is_rejected(self) -> bool {
matches!(
self,
Self::RejectedRemoteMoved | Self::RejectedProtectedByHost | Self::RejectedOther
)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushRefResult {
pub name: String,
pub remote: String,
pub expected_old: Oid,
pub new: Option<Oid>,
pub status: PushRefStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub detail: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushRewrittenOutcome {
pub run: RunId,
pub dry_run: bool,
pub results: Vec<PushRefResult>,
#[serde(default)]
pub atomic_fallback: bool,
}
impl PushRewrittenOutcome {
pub fn count(&self, status: PushRefStatus) -> usize {
self.results.iter().filter(|r| r.status == status).count()
}
pub fn is_clean(&self) -> bool {
self.results
.iter()
.all(|r| !r.status.is_rejected() && r.status != PushRefStatus::NotPushed)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushedRef {
pub name: String,
pub remote: String,
pub new: Oid,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RewriteRecord {
pub run: RunId,
pub dir: PathBuf,
pub refs: Vec<RefChange>,
pub on_remote: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RewriteMaps {
pub run: RunId,
pub dir: PathBuf,
pub commit_map: PathBuf,
pub ref_map: PathBuf,
pub copied: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NoteRefKind {
Branch,
Tag,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NoteRef {
pub name: String,
pub short: String,
pub kind: NoteRefKind,
pub remote: String,
pub old: Oid,
pub new: Oid,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RewriteNote {
pub run: RunId,
pub refs: Vec<NoteRef>,
pub text: String,
}
pub fn note_refs(record: &RewriteRecord) -> Vec<NoteRef> {
record
.refs
.iter()
.filter(|r| record.on_remote.contains(&r.name))
.filter_map(|r| {
let PushState::OnRemote { remote, .. } = &r.pushed else {
return None;
};
let new = r.new.filter(|n| *n != r.old)?;
let (kind, short) = if let Some(s) = r.name.strip_prefix("refs/heads/") {
(NoteRefKind::Branch, s)
} else {
(NoteRefKind::Tag, r.name.strip_prefix("refs/tags/")?)
};
Some(NoteRef {
name: r.name.clone(),
short: short.to_string(),
kind,
remote: remote.clone(),
old: r.old,
new,
})
})
.collect()
}
fn short(oid: Oid) -> String {
oid.to_hex()[..12].to_string()
}
pub fn note_text(refs: &[NoteRef]) -> String {
if refs.is_empty() {
return "No pushed ref was rewritten: nobody else needs to do anything.\n".to_string();
}
let mut out = String::from(
"The history of this repository was rewritten and force-pushed.\n\
Your clone still has the old history. Do not push your old branches \
back: that would bring the old commits back.\n\n",
);
for r in refs {
let (old, new) = (short(r.old), short(r.new));
match r.kind {
NoteRefKind::Branch => out.push_str(&format!(
"Branch {b} on {rem}: {old} -> {new}\n \
No local work on it:\n \
git fetch {rem}\n \
git checkout {b}\n \
git reset --hard {rem}/{b}\n \
Local commits on top of the old {b}:\n \
git fetch {rem}\n \
git rebase --onto {rem}/{b} {old} {b}\n\n",
b = r.short,
rem = r.remote,
)),
NoteRefKind::Tag => out.push_str(&format!(
"Tag {t} on {rem}: {old} -> {new}\n \
Replace your copy:\n \
git fetch {rem} +refs/tags/{t}:refs/tags/{t}\n\n",
t = r.short,
rem = r.remote,
)),
}
}
out.push_str(
"Other branches of yours that contain old commits keep them until you \
rebase them with `git rebase --onto <new base> <old base> <branch>` \
(the old and new ids above, and the commit map, tell which).\n",
);
out
}
#[cfg(test)]
mod tests {
use super::*;
fn change(name: &str, pushed: PushState) -> RefChange {
RefChange {
name: name.into(),
old: Oid([1; 20]),
new: Some(Oid([2; 20])),
pushed,
protected: false,
included: true,
checked_out_in: None,
}
}
fn on_origin() -> PushState {
PushState::OnRemote {
remote: "origin".into(),
remote_oid: Oid([1; 20]),
rewritten_on_remote: 1,
}
}
#[test]
fn a_note_lists_only_pushed_branches_and_tags() {
let record = RewriteRecord {
run: RunId("r".into()),
dir: PathBuf::new(),
refs: vec![
change("refs/heads/main", on_origin()),
change("refs/heads/local", PushState::LocalOnly),
change("refs/heads/odd", on_origin()),
change("refs/tags/v1", on_origin()),
],
on_remote: vec!["refs/heads/main".into(), "refs/tags/v1".into()],
};
let refs = note_refs(&record);
assert_eq!(refs.len(), 2);
assert_eq!(refs[0].kind, NoteRefKind::Branch);
assert_eq!(refs[1].short, "v1");
let text = note_text(&refs);
assert!(text.contains("git reset --hard origin/main"), "{text}");
assert!(
text.contains("git rebase --onto origin/main 010101010101 main"),
"{text}"
);
assert!(
text.contains("git fetch origin +refs/tags/v1:refs/tags/v1"),
"{text}"
);
assert!(!text.contains('\u{2014}'));
}
#[test]
fn a_request_defaults_to_atomic_and_asks_for_protected_refs_by_name() {
let args: PushRewritten = serde_json::from_str(r#"{"run":"r"}"#).unwrap();
assert!(args.atomic && !args.dry_run && !args.include_all_protected);
let mut asked = PushRewritten::new(RunId("r".into()));
asked.include_protected = vec!["main".into()];
assert!(asked.includes("refs/heads/main"));
assert!(!asked.includes("refs/heads/develop"));
asked.include_all_protected = true;
assert!(asked.includes("refs/heads/develop"));
assert!(asked.selects("refs/heads/anything"), "no selection: all");
asked.refs = Some(vec!["main".into(), "refs/tags/v1".into()]);
assert!(asked.selects("refs/heads/main") && asked.selects("refs/tags/v1"));
assert!(!asked.selects("refs/heads/dev"));
}
}