]> git.ipfire.org Git - thirdparty/git.git/commitdiff
branch: use branch_checked_out() when deleting refs
authorDerrick Stolee <derrickstolee@github.com>
Tue, 14 Jun 2022 19:27:32 +0000 (19:27 +0000)
committerJunio C Hamano <gitster@pobox.com>
Wed, 15 Jun 2022 17:47:19 +0000 (10:47 -0700)
This is the last current use of find_shared_symref() that can easily be
replaced by branch_checked_out(). The benefit of this switch is that the
code is a bit simpler, but also it is faster on repeated calls.

The remaining uses of find_shared_symref() are non-trivial to remove, so
we probably should not continue in that direction:

* builtin/notes.c uses find_shared_symref() with "NOTES_MERGE_REF"
  instead of "HEAD", so it doesn't have an immediate analogue with
  branch_checked_out(). Perhaps we should consider extending it to
  include that symref in addition to HEAD, BISECT_HEAD, and
  REBASE_HEAD.

* receive-pack.c checks to see if a worktree has a checkout for the ref
  that is being updated. The tricky part is that it can actually decide
  to update the worktree directly instead of just skipping the update.
  This all depends on the receive.denyCurrentBranch config option. The
  implementation currenty cares about receiving the worktree in the
  result, so the current branch_checked_out() prototype is insufficient
  currently. This is something to investigate later, though, since a
  large number of refs could be updated at the same time and using the
  strmap implementation of branch_checked_out() could be beneficial.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/branch.c
t/t2407-worktree-heads.sh

index 5d00d0b8d327c5cc048ac0baf997e3670d5ff3df..f875952e7b5b803fa32ebe3e3af70697978c08c9 100644 (file)
@@ -253,12 +253,11 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
                name = mkpathdup(fmt, bname.buf);
 
                if (kinds == FILTER_REFS_BRANCHES) {
-                       const struct worktree *wt =
-                               find_shared_symref(worktrees, "HEAD", name);
-                       if (wt) {
+                       const char *path;
+                       if ((path = branch_checked_out(name))) {
                                error(_("Cannot delete branch '%s' "
                                        "checked out at '%s'"),
-                                     bname.buf, wt->path);
+                                     bname.buf, path);
                                ret = 1;
                                continue;
                        }
index 1fbde05fe2bef021e8c5f396e3720aa031a25eee..a5aec1486c5a3cda713e3c34d0e7070aeebdbc85 100755 (executable)
@@ -37,7 +37,10 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
        for i in 1 2 3 4
        do
                test_must_fail git branch -f wt-$i HEAD 2>err
-               grep "cannot force update the branch" err || return 1
+               grep "cannot force update the branch" err &&
+
+               test_must_fail git branch -D wt-$i 2>err
+               grep "Cannot delete branch" err || return 1
        done
 '