worktrees: add find_shared_symref

Add a new function, find_shared_symref, which contains the heart of die_if_checked_out, but works for any symref, not just HEAD. Refactor die_if_checked_out to use the same infrastructure as find_shared_symref. Soon, we will use find_shared_symref to protect notes merges in worktrees. Signed-off-by: David Turner <dturner@twopensource.com> Reviewed-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Aug 10, 2015 at 13:52 UTC 41af65651d4333cd19ab4af3509fa2d08f3816ed
2 files changed +42 -12
branch.c
+34 -12
@@ -310,21 +310,23 @@ void remove_branch_state(void)
310 unlink(git_path("SQUASH_MSG"));
311 }
312
313 -static void check_linked_checkout(const char *branch, const char *id)
313 +static char *find_linked_symref(const char *symref, const char *branch,
314 + const char *id)
315 {
316 struct strbuf sb = STRBUF_INIT;
317 struct strbuf path = STRBUF_INIT;
318 struct strbuf gitdir = STRBUF_INIT;
319 + char *existing = NULL;
320
321 /*
320 - * $GIT_COMMON_DIR/HEAD is practically outside
321 - * $GIT_DIR so resolve_ref_unsafe() won't work (it
322 - * uses git_path). Parse the ref ourselves.
322 + * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
323 + * $GIT_DIR so resolve_ref_unsafe() won't work (it uses
324 + * git_path). Parse the ref ourselves.
325 */
326 if (id)
325 - strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
327 + strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
328 else
327 - strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
329 + strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
330
331 if (!strbuf_readlink(&sb, path.buf, 0)) {
332 if (!starts_with(sb.buf, "refs/") ||
@@ -346,33 +348,53 @@ static void check_linked_checkout(const char *branch, const char *id)
348 strbuf_rtrim(&gitdir);
349 } else
350 strbuf_addstr(&gitdir, get_git_common_dir());
349 - skip_prefix(branch, "refs/heads/", &branch);
351 strbuf_strip_suffix(&gitdir, ".git");
351 - die(_("'%s' is already checked out at '%s'"), branch, gitdir.buf);
352 +
353 + existing = strbuf_detach(&gitdir, NULL);
354 done:
355 strbuf_release(&path);
356 strbuf_release(&sb);
357 strbuf_release(&gitdir);
358 +
359 + return existing;
360 }
361
358 -void die_if_checked_out(const char *branch)
362 +char *find_shared_symref(const char *symref, const char *target)
363 {
364 struct strbuf path = STRBUF_INIT;
365 DIR *dir;
366 struct dirent *d;
367 + char *existing;
368
364 - check_linked_checkout(branch, NULL);
369 + if ((existing = find_linked_symref(symref, target, NULL)))
370 + return existing;
371
372 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
373 dir = opendir(path.buf);
374 strbuf_release(&path);
375 if (!dir)
370 - return;
376 + return NULL;
377
378 while ((d = readdir(dir)) != NULL) {
379 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
380 continue;
375 - check_linked_checkout(branch, d->d_name);
381 + existing = find_linked_symref(symref, target, d->d_name);
382 + if (existing)
383 + goto done;
384 }
385 +done:
386 closedir(dir);
387 +
388 + return existing;
389 +}
390 +
391 +void die_if_checked_out(const char *branch)
392 +{
393 + char *existing;
394 +
395 + existing = find_shared_symref("HEAD", branch);
396 + if (existing) {
397 + skip_prefix(branch, "refs/heads/", &branch);
398 + die(_("'%s' is already checked out at '%s'"), branch, existing);
399 + }
400 }
branch.h
+8
@@ -59,4 +59,12 @@ extern int read_branch_desc(struct strbuf *, const char *branch_name);
59 */
60 extern void die_if_checked_out(const char *branch);
61
62 +/*
63 + * Check if a per-worktree symref points to a ref in the main worktree
64 + * or any linked worktree, and return the path to the exising worktree
65 + * if it is. Returns NULL if there is no existing ref. The caller is
66 + * responsible for freeing the returned path.
67 + */
68 +extern char *find_shared_symref(const char *symref, const char *target);
69 +
70 #endif