From c65973a9667b4bc34b459a1878eb79fcc1c552bb Mon Sep 17 00:00:00 2001 From: Himanshu Chauhan Date: Mon, 30 Nov 2026 13:38:31 +0530 Subject: [PATCH] Read a type change as the two sections Git prints Git prints a type change, such as a file replaced by a symbolic link, as a deletion and a creation of the same path, but diff_paths recorded one entry for it. The section count then disagreed with the path metadata, so a local review and the commit and push hooks failed with "could not read the local patch's Git path metadata" instead of sending the patch for the server to judge. Record a deletion and a creation, in Git's order, so preflight reads each section against its own path. --- src/git.rs | 14 +++++++++++- tests/local_patch.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/src/git.rs b/src/git.rs index c88f567..098839d 100644 --- a/src/git.rs +++ b/src/git.rs @@ -486,10 +486,22 @@ fn diff_paths(mut output: &[u8]) -> Result<(Vec, &[u8]), GitError> { old: Some(first), new: Some(diff_path(&mut output)?), }, - b'M' | b'T' => PatchPaths { + b'M' => PatchPaths { old: Some(first.clone()), new: Some(first), }, + // Git prints a type change, such as a file replaced by a + // symbolic link, as a deletion followed by a creation. + b'T' => { + paths.push(PatchPaths { + old: Some(first.clone()), + new: None, + }); + PatchPaths { + old: None, + new: Some(first), + } + } _ => return Err(GitError::InvalidDiff), }; paths.push(file); diff --git a/tests/local_patch.rs b/tests/local_patch.rs index 670a586..d092f67 100644 --- a/tests/local_patch.rs +++ b/tests/local_patch.rs @@ -1114,3 +1114,57 @@ async fn local_path_never_expands_a_file_replaced_by_a_directory() { assert_eq!(checkout.index().await, index); server.close().await; } + +#[cfg(unix)] +#[tokio::test] +async fn a_file_replaced_by_a_symlink_uploads_its_two_sections_for_the_server() { + let checkout = Checkout::new(&[("linked.txt", "base\n"), ("target.txt", "target\n")]).await; + tokio::fs::remove_file(checkout.root().join("linked.txt")) + .await + .unwrap(); + tokio::fs::symlink("target.txt", checkout.root().join("linked.txt")) + .await + .unwrap(); + for flags in [vec![], vec!["--path", "linked.txt"]] { + let server = Server::start(true).await; + let output = checkout.review(&server, &[], &flags).await; + assert!( + output.status.success(), + "{flags:?}: {}", + String::from_utf8_lossy(&output.stderr) + ); + { + let uploads = server.fixture.uploads.lock().unwrap(); + assert_eq!(uploads.len(), 1); + let patch = uploads[0]["patch"].as_str().unwrap(); + assert_eq!( + patch + .matches("diff --git a/linked.txt b/linked.txt") + .count(), + 2, + "{patch}" + ); + assert!(patch.contains("deleted file mode 100644"), "{patch}"); + assert!(patch.contains("new file mode 120000"), "{patch}"); + } + server.close().await; + } +} + +#[cfg(unix)] +#[tokio::test] +async fn a_secret_in_a_file_replaced_by_a_symlink_is_still_refused() { + let secret = "-----BEGIN PRIVATE KEY-----"; + let checkout = Checkout::new(&[("key.pem", "base\n")]).await; + checkout.git(&["rm", "-q", "key.pem"]).await; + tokio::fs::symlink(secret, checkout.root().join("key.pem")) + .await + .unwrap(); + let server = Server::start(true).await; + let output = checkout + .review(&server, &[], &["--include-untracked"]) + .await; + assert_refusal(&output, "key.pem", secret); + server.assert_no_requests(); + server.close().await; +}