Skip to content

Commit 917f2cd

Browse files
committed
Merge branch 'nd/rewritten-ref-is-per-worktree'
"git rebase" uses the refs/rewritten/ hierarchy to store its intermediate states, which inherently makes the hierarchy per worktree, but it didn't quite work well. * nd/rewritten-ref-is-per-worktree: Make sure refs/rewritten/ is per-worktree files-backend.c: reduce duplication in add_per_worktree_entries_to_dir() files-backend.c: factor out per-worktree code in loose_fill_ref_dir()
2 parents 1828e52 + b9317d5 commit 917f2cd

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

path.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,13 @@ static struct common_dir common_list[] = {
115115
{ 1, 1, 0, "logs" },
116116
{ 1, 1, 1, "logs/HEAD" },
117117
{ 0, 1, 1, "logs/refs/bisect" },
118+
{ 0, 1, 1, "logs/refs/rewritten" },
119+
{ 0, 1, 1, "logs/refs/worktree" },
118120
{ 0, 1, 0, "lost-found" },
119121
{ 0, 1, 0, "objects" },
120122
{ 0, 1, 0, "refs" },
121123
{ 0, 1, 1, "refs/bisect" },
124+
{ 0, 1, 1, "refs/rewritten" },
122125
{ 0, 1, 1, "refs/worktree" },
123126
{ 0, 1, 0, "remotes" },
124127
{ 0, 1, 0, "worktrees" },

refs/files-backend.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,33 @@ static void files_ref_path(struct files_ref_store *refs,
214214
}
215215
}
216216

217+
/*
218+
* Manually add refs/bisect, refs/rewritten and refs/worktree, which, being
219+
* per-worktree, might not appear in the directory listing for
220+
* refs/ in the main repo.
221+
*/
222+
static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname)
223+
{
224+
const char *prefixes[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" };
225+
int ip;
226+
227+
if (strcmp(dirname, "refs/"))
228+
return;
229+
230+
for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) {
231+
const char *prefix = prefixes[ip];
232+
int prefix_len = strlen(prefix);
233+
struct ref_entry *child_entry;
234+
int pos;
235+
236+
pos = search_ref_dir(dir, prefix, prefix_len);
237+
if (pos >= 0)
238+
continue;
239+
child_entry = create_dir_entry(dir->cache, prefix, prefix_len, 1);
240+
add_entry_to_dir(dir, child_entry);
241+
}
242+
}
243+
217244
/*
218245
* Read the loose references from the namespace dirname into dir
219246
* (without recursing). dirname must end with '/'. dir must be the
@@ -297,28 +324,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
297324
strbuf_release(&path);
298325
closedir(d);
299326

300-
/*
301-
* Manually add refs/bisect and refs/worktree, which, being
302-
* per-worktree, might not appear in the directory listing for
303-
* refs/ in the main repo.
304-
*/
305-
if (!strcmp(dirname, "refs/")) {
306-
int pos = search_ref_dir(dir, "refs/bisect/", 12);
307-
308-
if (pos < 0) {
309-
struct ref_entry *child_entry = create_dir_entry(
310-
dir->cache, "refs/bisect/", 12, 1);
311-
add_entry_to_dir(dir, child_entry);
312-
}
313-
314-
pos = search_ref_dir(dir, "refs/worktree/", 11);
315-
316-
if (pos < 0) {
317-
struct ref_entry *child_entry = create_dir_entry(
318-
dir->cache, "refs/worktree/", 11, 1);
319-
add_entry_to_dir(dir, child_entry);
320-
}
321-
}
327+
add_per_worktree_entries_to_dir(dir, dirname);
322328
}
323329

324330
static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs)

t/t1415-worktree-refs.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,39 @@ test_expect_success 'reflog of worktrees/xx/HEAD' '
7676
test_cmp expected actual.wt2
7777
'
7878

79+
test_expect_success 'for-each-ref from main repo' '
80+
mkdir fer1 &&
81+
git -C fer1 init repo &&
82+
test_commit -C fer1/repo initial &&
83+
git -C fer1/repo worktree add ../second &&
84+
git -C fer1/repo update-ref refs/bisect/main HEAD &&
85+
git -C fer1/repo update-ref refs/rewritten/main HEAD &&
86+
git -C fer1/repo update-ref refs/worktree/main HEAD &&
87+
git -C fer1/repo for-each-ref --format="%(refname)" | grep main >actual &&
88+
cat >expected <<-\EOF &&
89+
refs/bisect/main
90+
refs/rewritten/main
91+
refs/worktree/main
92+
EOF
93+
test_cmp expected actual
94+
'
95+
96+
test_expect_success 'for-each-ref from linked repo' '
97+
mkdir fer2 &&
98+
git -C fer2 init repo &&
99+
test_commit -C fer2/repo initial &&
100+
git -C fer2/repo worktree add ../second &&
101+
git -C fer2/second update-ref refs/bisect/second HEAD &&
102+
git -C fer2/second update-ref refs/rewritten/second HEAD &&
103+
git -C fer2/second update-ref refs/worktree/second HEAD &&
104+
git -C fer2/second for-each-ref --format="%(refname)" | grep second >actual &&
105+
cat >expected <<-\EOF &&
106+
refs/bisect/second
107+
refs/heads/second
108+
refs/rewritten/second
109+
refs/worktree/second
110+
EOF
111+
test_cmp expected actual
112+
'
113+
79114
test_done

0 commit comments

Comments
 (0)