diff --git a/radicle-cli/examples/rad-patch-checkout-force.md b/radicle-cli/examples/rad-patch-checkout-force.md index 3f69abf6..172935dd 100644 --- a/radicle-cli/examples/rad-patch-checkout-force.md +++ b/radicle-cli/examples/rad-patch-checkout-force.md @@ -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... +✗ Error: branch 'alice-init' already exists (use `--force` to overwrite) +``` ``` ~bob $ rad patch checkout 6ff4f09 -f --name alice-init diff --git a/radicle-cli/examples/rad-patch-checkout.md b/radicle-cli/examples/rad-patch-checkout.md index d2a089bc..73714a93 100644 --- a/radicle-cli/examples/rad-patch-checkout.md +++ b/radicle-cli/examples/rad-patch-checkout.md @@ -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) diff --git a/radicle-cli/src/commands/patch/checkout.rs b/radicle-cli/src/commands/patch/checkout.rs index b43d9239..4b35fcd0 100644 --- a/radicle-cli/src/commands/patch/checkout.rs +++ b/radicle-cli/src/commands/patch/checkout.rs @@ -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.