Skip to content

Commit 4540dbe

Browse files
committed
fetch: use new branch_checked_out() and add tests
When fetching refs from a remote, it is possible that the refspec will cause use to overwrite a ref that is checked out in a worktree. The existing logic in builtin/fetch.c uses a possibly-slow mechanism. Update those sections to use the new, more efficient branch_checked_out() helper. These uses were not previously tested, so add a test case that can be used for these kinds of collisions. There is only one test now, but more tests will be added as other consumers of branch_checked_out() are added. Note that there are two uses in builtin/fetch.c, but only one of the messages is tested. This is because the tested check is run before completing the fetch, and the untested check is not reachable without concurrent updates to the filesystem. Thus, it is beneficial to keep that extra check for the sake of defense-in-depth. However, we should not attempt to test the check, as the effort required is too complicated to be worth the effort. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 18bad9b commit 4540dbe

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

builtin/fetch.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ static int update_local_ref(struct ref *ref,
885885
struct worktree **worktrees)
886886
{
887887
struct commit *current = NULL, *updated;
888-
const struct worktree *wt;
888+
char *path = NULL;
889889
const char *pretty_ref = prettify_refname(ref->name);
890890
int fast_forward = 0;
891891

@@ -900,17 +900,17 @@ static int update_local_ref(struct ref *ref,
900900
}
901901

902902
if (!update_head_ok &&
903-
(wt = find_shared_symref(worktrees, "HEAD", ref->name)) &&
904-
!wt->is_bare && !is_null_oid(&ref->old_oid)) {
903+
!is_null_oid(&ref->old_oid) &&
904+
branch_checked_out(ref->name, &path)) {
905905
/*
906906
* If this is the head, and it's not okay to update
907907
* the head, and the old value of the head isn't empty...
908908
*/
909909
format_display(display, '!', _("[rejected]"),
910-
wt->is_current ?
911-
_("can't fetch in current branch") :
912-
_("checked out in another worktree"),
910+
path ? _("can't fetch in current branch") :
911+
_("checked out in another worktree"),
913912
remote, pretty_ref, summary_width);
913+
free(path);
914914
return 1;
915915
}
916916

@@ -1434,19 +1434,16 @@ static int prune_refs(struct refspec *rs,
14341434
return result;
14351435
}
14361436

1437-
static void check_not_current_branch(struct ref *ref_map,
1438-
struct worktree **worktrees)
1437+
static void check_not_current_branch(struct ref *ref_map)
14391438
{
1440-
const struct worktree *wt;
1439+
char *path;
14411440
for (; ref_map; ref_map = ref_map->next)
14421441
if (ref_map->peer_ref &&
14431442
starts_with(ref_map->peer_ref->name, "refs/heads/") &&
1444-
(wt = find_shared_symref(worktrees, "HEAD",
1445-
ref_map->peer_ref->name)) &&
1446-
!wt->is_bare)
1443+
branch_checked_out(ref_map->peer_ref->name, &path))
14471444
die(_("refusing to fetch into branch '%s' "
14481445
"checked out at '%s'"),
1449-
ref_map->peer_ref->name, wt->path);
1446+
ref_map->peer_ref->name, path);
14501447
}
14511448

14521449
static int truncate_fetch_head(void)
@@ -1650,7 +1647,7 @@ static int do_fetch(struct transport *transport,
16501647
ref_map = get_ref_map(transport->remote, remote_refs, rs,
16511648
tags, &autotags);
16521649
if (!update_head_ok)
1653-
check_not_current_branch(ref_map, worktrees);
1650+
check_not_current_branch(ref_map);
16541651

16551652
retcode = open_fetch_head(&fetch_head);
16561653
if (retcode)

t/t2407-worktree-heads.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ test_expect_success 'setup' '
1010
test_commit $i &&
1111
git branch wt-$i &&
1212
git worktree add wt-$i wt-$i || return 1
13+
done &&
14+
15+
# Create a server that updates each branch by one commit
16+
git clone . server &&
17+
git remote add server ./server &&
18+
for i in 1 2 3 4
19+
do
20+
git -C server checkout wt-$i &&
21+
test_commit -C server A-$i || return 1
1322
done
1423
'
1524

@@ -21,6 +30,16 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
2130
done
2231
'
2332

33+
test_expect_success 'refuse to overwrite during fetch' '
34+
test_must_fail git fetch server +refs/heads/wt-3:refs/heads/wt-3 2>err &&
35+
grep "refusing to fetch into branch '\''refs/heads/wt-3'\''" err &&
36+
37+
# General fetch into refs/heads/ will fail on first ref,
38+
# so use a generic error message check.
39+
test_must_fail git fetch server +refs/heads/*:refs/heads/* 2>err &&
40+
grep "refusing to fetch into branch" err
41+
'
42+
2443
test_expect_success 'refuse to overwrite: worktree in bisect' '
2544
test_when_finished test_might_fail git -C wt-4 bisect reset &&
2645
@@ -31,7 +50,10 @@ test_expect_success 'refuse to overwrite: worktree in bisect' '
3150
) &&
3251
3352
test_must_fail git branch -f wt-4 HEAD 2>err &&
34-
grep "cannot force update the branch '\''wt-4'\'' checked out at" err
53+
grep "cannot force update the branch '\''wt-4'\'' checked out at" err &&
54+
55+
test_must_fail git fetch server +refs/heads/wt-4:refs/heads/wt-4 2>err &&
56+
grep "refusing to fetch into branch '\''refs/heads/wt-4'\''" err
3557
'
3658

3759
. "$TEST_DIRECTORY"/lib-rebase.sh
@@ -47,7 +69,10 @@ test_expect_success 'refuse to overwrite: worktree in rebase' '
4769
) &&
4870
4971
test_must_fail git branch -f wt-4 HEAD 2>err &&
50-
grep "cannot force update the branch '\''wt-4'\'' checked out at" err
72+
grep "cannot force update the branch '\''wt-4'\'' checked out at" err &&
73+
74+
test_must_fail git fetch server +refs/heads/wt-4:refs/heads/wt-4 2>err &&
75+
grep "refusing to fetch into branch '\''refs/heads/wt-4'\''" err
5176
'
5277

5378
test_done

0 commit comments

Comments
 (0)