From e4a52864cc43d352f96ba48361e15e5e2a5ddf64 Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 05:34:57 +0000 Subject: [PATCH 1/7] Keep default-branch commits out of pre-push reviews after a merge or rebase The pre-push hook compared each updated branch with the commit the remote had. Once the branch merged or rebased onto newer default-branch commits, that diff also carried those commits, so the push review included other people's already-merged changes: credits spent on code already reviewed, findings in code the push did not add, and larger patches more likely to come back incomplete. The remote's commit stays the base while the branch takes in nothing new from the default branch, so ordinary pushes still review only what they add. Otherwise the branch is compared with its merge-base with the default branch, as a new branch already is. A remote commit missing from the checkout falls back the same way instead of failing the diff. --- src/git.rs | 53 ++++++++++++++++++++++++++++++++++++++++ src/hooks.rs | 3 ++- tests/hooks.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index 1c392b4..225c3c6 100644 --- a/src/git.rs +++ b/src/git.rs @@ -365,6 +365,59 @@ pub async fn new_branch_base(dir: &Path, remote: &str, head: &str) -> Result Result { + let remote_sha = commit_sha(remote_sha)?; + let known = match resolve(dir, &remote_sha).await { + Ok(_) => true, + Err(GitError::UnknownRevision(_)) => false, + Err(error) => return Err(error), + }; + let default = format!("refs/remotes/{remote}/HEAD"); + let fork_point = match merge_base(dir, remote, &commit_sha(head)?, &[default.as_str()]).await { + Ok(fork_point) => fork_point, + // Without a default branch, the remote's commit is the only base. + Err(GitError::NoBase(_)) if known => return Ok(remote_sha), + Err(error) => return Err(error), + }; + if known && is_ancestor(dir, &fork_point, &remote_sha).await? { + Ok(remote_sha) + } else { + Ok(fork_point) + } +} + +/// Whether `ancestor` is reachable from `descendant`. +async fn is_ancestor(dir: &Path, ancestor: &str, descendant: &str) -> Result { + let args = ["merge-base", "--is-ancestor", ancestor, descendant]; + let output = Command::new("git") + .arg("-C") + .arg(dir) + .args(args) + .output() + .await + .map_err(|error| GitError::Unavailable(error.to_string()))?; + match output.status.code() { + Some(0) => Ok(true), + Some(1) => Ok(false), + _ => Err(GitError::Failed { + command: args.join(" "), + stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(), + }), + } +} + /// The patch from `base_commit_sha` to `head`, or to `index` when `head` is /// `None`. async fn diff( diff --git a/src/hooks.rs b/src/hooks.rs index e39691d..fea08ca 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -394,7 +394,8 @@ async fn subjects(hook: Hook, here: &Path, remote: &str) -> anyhow::Result<(Vec< let base = if is_zero(&pushed.remote_sha) { git::new_branch_base(here, remote, &pushed.local_sha).await } else { - Ok(pushed.remote_sha) + git::pushed_branch_base(here, remote, &pushed.remote_sha, &pushed.local_sha) + .await }; let changes = match base { Ok(base) => git::range_changes(here, &base, &pushed.local_sha).await, diff --git a/tests/hooks.rs b/tests/hooks.rs index 3473f84..e32b0af 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -611,6 +611,72 @@ async fn each_pushed_branch_is_reviewed_against_the_commit_the_remote_has() { ); } +#[tokio::test] +async fn a_pushed_branch_is_not_reviewed_with_commits_from_the_default_branch() { + let checkout = Checkout::new().await; + let uploads = Uploads::default(); + let origin = serve(uploads.clone(), "SERIOUS", false).await; + checkout.git(&["checkout", "-q", "-b", "feature"]).await; + let pushed = checkout.commit("feature.rs", "fn feature() {}\n").await; + // Another change reaches the default branch after the branch was pushed. + checkout.git(&["checkout", "-q", "main"]).await; + let main = checkout.commit("teammate.rs", "fn teammate() {}\n").await; + checkout + .git(&["update-ref", "refs/remotes/origin/main", &main]) + .await; + let unknown = "1".repeat(40); + // (how the branch is updated, the remote's commit, the expected base) + let cases = [ + ("merge", pushed.as_str(), main.as_str()), + ("rebase", pushed.as_str(), main.as_str()), + ("amend", pushed.as_str(), pushed.as_str()), + ("amend", unknown.as_str(), checkout.base.as_str()), + ]; + for (update, remote_sha, expected_base) in cases { + checkout + .git(&["checkout", "-q", "-B", "feature", &pushed]) + .await; + match update { + "merge" => { + checkout + .git(&["merge", "-q", "--no-edit", "origin/main"]) + .await; + } + "rebase" => { + checkout.git(&["rebase", "-q", "origin/main"]).await; + } + _ => { + checkout.git(&["reset", "-q", "--soft", "HEAD~"]).await; + } + } + let head = checkout + .commit("feature.rs", "fn feature() { pushed() }\n") + .await; + uploads.lock().unwrap().clear(); + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("refs/heads/feature {head} refs/heads/feature {remote_sha}\n"), + ) + .await; + let case = format!("{update} over {remote_sha}"); + assert_eq!(output.status.code(), Some(0), "{case}: {}", stderr(&output)); + let uploads = uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1, "{case}"); + assert_eq!(uploads[0]["base_commit_sha"], expected_base, "{case}"); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert!(patch.contains("+fn feature() { pushed() }"), "{case}"); + assert!(!patch.contains("teammate"), "{case}: {patch}"); + } +} + #[tokio::test] #[ignore = "requires official pre-commit >=3.2.0 on PATH; run this suite with --include-ignored"] async fn the_pre_commit_framework_preserves_selection_policy_and_existing_configuration() { From 4a9a05c00c60d8919d4c63e77936da0d66ecf286 Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:08:07 +0000 Subject: [PATCH 2/7] Keep the remote commit as the base of a branch unrelated to the default branch A branch with no history in common with the default branch, such as gh-pages, has no merge-base with it. `git merge-base` then exits 1, which the pre-push base selection treated as a failure, so the push printed as not reviewed where it was reviewed against the remote's commit before. Exit status 1 now means "no common ancestor", and the remote's commit stays the base. The amend case of the regression test now also checks that a patch against the remote's commit holds only the change to it, and that a patch against a merge-base adds the whole file. Both found by a Proofline review of this change. --- src/git.rs | 46 ++++++++++++++++++++++++++++++++-------------- tests/hooks.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/src/git.rs b/src/git.rs index 225c3c6..817af79 100644 --- a/src/git.rs +++ b/src/git.rs @@ -372,6 +372,8 @@ pub async fn new_branch_base(dir: &Path, remote: &str, head: &str) -> Result return Err(error), }; let default = format!("refs/remotes/{remote}/HEAD"); - let fork_point = match merge_base(dir, remote, &commit_sha(head)?, &[default.as_str()]).await { - Ok(fork_point) => fork_point, - // Without a default branch, the remote's commit is the only base. - Err(GitError::NoBase(_)) if known => return Ok(remote_sha), - Err(error) => return Err(error), - }; - if known && is_ancestor(dir, &fork_point, &remote_sha).await? { - Ok(remote_sha) + let fork_point = if line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) + .await + .is_ok() + { + answer_in(dir, &["merge-base", &commit_sha(head)?, &default]) + .await? + .map(|fork_point| commit_sha(&fork_point)) + .transpose()? } else { - Ok(fork_point) + None + }; + match fork_point { + Some(fork_point) if known => { + let ancestor = ["merge-base", "--is-ancestor", &fork_point, &remote_sha]; + Ok(if answer_in(dir, &ancestor).await?.is_some() { + remote_sha + } else { + fork_point + }) + } + Some(fork_point) => Ok(fork_point), + None if known => Ok(remote_sha), + None => Err(GitError::NoBase(remote.to_owned())), } } -/// Whether `ancestor` is reachable from `descendant`. -async fn is_ancestor(dir: &Path, ancestor: &str, descendant: &str) -> Result { - let args = ["merge-base", "--is-ancestor", ancestor, descendant]; +/// Runs git in `dir` where exit status 1 is an answer, not a failure: `None` +/// for status 1 (not an ancestor, no common ancestor), and the trimmed output +/// otherwise. +async fn answer_in(dir: &Path, args: &[&str]) -> Result, GitError> { let output = Command::new("git") .arg("-C") .arg(dir) @@ -409,8 +425,10 @@ async fn is_ancestor(dir: &Path, ancestor: &str, descendant: &str) -> Result Ok(true), - Some(1) => Ok(false), + Some(0) => Ok(Some( + String::from_utf8_lossy(&output.stdout).trim().to_owned(), + )), + Some(1) => Ok(None), _ => Err(GitError::Failed { command: args.join(" "), stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(), diff --git a/tests/hooks.rs b/tests/hooks.rs index e32b0af..c071be7 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -673,10 +673,51 @@ async fn a_pushed_branch_is_not_reviewed_with_commits_from_the_default_branch() assert_eq!(uploads[0]["base_commit_sha"], expected_base, "{case}"); let patch = uploads[0]["patch"].as_str().unwrap(); assert!(patch.contains("+fn feature() { pushed() }"), "{case}"); + // Against the remote's commit, the patch is only the change to it; + // against a merge-base, it adds the whole file. + assert_eq!( + patch.contains("-fn feature() {}"), + expected_base == pushed, + "{case}: {patch}" + ); assert!(!patch.contains("teammate"), "{case}: {patch}"); } } +#[tokio::test] +async fn a_branch_without_history_in_common_with_the_default_branch_keeps_the_remote_commit() { + let checkout = Checkout::new().await; + let uploads = Uploads::default(); + let origin = serve(uploads.clone(), "SERIOUS", false).await; + // A site branch such as gh-pages starts from no commit of main. + checkout.git(&["checkout", "-q", "--orphan", "pages"]).await; + checkout.git(&["rm", "-q", "-r", "--cached", "."]).await; + let pushed = checkout.commit("index.html", "

old

\n").await; + let head = checkout.commit("index.html", "

new

\n").await; + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("refs/heads/pages {head} refs/heads/pages {pushed}\n"), + ) + .await; + assert_eq!(output.status.code(), Some(0), "{}", stderr(&output)); + let uploads = uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1, "{}", stderr(&output)); + assert_eq!(uploads[0]["base_commit_sha"], pushed); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert!( + patch.contains("-

old

") && patch.contains("+

new

"), + "{patch}" + ); +} + #[tokio::test] #[ignore = "requires official pre-commit >=3.2.0 on PATH; run this suite with --include-ignored"] async fn the_pre_commit_framework_preserves_selection_policy_and_existing_configuration() { From 35eb68e870b99f97a2224ac0ebeff57ef3bb73cb Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:17:05 +0000 Subject: [PATCH 3/7] Review a push to the default branch against the commit the remote has A push to the default branch itself compared its tip with this checkout's tracking ref for that branch. When the remote's default branch had been rewound and the push restored the commit the stale tracking ref still held, the merge-base was the pushed commit, so the hook found nothing to review and let the push through even with onIncomplete=block. The hook now keeps the destination ref from Git's input, and an update of the remote's default branch is compared with the commit the remote advertises, as before this change. The base-selection tests now push a branch that is not checked out, so a base read from HEAD instead of the pushed commit fails them. Both found by a second Proofline review of this change. --- src/git.rs | 13 ++++++++++++- src/hooks.rs | 16 +++++++++++++--- tests/hooks.rs | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/git.rs b/src/git.rs index 817af79..c413b69 100644 --- a/src/git.rs +++ b/src/git.rs @@ -373,10 +373,13 @@ pub async fn new_branch_base(dir: &Path, remote: &str, head: &str) -> Result Result { @@ -387,6 +390,14 @@ pub async fn pushed_branch_base( Err(error) => return Err(error), }; let default = format!("refs/remotes/{remote}/HEAD"); + let default_branch = answer_in(dir, &["symbolic-ref", "--quiet", &default]).await?; + let to_default = default_branch + .as_deref() + .and_then(|target| target.strip_prefix(&format!("refs/remotes/{remote}/"))) + .is_some_and(|name| remote_ref.strip_prefix("refs/heads/") == Some(name)); + if known && to_default { + return Ok(remote_sha); + } let fork_point = if line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) .await .is_ok() diff --git a/src/hooks.rs b/src/hooks.rs index fea08ca..e249139 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -317,6 +317,7 @@ async fn settings(dir: &Path, hook: Hook) -> anyhow::Result { struct PushedRef { local_ref: String, local_sha: String, + remote_ref: String, remote_sha: String, } @@ -330,7 +331,7 @@ fn pushed_refs(input: &str) -> anyhow::Result> { let mut refs = Vec::new(); for line in input.lines().filter(|line| !line.trim().is_empty()) { let fields: Vec<&str> = line.split(' ').collect(); - let [local_ref, local_sha, _, remote_sha] = fields.as_slice() else { + let [local_ref, local_sha, remote_ref, remote_sha] = fields.as_slice() else { bail!( "Git's pre-push input has a line Proofline cannot read: {:?}", one_line(line) @@ -342,6 +343,7 @@ fn pushed_refs(input: &str) -> anyhow::Result> { refs.push(PushedRef { local_ref: (*local_ref).to_owned(), local_sha: (*local_sha).to_owned(), + remote_ref: (*remote_ref).to_owned(), remote_sha: (*remote_sha).to_owned(), }); } @@ -394,8 +396,14 @@ async fn subjects(hook: Hook, here: &Path, remote: &str) -> anyhow::Result<(Vec< let base = if is_zero(&pushed.remote_sha) { git::new_branch_base(here, remote, &pushed.local_sha).await } else { - git::pushed_branch_base(here, remote, &pushed.remote_sha, &pushed.local_sha) - .await + git::pushed_branch_base( + here, + remote, + &pushed.remote_ref, + &pushed.remote_sha, + &pushed.local_sha, + ) + .await }; let changes = match base { Ok(base) => git::range_changes(here, &base, &pushed.local_sha).await, @@ -844,11 +852,13 @@ mod tests { PushedRef { local_ref: "refs/heads/new".to_owned(), local_sha: a.clone(), + remote_ref: "refs/heads/new".to_owned(), remote_sha: zero, }, PushedRef { local_ref: "refs/heads/main".to_owned(), local_sha: b, + remote_ref: "refs/heads/main".to_owned(), remote_sha: a, }, ] diff --git a/tests/hooks.rs b/tests/hooks.rs index c071be7..c10d822 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -652,6 +652,9 @@ async fn a_pushed_branch_is_not_reviewed_with_commits_from_the_default_branch() let head = checkout .commit("feature.rs", "fn feature() { pushed() }\n") .await; + // Push the branch without it checked out, so a base read from HEAD + // instead of the pushed commit fails the amend cases. + checkout.git(&["checkout", "-q", "main"]).await; uploads.lock().unwrap().clear(); let output = checkout .proofline( @@ -684,6 +687,43 @@ async fn a_pushed_branch_is_not_reviewed_with_commits_from_the_default_branch() } } +#[tokio::test] +async fn a_push_to_the_default_branch_is_reviewed_against_the_commit_the_remote_has() { + let checkout = Checkout::new().await; + let uploads = Uploads::default(); + let origin = serve(uploads.clone(), "SERIOUS", false).await; + // The remote's main was rewound to the base, but this checkout's + // tracking ref still holds the commit the push now restores. + let restored = checkout + .commit("orders.rs", "fn main() { restored() }\n") + .await; + checkout + .git(&["update-ref", "refs/remotes/origin/main", &restored]) + .await; + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!( + "refs/heads/main {restored} refs/heads/main {}\n", + checkout.base + ), + ) + .await; + assert_eq!(output.status.code(), Some(0), "{}", stderr(&output)); + let uploads = uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1, "{}", stderr(&output)); + assert_eq!(uploads[0]["base_commit_sha"], checkout.base); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert!(patch.contains("+fn main() { restored() }"), "{patch}"); +} + #[tokio::test] async fn a_branch_without_history_in_common_with_the_default_branch_keeps_the_remote_commit() { let checkout = Checkout::new().await; @@ -691,9 +731,10 @@ async fn a_branch_without_history_in_common_with_the_default_branch_keeps_the_re let origin = serve(uploads.clone(), "SERIOUS", false).await; // A site branch such as gh-pages starts from no commit of main. checkout.git(&["checkout", "-q", "--orphan", "pages"]).await; - checkout.git(&["rm", "-q", "-r", "--cached", "."]).await; + checkout.git(&["rm", "-q", "-r", "-f", "."]).await; let pushed = checkout.commit("index.html", "

old

\n").await; let head = checkout.commit("index.html", "

new

\n").await; + checkout.git(&["checkout", "-q", "main"]).await; let output = checkout .proofline( &origin, From 7bb6b44630ca9abd0c20912d4580c95b1ab32240 Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:22:05 +0000 Subject: [PATCH 4/7] Never let a stale tracking ref alone decide a push has nothing to review The merge-base with the tracking ref of the default branch is only a better base than the remote's commit when it leaves out commits the remote already has. A stale tracking ref breaks that. When the remote rewound a branch and the push restored a commit the tracking ref still held, the merge-base was the pushed commit itself, so the hook found nothing to review and let the push through. A destination that is not a branch, such as refs/for/main, had the same empty patch. Those pushes now keep the remote's commit as their base, as before this change: a push of a commit the tracking ref already holds, and a push to a ref outside refs/heads/. The tests push from a differently named local branch, so they fail if the hook reads the source ref where it needs the destination. Found by a third Proofline review of this change. --- src/git.rs | 15 ++++++++----- src/hooks.rs | 4 ++-- tests/hooks.rs | 61 ++++++++++++++++++++++++++++++-------------------- 3 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/git.rs b/src/git.rs index c413b69..0ec3c95 100644 --- a/src/git.rs +++ b/src/git.rs @@ -373,9 +373,12 @@ pub async fn new_branch_base(dir: &Path, remote: &str, head: &str) -> Result Result { let remote_sha = commit_sha(remote_sha)?; + let head = commit_sha(head)?; let known = match resolve(dir, &remote_sha).await { Ok(_) => true, Err(GitError::UnknownRevision(_)) => false, @@ -395,14 +399,14 @@ pub async fn pushed_branch_base( .as_deref() .and_then(|target| target.strip_prefix(&format!("refs/remotes/{remote}/"))) .is_some_and(|name| remote_ref.strip_prefix("refs/heads/") == Some(name)); - if known && to_default { + if known && (to_default || !remote_ref.starts_with("refs/heads/")) { return Ok(remote_sha); } let fork_point = if line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) .await .is_ok() { - answer_in(dir, &["merge-base", &commit_sha(head)?, &default]) + answer_in(dir, &["merge-base", &head, &default]) .await? .map(|fork_point| commit_sha(&fork_point)) .transpose()? @@ -410,6 +414,7 @@ pub async fn pushed_branch_base( None }; match fork_point { + Some(fork_point) if known && fork_point == head => Ok(remote_sha), Some(fork_point) if known => { let ancestor = ["merge-base", "--is-ancestor", &fork_point, &remote_sha]; Ok(if answer_in(dir, &ancestor).await?.is_some() { diff --git a/src/hooks.rs b/src/hooks.rs index e249139..7058225 100644 --- a/src/hooks.rs +++ b/src/hooks.rs @@ -842,7 +842,7 @@ mod tests { let (a, b) = ("a".repeat(40), "b".repeat(40)); let input = format!( "refs/heads/new {a} refs/heads/new {zero}\n\ - refs/heads/main {b} refs/heads/main {a}\n\ + refs/heads/topic {b} refs/heads/main {a}\n\ (delete) {zero} refs/heads/old {b}\n\ refs/tags/v1 {a} refs/tags/v1 {zero}\n" ); @@ -856,7 +856,7 @@ mod tests { remote_sha: zero, }, PushedRef { - local_ref: "refs/heads/main".to_owned(), + local_ref: "refs/heads/topic".to_owned(), local_sha: b, remote_ref: "refs/heads/main".to_owned(), remote_sha: a, diff --git a/tests/hooks.rs b/tests/hooks.rs index c10d822..da4c2b5 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -688,40 +688,53 @@ async fn a_pushed_branch_is_not_reviewed_with_commits_from_the_default_branch() } #[tokio::test] -async fn a_push_to_the_default_branch_is_reviewed_against_the_commit_the_remote_has() { +async fn a_pushed_commit_the_tracking_ref_holds_is_reviewed_against_the_commit_the_remote_has() { let checkout = Checkout::new().await; let uploads = Uploads::default(); let origin = serve(uploads.clone(), "SERIOUS", false).await; // The remote's main was rewound to the base, but this checkout's - // tracking ref still holds the commit the push now restores. + // tracking ref still holds the commit each push now restores. let restored = checkout .commit("orders.rs", "fn main() { restored() }\n") .await; checkout .git(&["update-ref", "refs/remotes/origin/main", &restored]) .await; - let output = checkout - .proofline( - &origin, - &[ - "hook", - "run", - "pre-push", - "origin", - "https://github.com/acme/web.git", - ], - &format!( - "refs/heads/main {restored} refs/heads/main {}\n", - checkout.base - ), - ) - .await; - assert_eq!(output.status.code(), Some(0), "{}", stderr(&output)); - let uploads = uploads.lock().unwrap(); - assert_eq!(uploads.len(), 1, "{}", stderr(&output)); - assert_eq!(uploads[0]["base_commit_sha"], checkout.base); - let patch = uploads[0]["patch"].as_str().unwrap(); - assert!(patch.contains("+fn main() { restored() }"), "{patch}"); + // (the pushed local ref, the destination ref) + let pushes = [ + // The default branch, pushed from a differently named branch. + ("refs/heads/restore", "refs/heads/main"), + // Another branch the remote also had at the base. + ("refs/heads/feature", "refs/heads/feature"), + // A destination that is not a branch. + ("refs/heads/main", "refs/for/main"), + ]; + for (local_ref, remote_ref) in pushes { + uploads.lock().unwrap().clear(); + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("{local_ref} {restored} {remote_ref} {}\n", checkout.base), + ) + .await; + let case = format!("{local_ref} to {remote_ref}"); + assert_eq!(output.status.code(), Some(0), "{case}: {}", stderr(&output)); + let uploads = uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1, "{case}: {}", stderr(&output)); + assert_eq!(uploads[0]["base_commit_sha"], checkout.base, "{case}"); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert!( + patch.contains("+fn main() { restored() }"), + "{case}: {patch}" + ); + } } #[tokio::test] From ba71aa2df398518b7f69873047e704b44c7f5c43 Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:25:10 +0000 Subject: [PATCH 5/7] Report a missing remote commit instead of an empty pre-push review When the remote's commit was missing from the checkout and the stale tracking ref already held the pushed commit, the merge-base fallback was the pushed commit itself. The hook then compared the commit with itself, found nothing to review and let the push through even with onIncomplete=block. Before this change the missing commit surfaced as a failed diff. It now surfaces as "names no commit in this checkout", so the push prints as not reviewed and onIncomplete applies. Found by a fourth Proofline review of this change. --- src/git.rs | 9 ++++++++- tests/hooks.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index 0ec3c95..43bf733 100644 --- a/src/git.rs +++ b/src/git.rs @@ -414,7 +414,14 @@ pub async fn pushed_branch_base( None }; match fork_point { - Some(fork_point) if known && fork_point == head => Ok(remote_sha), + // Comparing the pushed commit with itself would review nothing. + Some(fork_point) if fork_point == head => { + if known { + Ok(remote_sha) + } else { + Err(GitError::UnknownRevision(remote_sha)) + } + } Some(fork_point) if known => { let ancestor = ["merge-base", "--is-ancestor", &fork_point, &remote_sha]; Ok(if answer_in(dir, &ancestor).await?.is_some() { diff --git a/tests/hooks.rs b/tests/hooks.rs index da4c2b5..f3c4f94 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -737,6 +737,43 @@ async fn a_pushed_commit_the_tracking_ref_holds_is_reviewed_against_the_commit_t } } +#[tokio::test] +async fn a_push_is_not_reviewed_as_empty_when_the_remote_commit_is_missing() { + let checkout = Checkout::new().await; + let uploads = Uploads::default(); + let origin = serve(uploads.clone(), "SERIOUS", false).await; + // The remote's branch moved to a commit this checkout never fetched, + // and the stale tracking ref holds the commit the push restores. + let restored = checkout + .commit("orders.rs", "fn main() { restored() }\n") + .await; + checkout + .git(&["update-ref", "refs/remotes/origin/main", &restored]) + .await; + checkout + .git(&["config", "proofline.hook.pre-push.onIncomplete", "block"]) + .await; + let missing = "1".repeat(40); + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("refs/heads/feature {restored} refs/heads/feature {missing}\n"), + ) + .await; + let text = stderr(&output); + assert!(uploads.lock().unwrap().is_empty(), "{text}"); + assert!(!text.contains("nothing to review"), "{text}"); + assert!(text.contains("could not review feature"), "{text}"); + assert_eq!(output.status.code(), Some(1), "{text}"); +} + #[tokio::test] async fn a_branch_without_history_in_common_with_the_default_branch_keeps_the_remote_commit() { let checkout = Checkout::new().await; From ad48c72332bfed88e868e5d3261d183bada6d82c Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:28:42 +0000 Subject: [PATCH 6/7] Never fall back to the tracking ref for the default branch or other refs A push to the remote's default branch, or to a destination outside refs/heads/, is compared only with the commit the remote advertises. When that commit was missing from the checkout, the selector still fell back to the merge-base with the stale tracking ref and could review the wrong commits. Those pushes now print as not reviewed, as the missing commit did before this change, so onIncomplete applies. The rewind tests also push a further local commit, whose merge-base with the tracking ref differs from the pushed commit, so removing either destination check fails them. Found by a fifth Proofline review of this change. --- src/git.rs | 9 ++++-- tests/hooks.rs | 81 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/git.rs b/src/git.rs index 43bf733..fd7c327 100644 --- a/src/git.rs +++ b/src/git.rs @@ -399,8 +399,13 @@ pub async fn pushed_branch_base( .as_deref() .and_then(|target| target.strip_prefix(&format!("refs/remotes/{remote}/"))) .is_some_and(|name| remote_ref.strip_prefix("refs/heads/") == Some(name)); - if known && (to_default || !remote_ref.starts_with("refs/heads/")) { - return Ok(remote_sha); + if to_default || !remote_ref.starts_with("refs/heads/") { + // Only the remote's commit is a base for these destinations. + return if known { + Ok(remote_sha) + } else { + Err(GitError::UnknownRevision(remote_sha)) + }; } let fork_point = if line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) .await diff --git a/tests/hooks.rs b/tests/hooks.rs index f3c4f94..e6e15c3 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -697,19 +697,24 @@ async fn a_pushed_commit_the_tracking_ref_holds_is_reviewed_against_the_commit_t let restored = checkout .commit("orders.rs", "fn main() { restored() }\n") .await; + // A further local commit on top: its merge-base with the tracking ref is + // the restored commit, which the destination checks alone must skip. + let extended = checkout.commit("extra.rs", "fn extra() {}\n").await; checkout .git(&["update-ref", "refs/remotes/origin/main", &restored]) .await; - // (the pushed local ref, the destination ref) + // (the pushed local ref, the pushed commit, the destination ref) let pushes = [ // The default branch, pushed from a differently named branch. - ("refs/heads/restore", "refs/heads/main"), + ("refs/heads/restore", &restored, "refs/heads/main"), + ("refs/heads/restore", &extended, "refs/heads/main"), // Another branch the remote also had at the base. - ("refs/heads/feature", "refs/heads/feature"), + ("refs/heads/feature", &restored, "refs/heads/feature"), // A destination that is not a branch. - ("refs/heads/main", "refs/for/main"), + ("refs/heads/main", &restored, "refs/for/main"), + ("refs/heads/main", &extended, "refs/for/main"), ]; - for (local_ref, remote_ref) in pushes { + for (local_ref, pushed, remote_ref) in pushes { uploads.lock().unwrap().clear(); let output = checkout .proofline( @@ -721,10 +726,10 @@ async fn a_pushed_commit_the_tracking_ref_holds_is_reviewed_against_the_commit_t "origin", "https://github.com/acme/web.git", ], - &format!("{local_ref} {restored} {remote_ref} {}\n", checkout.base), + &format!("{local_ref} {pushed} {remote_ref} {}\n", checkout.base), ) .await; - let case = format!("{local_ref} to {remote_ref}"); + let case = format!("{local_ref} at {pushed} to {remote_ref}"); assert_eq!(output.status.code(), Some(0), "{case}: {}", stderr(&output)); let uploads = uploads.lock().unwrap(); assert_eq!(uploads.len(), 1, "{case}: {}", stderr(&output)); @@ -747,6 +752,7 @@ async fn a_push_is_not_reviewed_as_empty_when_the_remote_commit_is_missing() { let restored = checkout .commit("orders.rs", "fn main() { restored() }\n") .await; + let extended = checkout.commit("extra.rs", "fn extra() {}\n").await; checkout .git(&["update-ref", "refs/remotes/origin/main", &restored]) .await; @@ -754,24 +760,49 @@ async fn a_push_is_not_reviewed_as_empty_when_the_remote_commit_is_missing() { .git(&["config", "proofline.hook.pre-push.onIncomplete", "block"]) .await; let missing = "1".repeat(40); - let output = checkout - .proofline( - &origin, - &[ - "hook", - "run", - "pre-push", - "origin", - "https://github.com/acme/web.git", - ], - &format!("refs/heads/feature {restored} refs/heads/feature {missing}\n"), - ) - .await; - let text = stderr(&output); - assert!(uploads.lock().unwrap().is_empty(), "{text}"); - assert!(!text.contains("nothing to review"), "{text}"); - assert!(text.contains("could not review feature"), "{text}"); - assert_eq!(output.status.code(), Some(1), "{text}"); + // (the pushed local ref, the pushed commit, the destination ref). Only a + // branch other than the default may fall back to the tracking ref, and + // not when that would compare the pushed commit with itself. + let pushes = [ + ( + "refs/heads/feature", + &restored, + "refs/heads/feature", + "feature", + ), + ( + "refs/heads/restore", + &extended, + "refs/heads/main", + "restore", + ), + ("refs/heads/main", &extended, "refs/for/main", "main"), + ]; + for (local_ref, pushed, remote_ref, label) in pushes { + uploads.lock().unwrap().clear(); + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("{local_ref} {pushed} {remote_ref} {missing}\n"), + ) + .await; + let text = stderr(&output); + let case = format!("{local_ref} to {remote_ref}: {text}"); + assert!(uploads.lock().unwrap().is_empty(), "{case}"); + assert!(!text.contains("nothing to review"), "{case}"); + assert!( + text.contains(&format!("could not review {label}")), + "{case}" + ); + assert_eq!(output.status.code(), Some(1), "{case}"); + } } #[tokio::test] From 1643c6416594e559df1e11de64e1b2c23da2ed0d Mon Sep 17 00:00:00 2001 From: Utkarsh Srivastava <143518788+Utkarsh-Srivastava29@users.noreply.github.com> Date: Mon, 23 Nov 2026 06:35:17 +0000 Subject: [PATCH 7/7] Compare trees, not commits, before trusting the tracking-ref base The selector fell back to the remote's commit only when the merge-base with the tracking ref was the pushed commit itself. A different commit with the same files, such as an empty commit on the default branch's tip force-pushed over a branch, still produced an empty patch, so a push that removed the branch's changes found nothing to review. The fallback now applies whenever the merge-base has the pushed commit's tree. A missing refs/remotes//HEAD is now read as no default branch before `git symbolic-ref` runs, whatever status it exits with for a missing ref. The refs/for/main cases push from a branch that is not the default, so passing the source ref where the destination belongs fails the tests. Found by a sixth Proofline review of this change. --- src/git.rs | 67 +++++++++++++++++++++++++++++++------------------- tests/hooks.rs | 50 ++++++++++++++++++++++++++++++++++--- 2 files changed, 88 insertions(+), 29 deletions(-) diff --git a/src/git.rs b/src/git.rs index fd7c327..352a180 100644 --- a/src/git.rs +++ b/src/git.rs @@ -394,7 +394,16 @@ pub async fn pushed_branch_base( Err(error) => return Err(error), }; let default = format!("refs/remotes/{remote}/HEAD"); - let default_branch = answer_in(dir, &["symbolic-ref", "--quiet", &default]).await?; + // A missing default ref means no default branch, whatever status + // `symbolic-ref` exits with for it. + let has_default = line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) + .await + .is_ok(); + let default_branch = if has_default { + answer_in(dir, &["symbolic-ref", "--quiet", &default]).await? + } else { + None + }; let to_default = default_branch .as_deref() .and_then(|target| target.strip_prefix(&format!("refs/remotes/{remote}/"))) @@ -407,10 +416,7 @@ pub async fn pushed_branch_base( Err(GitError::UnknownRevision(remote_sha)) }; } - let fork_point = if line_in(dir, &["rev-parse", "--verify", "--quiet", &default]) - .await - .is_ok() - { + let fork_point = if has_default { answer_in(dir, &["merge-base", &head, &default]) .await? .map(|fork_point| commit_sha(&fork_point)) @@ -418,26 +424,37 @@ pub async fn pushed_branch_base( } else { None }; - match fork_point { - // Comparing the pushed commit with itself would review nothing. - Some(fork_point) if fork_point == head => { - if known { - Ok(remote_sha) - } else { - Err(GitError::UnknownRevision(remote_sha)) - } - } - Some(fork_point) if known => { - let ancestor = ["merge-base", "--is-ancestor", &fork_point, &remote_sha]; - Ok(if answer_in(dir, &ancestor).await?.is_some() { - remote_sha - } else { - fork_point - }) - } - Some(fork_point) => Ok(fork_point), - None if known => Ok(remote_sha), - None => Err(GitError::NoBase(remote.to_owned())), + let Some(fork_point) = fork_point else { + return if known { + Ok(remote_sha) + } else { + Err(GitError::NoBase(remote.to_owned())) + }; + }; + // A base with the pushed commit's files would review nothing, even + // where the push changes what the remote has. + let trees = line_in( + dir, + &[ + "rev-parse", + &format!("{fork_point}^{{tree}}"), + &format!("{head}^{{tree}}"), + ], + ) + .await?; + let trees: Vec<&str> = trees.lines().collect(); + if trees.first() == trees.get(1) { + return if known { + Ok(remote_sha) + } else { + Err(GitError::UnknownRevision(remote_sha)) + }; + } + let ancestor = ["merge-base", "--is-ancestor", &fork_point, &remote_sha]; + if known && answer_in(dir, &ancestor).await?.is_some() { + Ok(remote_sha) + } else { + Ok(fork_point) } } diff --git a/tests/hooks.rs b/tests/hooks.rs index e6e15c3..27dd1bf 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -710,9 +710,10 @@ async fn a_pushed_commit_the_tracking_ref_holds_is_reviewed_against_the_commit_t ("refs/heads/restore", &extended, "refs/heads/main"), // Another branch the remote also had at the base. ("refs/heads/feature", &restored, "refs/heads/feature"), - // A destination that is not a branch. - ("refs/heads/main", &restored, "refs/for/main"), - ("refs/heads/main", &extended, "refs/for/main"), + // A destination that is not a branch, from a branch that is not the + // default either. + ("refs/heads/topic", &restored, "refs/for/main"), + ("refs/heads/topic", &extended, "refs/for/main"), ]; for (local_ref, pushed, remote_ref) in pushes { uploads.lock().unwrap().clear(); @@ -776,7 +777,7 @@ async fn a_push_is_not_reviewed_as_empty_when_the_remote_commit_is_missing() { "refs/heads/main", "restore", ), - ("refs/heads/main", &extended, "refs/for/main", "main"), + ("refs/heads/topic", &extended, "refs/for/main", "topic"), ]; for (local_ref, pushed, remote_ref, label) in pushes { uploads.lock().unwrap().clear(); @@ -805,6 +806,47 @@ async fn a_push_is_not_reviewed_as_empty_when_the_remote_commit_is_missing() { } } +#[tokio::test] +async fn a_force_push_that_only_drops_branch_changes_is_reviewed() { + let checkout = Checkout::new().await; + let uploads = Uploads::default(); + let origin = serve(uploads.clone(), "SERIOUS", false).await; + checkout.git(&["checkout", "-q", "-b", "feature"]).await; + let pushed = checkout.commit("feature.rs", "fn feature() {}\n").await; + checkout.git(&["checkout", "-q", "main"]).await; + let main = checkout.commit("teammate.rs", "fn teammate() {}\n").await; + checkout + .git(&["update-ref", "refs/remotes/origin/main", &main]) + .await; + // An empty commit on the default branch's tip replaces the branch: its + // files equal that tip's, but the push removes feature.rs. + checkout.git(&["checkout", "-q", "-b", "replacement"]).await; + checkout + .git(&["commit", "-q", "--allow-empty", "-m", "replace"]) + .await; + let head = checkout.git(&["rev-parse", "HEAD"]).await; + checkout.git(&["checkout", "-q", "main"]).await; + let output = checkout + .proofline( + &origin, + &[ + "hook", + "run", + "pre-push", + "origin", + "https://github.com/acme/web.git", + ], + &format!("refs/heads/replacement {head} refs/heads/feature {pushed}\n"), + ) + .await; + assert_eq!(output.status.code(), Some(0), "{}", stderr(&output)); + let uploads = uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1, "{}", stderr(&output)); + assert_eq!(uploads[0]["base_commit_sha"], pushed); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert!(patch.contains("-fn feature() {}"), "{patch}"); +} + #[tokio::test] async fn a_branch_without_history_in_common_with_the_default_branch_keeps_the_remote_commit() { let checkout = Checkout::new().await;