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 <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Aug 31, 2015 at 22:13 UTC ce414b33ec0385a2aa701b6eaa712695ac13717a
6 files changed +67 -3
Documentation/glossary-content.txt
+3 -2
@@ -413,8 +413,9 @@ exclude;;
413
414 [[def_per_worktree_ref]]per-worktree ref::
415 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.
419
420 [[def_pseudoref]]pseudoref::
421 Pseudorefs are a class of files under `$GIT_DIR` which behave
path.c
+2
@@ -107,9 +107,11 @@ static struct common_dir common_list[] = {
107 { 0, 0, 1, "info/sparse-checkout" },
108 { 1, 1, 0, "logs" },
109 { 1, 1, 1, "logs/HEAD" },
110 + { 0, 1, 1, "logs/refs/bisect" },
111 { 0, 1, 0, "lost-found" },
112 { 0, 1, 0, "objects" },
113 { 0, 1, 0, "refs" },
114 + { 0, 1, 1, "refs/bisect" },
115 { 0, 1, 0, "remotes" },
116 { 0, 1, 0, "worktrees" },
117 { 0, 1, 0, "rr-cache" },
refs.c
+31 -1
@@ -304,6 +304,11 @@ struct ref_entry {
304 };
305
306 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);
312
313 static struct ref_dir *get_ref_dir(struct ref_entry *entry)
314 {
@@ -312,6 +317,24 @@ static struct ref_dir *get_ref_dir(struct ref_entry *entry)
317 dir = &entry->u.subdir;
318 if (entry->flag & REF_INCOMPLETE) {
319 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 + }
338 entry->flag &= ~REF_INCOMPLETE;
339 }
340 return dir;
@@ -2649,6 +2672,8 @@ struct pack_refs_cb_data {
2672 struct ref_to_prune *ref_to_prune;
2673 };
2674
2675 +static int is_per_worktree_ref(const char *refname);
2676 +
2677 /*
2678 * An each_ref_entry_fn that is run over loose references only. If
2679 * 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)
2687 struct ref_entry *packed_entry;
2688 int is_tag_ref = starts_with(entry->name, "refs/tags/");
2689
2690 + /* Do not pack per-worktree refs: */
2691 + if (is_per_worktree_ref(entry->name))
2692 + return 0;
2693 +
2694 /* ALWAYS pack tags */
2695 if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref)
2696 return 0;
@@ -2856,7 +2885,8 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
2885
2886 static int is_per_worktree_ref(const char *refname)
2887 {
2859 - return !strcmp(refname, "HEAD");
2888 + return !strcmp(refname, "HEAD") ||
2889 + starts_with(refname, "refs/bisect/");
2890 }
2891
2892 static int is_pseudoref_syntax(const char *refname)
t/t0060-path-utils.sh
+5
@@ -266,6 +266,10 @@ test_expect_success 'setup common repository' 'git --git-dir=bar init'
266 test_git_path GIT_COMMON_DIR=bar index .git/index
267 test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
268 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
273 test_git_path GIT_COMMON_DIR=bar objects bar/objects
274 test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
275 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
280 test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
281 test_git_path GIT_COMMON_DIR=bar logs/refs/heads/master bar/logs/refs/heads/master
282 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
284 test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
285 test_git_path GIT_COMMON_DIR=bar config bar/config
286 test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
t/t1400-update-ref.sh
+19
@@ -1130,4 +1130,23 @@ test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches
1130 )
1131 '
1132
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 +
1152 test_done
t/t3210-pack-refs.sh
+7
@@ -160,6 +160,13 @@ test_expect_success 'pack ref directly below refs/' '
160 test_path_is_missing .git/refs/top
161 '
162
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 +
170 test_expect_success 'disable reflogs' '
171 git config core.logallrefupdates false &&
172 rm -rf .git/logs