cli: Use git push to open or update patches

This adds the ability to the git-remote-helper to open and update
patches via a `git push` to the magic ref `refs/patches` and
`refs/heads/patches/<patch-id>`.

The main change is that we had to move away from offering the `connect`
capability in the remote helper, to offering `push` and `fetch`
individually.

To get a feel for how this works, see the `rad-patch-via-push.md`
example.
This commit is contained in:
Alexis Sellier
2023-05-12 12:22:08 +02:00
parent f8ee109aaa
commit fd1237b2db
25 changed files with 1198 additions and 131 deletions
+30
View File
@@ -0,0 +1,30 @@
```
$ cd heartwood
$ git branch -r
rad/master
z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/master
```
```
$ git ls-remote z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi 'refs/heads/*'
145e1e69bef3ad93d14946ea212249c2fa9b9828 refs/heads/alice/1
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
```
``` (stderr)
$ git fetch z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new branch] alice/1 -> z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/alice/1
```
```
$ git branch -r
rad/master
z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/alice/1
z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/master
```
```
$ git rev-parse z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/alice/1
145e1e69bef3ad93d14946ea212249c2fa9b9828
```
@@ -0,0 +1,18 @@
Finally, we can also delete branches with `git push`:
```
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi refs/heads/*
145e1e69bef3ad93d14946ea212249c2fa9b9828 refs/heads/alice/1
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
```
``` (stderr) RAD_SOCKET=/dev/null
$ git push rad :alice/1
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
- [deleted] alice/1
```
```
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi refs/heads/*
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
```
+61
View File
@@ -0,0 +1,61 @@
```
$ git checkout -b alice/1
$ git commit -m "Alice's commit" --allow-empty -s
[alice/1 87fa120] Alice's commit
```
``` (stderr) RAD_SOCKET=/dev/null
$ git push rad HEAD:alice/1
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new branch] HEAD -> alice/1
```
Make sure we can't force-push without `+`:
``` (stderr)
$ git commit --amend -m "Alice's amended commit" --allow-empty -s
```
``` (stderr) (fail)
$ git push rad HEAD:alice/1
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
! [rejected] HEAD -> alice/1 (non-fast-forward)
error: failed to push some refs to 'rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
```
And that we can with `+`:
``` (stderr) RAD_SOCKET=/dev/null
$ git push rad +HEAD:alice/1
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
+ 87fa120...145e1e6 HEAD -> alice/1 (forced update)
```
```
$ git branch -r -vv
rad/alice/1 145e1e6 Alice's amended commit
rad/master f2de534 Second commit
```
List our namespaced refs:
```
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi 'refs/heads/*'
145e1e69bef3ad93d14946ea212249c2fa9b9828 refs/heads/alice/1
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
```
List the canonical refs:
```
$ git ls-remote rad
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
```
```
$ rad sync
✓ Synced with 1 node(s)
```
+220
View File
@@ -0,0 +1,220 @@
# Using `git push` to open patches
Let's checkout a branch, make a commit and push to the magic ref `refs/patches`.
When we push to this ref, a patch is created from our commits.
``` (stderr)
$ git checkout -b feature/1
Switched to a new branch 'feature/1'
$ git commit -a -m "Add things" -q --allow-empty
$ git push rad HEAD:refs/patches
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new reference] HEAD -> refs/patches
```
We can see a patch was created:
```
$ rad patch show 8f0e8ec
╭─────────────────────────────────────────────────────────────────────────────────────────╮
│ Title Add things │
│ Patch 8f0e8ecb47a17e8f3219f33150a4092d645e4875 │
│ Author did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi │
│ Head 42d894a83c9c356552a57af09ccdbd5587a99045 │
│ Branches feature/1 │
│ Commits ahead 1, behind 0 │
│ Status open │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [ .. ] │
╰─────────────────────────────────────────────────────────────────────────────────────────╯
```
If we check our local branch, we can see its upstream is set to track a remote
branch associated with this patch:
```
$ git branch -vv
* feature/1 42d894a [rad/patches/8f0e8ecb47a17e8f3219f33150a4092d645e4875] Add things
master f2de534 [rad/master] Second commit
```
Let's check that it's up to date with our local head:
```
$ git status --short --branch
## feature/1...rad/patches/8f0e8ecb47a17e8f3219f33150a4092d645e4875
$ git fetch
$ git push
```
And let's look at our local and remote refs:
```
$ git show-ref
42d894a83c9c356552a57af09ccdbd5587a99045 refs/heads/feature/1
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/remotes/rad/master
42d894a83c9c356552a57af09ccdbd5587a99045 refs/remotes/rad/patches/8f0e8ecb47a17e8f3219f33150a4092d645e4875
```
```
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi 'refs/heads/patches/*'
42d894a83c9c356552a57af09ccdbd5587a99045 refs/heads/patches/8f0e8ecb47a17e8f3219f33150a4092d645e4875
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi 'refs/cobs/*'
8f0e8ecb47a17e8f3219f33150a4092d645e4875 refs/cobs/xyz.radicle.patch/8f0e8ecb47a17e8f3219f33150a4092d645e4875
```
We can also create patches by pushing to the `rad/patches` remote. It's a bit
simpler:
``` (stderr)
$ git checkout -b feature/2 -q
$ git commit -a -m "Add more things" -q --allow-empty
$ git push rad/patches
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new reference] HEAD -> refs/patches
```
We see both branches with upstreams now:
```
$ git branch -vv
feature/1 42d894a [rad/patches/8f0e8ecb47a17e8f3219f33150a4092d645e4875] Add things
* feature/2 b94a835 [rad/patches/8678aafff1d1e28430952abf431e60b87e28023c] Add more things
master f2de534 [rad/master] Second commit
```
And both patches:
```
$ rad patch
╭────────────────────────────────────────────────────────────────────────────────────╮
│ ● ID Title Author Head + - Updated │
├────────────────────────────────────────────────────────────────────────────────────┤
│ ● 8678aaf Add more things z6MknSL…StBU8Vi (you) b94a835 +0 -0 [ ... ] │
│ ● 8f0e8ec Add things z6MknSL…StBU8Vi (you) 42d894a +0 -0 [ ... ] │
╰────────────────────────────────────────────────────────────────────────────────────╯
```
Note that we can't fetch from `rad/patches`:
``` (stderr) (fail)
$ git fetch rad/patches
fatal: couldn't find remote ref refs/patches
```
To update our patch, we simply push commits to the upstream branch:
```
$ git commit -a -m "Improve code" -q --allow-empty
```
``` (stderr)
$ git push
4b6618a6ccb0b406659364a70a00bb60e4cd7cf0
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
b94a835..662843e feature/2 -> patches/8678aafff1d1e28430952abf431e60b87e28023c
```
This last `git push` worked without specifying an upstream branch despite the
local branch having a different name than the remote. This is because Radicle
configures repositories upon `rad init` with `push.default = upstream`:
```
$ git config --local --get push.default
upstream
```
This allows for pushing to the remote patch branch without using the full
`<src>:<dst>` syntax.
We can then see that the patch head has moved:
```
$ rad patch show 8678aaf
╭─────────────────────────────────────────────────────────────────────────────────────────╮
│ Title Add more things │
│ Patch 8678aafff1d1e28430952abf431e60b87e28023c │
│ Author did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi │
│ Head 662843ed81e76efa69d7901fb7bdd775043015d0 │
│ Branches feature/2 │
│ Commits ahead 3, behind 0 │
│ Status open │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) [ ... ] │
│ ↑ updated to 4b6618a6ccb0b406659364a70a00bb60e4cd7cf0 (662843e) [ ... ] │
╰─────────────────────────────────────────────────────────────────────────────────────────╯
```
And we can check that all the refs are properly updated in our repository:
```
$ git rev-parse HEAD
662843ed81e76efa69d7901fb7bdd775043015d0
```
```
$ git status --short --branch
## feature/2...rad/patches/8678aafff1d1e28430952abf431e60b87e28023c
```
```
$ git rev-parse refs/remotes/rad/patches/8678aafff1d1e28430952abf431e60b87e28023c
662843ed81e76efa69d7901fb7bdd775043015d0
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi refs/heads/patches/8678aafff1d1e28430952abf431e60b87e28023c
662843ed81e76efa69d7901fb7bdd775043015d0 refs/heads/patches/8678aafff1d1e28430952abf431e60b87e28023c
```
## Force push
Sometimes, it's necessary to force-push a patch update. For example, if we amended
the commit and want the updated patch to reflect that.
Let's try.
```
$ git commit --amend -m "Amended commit" --allow-empty
[feature/2 3507cd5] Amended commit
Date: [..]
```
Now let's push to the patch head.
``` (stderr) (fail)
$ git push
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
! [rejected] feature/2 -> patches/8678aafff1d1e28430952abf431e60b87e28023c (non-fast-forward)
error: failed to push some refs to 'rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
```
The push fails because it's not a fast-forward update. To remedy this, we can
use `--force` to force the update.
``` (stderr)
$ git push --force
983f2d21c9fbe91c21ddfbcd3e3d6cd13db0a944
To rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
+ 662843e...3507cd5 feature/2 -> patches/8678aafff1d1e28430952abf431e60b87e28023c (forced update)
```
That worked. We can see the new revision if we call `rad patch show`:
```
$ rad patch show 8678aaf
╭─────────────────────────────────────────────────────────────────────────────────────────╮
│ Title Add more things │
│ Patch 8678aafff1d1e28430952abf431e60b87e28023c │
│ Author did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi │
│ Head 3507cd57811fe5f21f6a0a610a1ac8068b3a5d9f │
│ Branches feature/2 │
│ Commits ahead 3, behind 0 │
│ Status open │
├─────────────────────────────────────────────────────────────────────────────────────────┤
│ ● opened by did:key:z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (you) 4 months ago │
│ ↑ updated to 4b6618a6ccb0b406659364a70a00bb60e4cd7cf0 (662843e) 4 months ago │
│ ↑ updated to 983f2d21c9fbe91c21ddfbcd3e3d6cd13db0a944 (3507cd5) 4 months ago │
╰─────────────────────────────────────────────────────────────────────────────────────────╯
```
+4 -3
View File
@@ -9,9 +9,10 @@ Now, we can see that there is a new remote in the list of remotes:
```
$ rad remote list
bob z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (fetch)
rad the canonical upstream (fetch)
rad z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (push)
bob z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk (fetch)
rad (canonical upstream) (fetch)
rad z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi (push)
rad/patches (patches upstream) (push)
```
You can see both `bob` and `rad` as remotes. The `rad` remote is our personal remote of the project.
@@ -62,6 +62,13 @@ $ rad patch show 5f0a547f7a91bf002bb0542035a647fd5af134a5
╰─────────────────────────────────────────────────────────────────────────────────────────╯
```
We can also confirm that the patch branch is in storage:
```
$ git ls-remote rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk refs/heads/flux-capacitor-power
3e674d1a1df90807e934f9ae5da2591dd6848a33 refs/heads/flux-capacitor-power
```
Wait, let's add a README too! Just for fun.
```
@@ -16,7 +16,13 @@ peer. Upcoming versions of radicle will not require this step.
```
$ rad remote add z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --name bob
✓ Remote bob added with rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk
```
``` (stderr)
$ git fetch bob
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk
* [new branch] flux-capacitor-power -> bob/flux-capacitor-power
* [new branch] master -> bob/master
```
The contributor's changes are now visible to us.
+2 -2
View File
@@ -239,9 +239,9 @@ pub fn init(options: Options, profile: &profile::Profile) -> anyhow::Result<()>
// Setup eg. `master` -> `rad/master`
radicle::git::set_upstream(
&repo,
&radicle::rad::REMOTE_NAME,
&*radicle::rad::REMOTE_NAME,
proj.default_branch(),
&radicle::git::refs::workdir::branch(proj.default_branch()),
radicle::git::refs::workdir::branch(proj.default_branch()),
)?;
} else {
push_cmd = format!("git push {}", *radicle::rad::REMOTE_NAME);
+3 -2
View File
@@ -60,11 +60,12 @@ pub fn run(
let (target_ref, target_oid) = get_merge_target(storage, &head_branch)?;
if head_branch.upstream().is_err() {
// TODO(cloudhead): Disable this when invoked from remote helper.
radicle::git::set_upstream(
workdir,
&radicle::rad::REMOTE_NAME,
&*radicle::rad::REMOTE_NAME,
branch_name(&head_branch)?,
&head_branch_name,
head_branch_name,
)?;
}
+15 -5
View File
@@ -1,3 +1,4 @@
use radicle::rad;
use radicle_term::{Element, Table};
use crate::git;
@@ -11,13 +12,22 @@ pub fn run(repo: &git::Repository) -> anyhow::Result<()> {
let Some(url) = url else {
continue;
};
let nid = url.namespace.map_or(
term::format::dim("the canonical upstream".to_string()).italic(),
|namespace| term::format::tertiary(namespace.to_string()),
);
let description = if r.name == rad::PATCHES_REMOTE_NAME.as_str() {
if dir == "fetch" {
// Fetching from the patches remote is not allowed.
continue;
}
term::format::dim("(patches upstream)".to_string()).italic()
} else {
url.namespace.map_or(
term::format::dim("(canonical upstream)".to_string()).italic(),
|namespace| term::format::tertiary(namespace.to_string()),
)
};
table.push([
term::format::bold(r.name.clone()),
nid,
description,
term::format::parens(term::format::secondary(dir.to_owned())),
]);
}
+1 -1
View File
@@ -136,7 +136,7 @@ fn announce(rid: Id, mut node: Node, timeout: time::Duration) -> anyhow::Result<
let mut seeds = seeds.connected().collect::<BTreeSet<_>>();
if seeds.is_empty() {
term::info!("Not connected to any seeds");
term::info!("Not connected to any seeds.");
return Ok(());
}
node.announce_refs(rid)?;
+1 -1
View File
@@ -37,7 +37,7 @@ impl<'a> SetupRemote<'a> {
let local_branch = radicle::git::refs::workdir::branch(
node_ref.join(&self.default_branch).as_refstr(),
);
radicle::git::set_upstream(self.repo, &node.to_string(), &branch_name, &local_branch)?;
radicle::git::set_upstream(self.repo, node.to_string(), &branch_name, local_branch)?;
return Ok(Some((remote, branch_name)));
}
+76
View File
@@ -292,6 +292,28 @@ fn rad_patch_draft() {
.unwrap();
}
#[test]
fn rad_patch_via_push() {
logger::init(log::Level::Debug);
let mut environment = Environment::new();
let profile = environment.profile("alice");
let working = tempfile::tempdir().unwrap();
let home = &profile.home;
// Setup a test repository.
fixtures::repository(working.path());
test("examples/rad-init.md", working.path(), Some(home), []).unwrap();
test(
"examples/rad-patch-via-push.md",
working.path(),
Some(home),
[],
)
.unwrap();
}
#[test]
fn rad_rm() {
let mut environment = Environment::new();
@@ -791,6 +813,60 @@ fn rad_remote() {
.unwrap();
}
#[test]
fn git_push_and_pull() {
logger::init(log::Level::Debug);
let mut environment = Environment::new();
let alice = environment.node("alice");
let bob = environment.node("bob");
let working = environment.tmp().join("working");
fixtures::repository(working.join("alice"));
test(
"examples/rad-init.md",
working.join("alice"),
Some(&alice.home),
[],
)
.unwrap();
let alice = alice.spawn(Config::default());
let mut bob = bob.spawn(Config::default());
bob.connect(&alice).converge([&alice]);
test(
"examples/rad-clone.md",
&working.join("bob"),
Some(&bob.home),
[],
)
.unwrap();
test(
"examples/git/git-push.md",
&working.join("alice"),
Some(&alice.home),
[],
)
.unwrap();
test(
"examples/git/git-pull.md",
&working.join("bob"),
Some(&bob.home),
[],
)
.unwrap();
test(
"examples/git/git-push-delete.md",
&working.join("alice"),
Some(&alice.home),
[],
)
.unwrap();
}
#[test]
fn rad_workflow() {
let mut environment = Environment::new();