cli: Disallow patch checkout unless matching

Instead of quietly switching to an old commit, error out if we can't
checkout the patch without overwriting the existing branch.
This commit is contained in:
cloudhead 2024-01-05 11:29:49 +01:00
parent 7169e97bd5
commit 3a3820b06d
No known key found for this signature in database
3 changed files with 29 additions and 23 deletions

View File

@ -68,11 +68,16 @@ Your branch is behind 'rad/patches/6ff4f09c1b5a81347981f59b02ef43a31a07cdae' by
nothing to commit, working tree clean
```
If Bob was to run `rad patch checkout` again, their branch would not
update to the latest commit. This is because it sees that the branch
already exists and does not want overwrite any changes. Bob can choose
to use the `--force` (`-f`) flag to ensure that they are looking at
the latest changes:
If Bob was to run `rad patch checkout` again, it would error.
This is because the branch already exists and `rad` does not want to
overwrite any changes. Bob can choose to use the `--force` (`-f`) flag to
ensure that they are looking at the latest changes:
``` ~bob (fail)
$ rad patch checkout 6ff4f09 --name alice-init
✗ Performing checkout... <canceled>
✗ Error: branch 'alice-init' already exists (use `--force` to overwrite)
```
``` ~bob
$ rad patch checkout 6ff4f09 -f --name alice-init

View File

@ -35,6 +35,15 @@ $ rad patch checkout 6ff4f09
✓ Branch patch/6ff4f09 setup to track rad/patches/6ff4f09c1b5a81347981f59b02ef43a31a07cdae
```
Note that `rad patch checkout` can be used to switch to the patch branch
as long as we haven't made changes to it.
```
$ git checkout master -q
$ rad patch checkout 6ff4f09
✓ Switched to branch patch/6ff4f09
```
Now, let's add a README too!
```
@ -46,22 +55,6 @@ $ git commit --message "Add README, just for the fun"
create mode 100644 README.md
```
But maybe we first wanted to rebase `master` so we ended up being on
that branch:
``` (stderr)
$ git checkout master
Switched to branch 'master'
```
We can be safe in the knowledge that our changes on the
`patch/6ff4f09` branch are still safe:
```
$ rad patch checkout 6ff4f09
✓ Switched to branch patch/6ff4f09
```
We can now finish off the update:
``` (stderr)

View File

@ -54,13 +54,21 @@ pub fn run(
let commit =
match working.find_branch(patch_branch.as_str(), radicle::git::raw::BranchType::Local) {
Ok(branch) if !opts.force => branch.get().peel_to_commit()?,
Ok(branch) => {
Ok(branch) if opts.force => {
let commit = find_patch_commit(revision, stored, working)?;
let mut r = branch.into_reference();
r.set_target(commit.id(), &format!("force update '{patch_branch}'"))?;
commit
}
Ok(branch) => {
let head = branch.get().peel_to_commit()?;
if head.id() != *revision.head() {
anyhow::bail!(
"branch '{patch_branch}' already exists (use `--force` to overwrite)"
);
}
head
}
Err(e) if radicle::git::is_not_found_err(&e) => {
let commit = find_patch_commit(revision, stored, working)?;
// Create patch branch and switch to it.