Skip to content

Commit dbb7eae

Browse files
committed
branch: add branch_checked_out() helper
The validate_new_branchname() method contains a check to see if a branch is checked out in any non-bare worktree. This is intended to prevent a force push that will mess up an existing checkout. This helper is not suitable to performing just that check, because the method will die() when the branch is checked out instead of returning an error code. Create a new branch_checked_out() helper that performs the most basic form of this check. To ensure we can call branch_checked_out() in a loop with good performance, do a single preparation step that iterates over all worktrees and stores their current HEAD branches in a strmap. The branch_checked_out() helper can then discover these branches using a hash lookup. This helper is currently missing some key functionality. Namely: it doesn't look for active rebases or bisects which mean that the branch is "checked out" even though HEAD doesn't point to that ref. This functionality will be added in a coming change. We could use branch_checked_out() in validate_new_branchname(), but this missing functionality would be a regression. However, we have no tests that cover this case! Add a new test script that will be expanded with these cross-worktree ref updates. The current tests would still pass if we refactored validate_new_branchname() to use this version of branch_checked_out(). The next change will fix that functionality and add the proper test coverage. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 2668e36 commit dbb7eae

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

branch.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "worktree.h"
1111
#include "submodule-config.h"
1212
#include "run-command.h"
13+
#include "strmap.h"
1314

1415
struct tracking {
1516
struct refspec_item spec;
@@ -369,6 +370,47 @@ int validate_branchname(const char *name, struct strbuf *ref)
369370
return ref_exists(ref->buf);
370371
}
371372

373+
static int initialized_checked_out_branches;
374+
static struct strmap current_checked_out_branches = STRMAP_INIT;
375+
376+
static void prepare_checked_out_branches(void)
377+
{
378+
int i = 0;
379+
struct worktree **worktrees;
380+
381+
if (initialized_checked_out_branches)
382+
return;
383+
initialized_checked_out_branches = 1;
384+
385+
worktrees = get_worktrees();
386+
387+
while (worktrees[i]) {
388+
struct worktree *wt = worktrees[i++];
389+
390+
if (wt->is_bare)
391+
continue;
392+
393+
if (wt->head_ref)
394+
strmap_put(&current_checked_out_branches,
395+
wt->head_ref,
396+
xstrdup(wt->path));
397+
}
398+
399+
free_worktrees(worktrees);
400+
}
401+
402+
int branch_checked_out(const char *refname, char **path)
403+
{
404+
const char *path_in_set;
405+
prepare_checked_out_branches();
406+
407+
path_in_set = strmap_get(&current_checked_out_branches, refname);
408+
if (path_in_set && path)
409+
*path = xstrdup(path_in_set);
410+
411+
return !!path_in_set;
412+
}
413+
372414
/*
373415
* Check if a branch 'name' can be created as a new branch; die otherwise.
374416
* 'force' can be used when it is OK for the named branch already exists.

branch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ void create_branches_recursively(struct repository *r, const char *name,
101101
const char *tracking_name, int force,
102102
int reflog, int quiet, enum branch_track track,
103103
int dry_run);
104+
105+
/*
106+
* Returns true if the branch at 'refname' is checked out at any
107+
* non-bare worktree. The path of the worktree is stored in the
108+
* given 'path', if provided.
109+
*/
110+
int branch_checked_out(const char *refname, char **path);
111+
104112
/*
105113
* Check if 'name' can be a valid name for a branch; die otherwise.
106114
* Return 1 if the named branch already exists; return 0 otherwise.

t/t2407-worktree-heads.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/sh
2+
3+
test_description='test operations trying to overwrite refs at worktree HEAD'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'setup' '
8+
for i in 1 2 3 4
9+
do
10+
test_commit $i &&
11+
git branch wt-$i &&
12+
git worktree add wt-$i wt-$i || return 1
13+
done
14+
'
15+
16+
test_expect_success 'refuse to overwrite: checked out in worktree' '
17+
for i in 1 2 3 4
18+
do
19+
test_must_fail git branch -f wt-$i HEAD 2>err
20+
grep "cannot force update the branch" err || return 1
21+
done
22+
'
23+
24+
test_done

0 commit comments

Comments
 (0)