use crate::common;
use common::Repo;
fn with_origin() -> Repo {
let r = Repo::new();
r.stage("a.txt", "x\n");
r.commit("init");
let origin = r.path(".git/test-origin.git");
r.git(&["init", "-q", "--bare", origin.to_str().unwrap()]);
r.git(&["remote", "add", "origin", origin.to_str().unwrap()]);
assert!(
String::from_utf8_lossy(&r.git(&["status", "--porcelain"]).stdout)
.trim()
.is_empty(),
"the fixture itself must leave a CLEAN tree, or these tests prove nothing"
);
r
}
#[test]
fn a_branch_tracking_a_local_branch_is_not_silently_synced_as_origin() {
let r = with_origin();
r.git(&["checkout", "-q", "-b", "feat/from-local"]);
r.stage("b.txt", "extra work\n");
r.commit("extra work");
r.git(&["branch", "--set-upstream-to=main", "feat/from-local"]);
let before = String::from_utf8_lossy(&r.git(&["rev-parse", "HEAD"]).stdout)
.trim()
.to_string();
let run = r.hook("pre-push-pull-rebase", &[]);
assert!(run.passed(), "{}", run.output());
assert!(
run.says("not a remote-tracking branch"),
"a local-branch upstream must be named and skipped, not guessed at as origin: {}",
run.output()
);
let after = String::from_utf8_lossy(&r.git(&["rev-parse", "HEAD"]).stdout)
.trim()
.to_string();
assert_eq!(
before, after,
"the branch must not be touched at all when its upstream is not a real remote"
);
}
#[test]
fn the_default_branch_advisory_survives_a_worktree_marker() {
let r = with_origin();
r.stage("a.txt", "one\n");
r.commit("chore: seed");
r.git(&["push", "-q", "--no-verify", "-u", "origin", "main"]);
let wt = r.worktree("feat/adv");
let mut push = std::process::Command::new("git");
push.args(["push", "-q", "--no-verify", "-u", "origin", "feat/adv"])
.current_dir(&wt);
Repo::strip_git_env_impl(&mut push);
assert!(push.status().expect("push").success());
r.stage("a.txt", "two\n");
r.commit("chore: move main on");
r.git(&["push", "-q", "--no-verify", "-u", "origin", "main"]);
let listed = String::from_utf8_lossy(
&std::process::Command::new("git")
.args(["branch"])
.current_dir(&wt)
.output()
.expect("git branch")
.stdout,
)
.into_owned();
assert!(
listed.contains("+ main"),
"fixture: expected the worktree marker, got {listed:?}"
);
let run = r.hook_at(&wt, "pre-push-pull-rebase", &[]);
assert!(run.passed(), "{}", run.output());
assert!(
run.says("is ahead by 1 commit"),
"the advisory never fired past the worktree marker:\n{}",
run.output()
);
}
#[test]
fn the_advisory_uses_the_branch_s_own_remote() {
let r = Repo::new();
r.stage("a.txt", "one\n");
r.commit("chore: seed");
let remote = r.path(".git/test-upstream.git");
r.git(&["init", "-q", "--bare", remote.to_str().unwrap()]);
r.git(&["remote", "add", "upstream", remote.to_str().unwrap()]);
r.git(&["push", "-q", "--no-verify", "-u", "upstream", "main"]);
r.git(&["checkout", "-q", "-b", "test/zz"]);
r.git(&["push", "-q", "--no-verify", "-u", "upstream", "test/zz"]);
r.git(&["checkout", "-q", "main"]);
r.stage("a.txt", "two\n");
r.commit("chore: move main on");
r.git(&["push", "-q", "--no-verify", "upstream", "main"]);
r.git(&["checkout", "-q", "test/zz"]);
let listed = String::from_utf8_lossy(&r.git(&["branch"]).stdout).into_owned();
assert!(
listed.starts_with(" main"),
"fixture: main must sort first for this case to bite, got {listed:?}"
);
let run = r.hook("pre-push-pull-rebase", &[]);
assert!(run.passed(), "{}", run.output());
assert!(
run.says("upstream/main is ahead by 1 commit"),
"the advisory looked for a remote this repo does not have:\n{}",
run.output()
);
}
#[test]
fn offline_reads_as_unreachable_not_deleted() {
let r = with_origin();
r.git(&["checkout", "-q", "-b", "feat/offline"]);
r.git(&["push", "-q", "--no-verify", "-u", "origin", "feat/offline"]);
r.git(&["remote", "set-url", "origin", "/nonexistent/nowhere.git"]);
let run = r.hook("pre-push-pull-rebase", &[]);
assert!(run.passed(), "{}", run.output());
assert!(
run.says("Could not reach"),
"offline must read as unreachable:\n{}",
run.output()
);
assert!(
!run.says("no longer exists"),
"offline must NOT read as a deleted upstream:\n{}",
run.output()
);
}
#[cfg(unix)]
#[test]
fn a_hung_remote_is_cut_at_the_deadline() {
let r = with_origin();
r.git(&["checkout", "-q", "-b", "feat/hung"]);
r.git(&["push", "-q", "--no-verify", "-u", "origin", "feat/hung"]);
r.git(&["config", "greengate.timeout", "1"]);
let real = String::from_utf8(
std::process::Command::new("sh")
.args(["-c", "command -v git"])
.output()
.expect("sh")
.stdout,
)
.expect("utf8");
let real = real.trim().to_string();
let shims = r.path(".git/gitshim");
std::fs::create_dir_all(&shims).expect("mkdir");
common::fixture_exec(
&shims.join("git"),
&format!("#!/bin/sh\ncase \"$1\" in ls-remote) sleep 30 ;; esac\nexec {real} \"$@\"\n"),
);
let started = std::time::Instant::now();
let mut cmd = crate::common::amont_cmd();
cmd.arg("--hooks-dir")
.arg(r.path(".git/hooks"))
.arg("pre-push-pull-rebase")
.current_dir(&r.dir)
.stdin(std::process::Stdio::null())
.env(
"PATH",
format!(
"{}:{}",
shims.display(),
std::env::var("PATH").unwrap_or_default()
),
);
let out = cmd.output().expect("run hook");
let text = format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
assert!(out.status.success(), "{text}");
assert!(
text.contains("did not answer within"),
"the deadline must be named:\n{text}"
);
assert!(
started.elapsed() < std::time::Duration::from_secs(15),
"the probe was not killed at its deadline (took {:?})",
started.elapsed()
);
}
fn hook_with_push(r: &Repo, line: &str) -> (i32, String) {
use std::io::Write;
use std::process::Stdio;
let mut cmd = crate::common::amont_cmd();
cmd.arg("--hooks-dir")
.arg(r.path(".git/hooks"))
.arg("pre-push-pull-rebase")
.current_dir(&r.dir)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
Repo::strip_git_env_impl(&mut cmd);
Repo::pin_host_env(&mut cmd);
let mut child = cmd.spawn().expect("run amont");
child
.stdin
.take()
.unwrap()
.write_all(line.as_bytes())
.unwrap();
let out = child.wait_with_output().unwrap();
(
out.status.code().unwrap_or(-1),
format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
),
)
}
#[test]
fn a_tag_push_leaves_a_behind_branch_alone() {
let r = with_origin();
r.stage("b.txt", "ahead on the remote\n");
r.commit("chore: remote-side commit");
r.git(&["push", "-q", "--no-verify", "-u", "origin", "main"]);
r.git(&["reset", "--hard", "-q", "HEAD~1"]);
r.git(&["fetch", "-q", "origin"]);
let head = |r: &Repo| {
String::from_utf8_lossy(&r.git(&["rev-parse", "HEAD"]).stdout)
.trim()
.to_string()
};
let before = head(&r);
let zero = "0".repeat(before.len());
let tag = format!("refs/tags/v1 {before} refs/tags/v1 {zero}\n");
let (code, out) = hook_with_push(&r, &tag);
assert_eq!(code, 0, "{out}");
assert!(!out.contains("push again"), "{out}");
assert_eq!(head(&r), before, "the checkout was not touched");
let branch = format!("refs/heads/main {before} refs/heads/main {zero}\n");
let (code, out) = hook_with_push(&r, &branch);
assert_ne!(code, 0, "{out}");
assert!(out.contains("push again"), "{out}");
}