From cab56c1113e74c896fa27a6c0991aed538012c1f Mon Sep 17 00:00:00 2001 From: cloudhead Date: Mon, 18 Nov 2024 13:14:27 +0100 Subject: [PATCH] helper: Use the correct head when merging a patch Fixes a bug where the wrong head would be used as part of the merge action. Use the head of the revision instead of the head of the canonical branch. --- radicle-cli/examples/rad-merge-via-push.md | 52 +++++++++++++++++++--- radicle-remote-helper/src/push.rs | 4 +- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/radicle-cli/examples/rad-merge-via-push.md b/radicle-cli/examples/rad-merge-via-push.md index aa6a397b..27aa55cf 100644 --- a/radicle-cli/examples/rad-merge-via-push.md +++ b/radicle-cli/examples/rad-merge-via-push.md @@ -1,16 +1,22 @@ Let's start by creating two patches. -``` (stderr) RAD_SOCKET=/dev/null +``` $ git checkout -b feature/1 -q -$ git commit --allow-empty -q -m "First change" +$ git commit --allow-empty -m "First change" +[feature/1 20aa5dd] First change +``` +``` (stderr) RAD_SOCKET=/dev/null $ git push rad HEAD:refs/patches ✓ Patch 696ec5508494692899337afe6713fe1796d0315c opened To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi * [new reference] HEAD -> refs/patches ``` -``` (stderr) RAD_SOCKET=/dev/null +``` $ git checkout -b feature/2 -q master -$ git commit --allow-empty -q -m "Second change" +$ git commit --allow-empty -m "Second change" +[feature/2 daf349f] Second change +``` +``` (stderr) RAD_SOCKET=/dev/null $ git push rad HEAD:refs/patches ✓ Patch 356f73863a8920455ff6e77cd9c805d68910551b opened To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi @@ -76,6 +82,21 @@ $ rad patch --merged │ ✔ [ ... ] Second change alice (you) - daf349f +0 -0 now │ │ ✔ [ ... ] First change alice (you) - 20aa5dd +0 -0 now │ ╰─────────────────────────────────────────────────────────────────────────────╯ +$ rad patch show 696ec5508494692899337afe6713fe1796d0315c +╭────────────────────────────────────────────────────────────────╮ +│ Title First change │ +│ Patch 696ec5508494692899337afe6713fe1796d0315c │ +│ Author alice (you) │ +│ Head 20aa5dde6210796c3a2f04079b42316a31d02689 │ +│ Branches feature/1 │ +│ Commits ahead 0, behind 2 │ +│ Status merged │ +├────────────────────────────────────────────────────────────────┤ +│ 20aa5dd First change │ +├────────────────────────────────────────────────────────────────┤ +│ ● opened by alice (you) (20aa5dd) now │ +│ └─ ✓ merged by alice (you) at revision 696ec55 (20aa5dd) now │ +╰────────────────────────────────────────────────────────────────╯ $ rad patch show 356f73863a8920455ff6e77cd9c805d68910551b ╭────────────────────────────────────────────────────────────────╮ │ Title Second change │ @@ -89,7 +110,7 @@ $ rad patch show 356f73863a8920455ff6e77cd9c805d68910551b │ daf349f Second change │ ├────────────────────────────────────────────────────────────────┤ │ ● opened by alice (you) (daf349f) now │ -│ └─ ✓ merged by alice (you) at revision 356f738 (d6399c7) now │ +│ └─ ✓ merged by alice (you) at revision 356f738 (daf349f) now │ ╰────────────────────────────────────────────────────────────────╯ ``` @@ -119,3 +140,24 @@ z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi       ├── root       └── sigrefs ``` + +Finally, let's check that we can revert the second patch without affecting +the first patch, even though they were pushed together. + +``` (stderr) RAD_SOCKET=/dev/null +$ git reset --hard HEAD^ +$ git push -f rad +! Patch 356f73863a8920455ff6e77cd9c805d68910551b reverted at revision 356f738 +✓ Canonical head updated to 20aa5dde6210796c3a2f04079b42316a31d02689 +To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi + + d6399c7...20aa5dd master -> master (forced update) +``` +``` +$ rad patch --all +╭─────────────────────────────────────────────────────────────────────────────╮ +│ ● ID Title Author Reviews Head + - Updated │ +├─────────────────────────────────────────────────────────────────────────────┤ +│ ● 356f738 Second change alice (you) - daf349f +0 -0 now │ +│ ✔ 696ec55 First change alice (you) - 20aa5dd +0 -0 now │ +╰─────────────────────────────────────────────────────────────────────────────╯ +``` diff --git a/radicle-remote-helper/src/push.rs b/radicle-remote-helper/src/push.rs index 12c8c09f..a584c0ea 100644 --- a/radicle-remote-helper/src/push.rs +++ b/radicle-remote-helper/src/push.rs @@ -733,9 +733,9 @@ fn patch_merge_all( // It's possible for more than one revision to be merged by this push, so we pick the // revision that is closest to the tip of the commit chain we're pushing. for commit in &commits { - if let Some((revision_id, _)) = revisions.iter().find(|(_, head)| commit == head) { + if let Some((revision_id, head)) = revisions.iter().find(|(_, head)| commit == head) { let patch = patch::PatchMut::new(id, patch, patches); - patch_merge(patch, *revision_id, new, working, signer)?; + patch_merge(patch, *revision_id, *head, working, signer)?; break; }