Skip to content

Commit e7339ca

Browse files
derrickstoleegitster
authored andcommitted
branch: use branch_checked_out() when deleting refs
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fa45f04 commit e7339ca

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

builtin/branch.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
253253
name = mkpathdup(fmt, bname.buf);
254254

255255
if (kinds == FILTER_REFS_BRANCHES) {
256-
const struct worktree *wt =
257-
find_shared_symref(worktrees, "HEAD", name);
258-
if (wt) {
256+
char *path;
257+
if (branch_checked_out(name, &path)) {
259258
error(_("Cannot delete branch '%s' "
260259
"checked out at '%s'"),
261-
bname.buf, wt->path);
260+
bname.buf, path);
261+
free(path);
262262
ret = 1;
263263
continue;
264264
}

t/t2407-worktree-heads.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
2626
for i in 1 2 3 4
2727
do
2828
test_must_fail git branch -f wt-$i HEAD 2>err
29-
grep "cannot force update the branch" err || return 1
29+
grep "cannot force update the branch" err &&
30+
31+
test_must_fail git branch -D wt-$i 2>err
32+
grep "Cannot delete branch" err || return 1
3033
done
3134
'
3235

0 commit comments

Comments
 (0)