Skip to content

Commit ce414b3

Browse files
dturner-twgitster
authored andcommitted
refs: make refs/bisect/* per-worktree
We need the place we stick refs for bisects in progress to not be shared between worktrees. So we make the refs/bisect/ hierarchy per-worktree. The is_per_worktree_ref function and associated docs learn that refs/bisect/ is per-worktree, as does the git_path code in path.c The ref-packing functions learn that per-worktree refs should not be packed (since packed-refs is common rather than per-worktree). Since refs/bisect is per-worktree, logs/refs/bisect should be too. Signed-off-by: David Turner <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e09cf2 commit ce414b3

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

Documentation/glossary-content.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,9 @@ exclude;;
413413

414414
[[def_per_worktree_ref]]per-worktree ref::
415415
Refs that are per-<<def_working_tree,worktree>>, rather than
416-
global. This is presently only <<def_HEAD,HEAD>>, but might
417-
later include other unusual refs.
416+
global. This is presently only <<def_HEAD,HEAD>> and any refs
417+
that start with `refs/bisect/`, but might later include other
418+
unusual refs.
418419

419420
[[def_pseudoref]]pseudoref::
420421
Pseudorefs are a class of files under `$GIT_DIR` which behave

path.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ static struct common_dir common_list[] = {
107107
{ 0, 0, 1, "info/sparse-checkout" },
108108
{ 1, 1, 0, "logs" },
109109
{ 1, 1, 1, "logs/HEAD" },
110+
{ 0, 1, 1, "logs/refs/bisect" },
110111
{ 0, 1, 0, "lost-found" },
111112
{ 0, 1, 0, "objects" },
112113
{ 0, 1, 0, "refs" },
114+
{ 0, 1, 1, "refs/bisect" },
113115
{ 0, 1, 0, "remotes" },
114116
{ 0, 1, 0, "worktrees" },
115117
{ 0, 1, 0, "rr-cache" },

refs.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,11 @@ struct ref_entry {
304304
};
305305

306306
static void read_loose_refs(const char *dirname, struct ref_dir *dir);
307+
static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
308+
static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
309+
const char *dirname, size_t len,
310+
int incomplete);
311+
static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
307312

308313
static struct ref_dir *get_ref_dir(struct ref_entry *entry)
309314
{
@@ -312,6 +317,24 @@ static struct ref_dir *get_ref_dir(struct ref_entry *entry)
312317
dir = &entry->u.subdir;
313318
if (entry->flag & REF_INCOMPLETE) {
314319
read_loose_refs(entry->name, dir);
320+
321+
/*
322+
* Manually add refs/bisect, which, being
323+
* per-worktree, might not appear in the directory
324+
* listing for refs/ in the main repo.
325+
*/
326+
if (!strcmp(entry->name, "refs/")) {
327+
int pos = search_ref_dir(dir, "refs/bisect/", 12);
328+
if (pos < 0) {
329+
struct ref_entry *child_entry;
330+
child_entry = create_dir_entry(dir->ref_cache,
331+
"refs/bisect/",
332+
12, 1);
333+
add_entry_to_dir(dir, child_entry);
334+
read_loose_refs("refs/bisect",
335+
&child_entry->u.subdir);
336+
}
337+
}
315338
entry->flag &= ~REF_INCOMPLETE;
316339
}
317340
return dir;
@@ -2649,6 +2672,8 @@ struct pack_refs_cb_data {
26492672
struct ref_to_prune *ref_to_prune;
26502673
};
26512674

2675+
static int is_per_worktree_ref(const char *refname);
2676+
26522677
/*
26532678
* An each_ref_entry_fn that is run over loose references only. If
26542679
* the loose reference can be packed, add an entry in the packed ref
@@ -2662,6 +2687,10 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data)
26622687
struct ref_entry *packed_entry;
26632688
int is_tag_ref = starts_with(entry->name, "refs/tags/");
26642689

2690+
/* Do not pack per-worktree refs: */
2691+
if (is_per_worktree_ref(entry->name))
2692+
return 0;
2693+
26652694
/* ALWAYS pack tags */
26662695
if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
26672696
return 0;
@@ -2856,7 +2885,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
28562885

28572886
static int is_per_worktree_ref(const char *refname)
28582887
{
2859-
return !strcmp(refname, "HEAD");
2888+
return !strcmp(refname, "HEAD") ||
2889+
starts_with(refname, "refs/bisect/");
28602890
}
28612891

28622892
static int is_pseudoref_syntax(const char *refname)

t/t0060-path-utils.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ test_expect_success 'setup common repository' 'git --git-dir=bar init'
266266
test_git_path GIT_COMMON_DIR=bar index .git/index
267267
test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
268268
test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
269+
test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
270+
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
271+
test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
272+
test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
269273
test_git_path GIT_COMMON_DIR=bar objects bar/objects
270274
test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
271275
test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
@@ -276,6 +280,7 @@ test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
276280
test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
277281
test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
278282
test_git_path GIT_COMMON_DIR=bar refs/heads/master bar/refs/heads/master
283+
test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
279284
test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
280285
test_git_path GIT_COMMON_DIR=bar config bar/config
281286
test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs

t/t1400-update-ref.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,4 +1130,23 @@ test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches
11301130
)
11311131
'
11321132

1133+
test_expect_success 'handle per-worktree refs in refs/bisect' '
1134+
git commit --allow-empty -m "initial commit" &&
1135+
git worktree add -b branch worktree &&
1136+
(
1137+
cd worktree &&
1138+
git commit --allow-empty -m "test commit" &&
1139+
git for-each-ref >for-each-ref.out &&
1140+
! grep refs/bisect for-each-ref.out &&
1141+
git update-ref refs/bisect/something HEAD &&
1142+
git rev-parse refs/bisect/something >../worktree-head &&
1143+
git for-each-ref | grep refs/bisect/something
1144+
) &&
1145+
test_path_is_missing .git/refs/bisect &&
1146+
test_must_fail git rev-parse refs/bisect/something &&
1147+
git update-ref refs/bisect/something HEAD &&
1148+
git rev-parse refs/bisect/something >main-head &&
1149+
! test_cmp main-head worktree-head
1150+
'
1151+
11331152
test_done

t/t3210-pack-refs.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,13 @@ test_expect_success 'pack ref directly below refs/' '
160160
test_path_is_missing .git/refs/top
161161
'
162162

163+
test_expect_success 'do not pack ref in refs/bisect' '
164+
git update-ref refs/bisect/local HEAD &&
165+
git pack-refs --all --prune &&
166+
! grep refs/bisect/local .git/packed-refs >/dev/null &&
167+
test_path_is_file .git/refs/bisect/local
168+
'
169+
163170
test_expect_success 'disable reflogs' '
164171
git config core.logallrefupdates false &&
165172
rm -rf .git/logs

0 commit comments

Comments
 (0)