6 static char *find_linked_symref(const char *symref, const char *branch,
9 struct strbuf sb = STRBUF_INIT;
10 struct strbuf path = STRBUF_INIT;
11 struct strbuf gitdir = STRBUF_INIT;
12 char *existing = NULL;
15 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
16 * $GIT_DIR so resolve_ref_unsafe() won't work (it uses
17 * git_path). Parse the ref ourselves.
20 strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
22 strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
24 if (!strbuf_readlink(&sb, path.buf, 0)) {
25 if (!starts_with(sb.buf, "refs/") ||
26 check_refname_format(sb.buf, 0))
28 } else if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
29 starts_with(sb.buf, "ref:")) {
30 strbuf_remove(&sb, 0, strlen("ref:"));
34 if (strcmp(sb.buf, branch))
38 strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
39 if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
41 strbuf_rtrim(&gitdir);
43 strbuf_addstr(&gitdir, get_git_common_dir());
44 strbuf_strip_suffix(&gitdir, ".git");
46 existing = strbuf_detach(&gitdir, NULL);
48 strbuf_release(&path);
50 strbuf_release(&gitdir);
55 char *find_shared_symref(const char *symref, const char *target)
57 struct strbuf path = STRBUF_INIT;
62 if ((existing = find_linked_symref(symref, target, NULL)))
65 strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
66 dir = opendir(path.buf);
67 strbuf_release(&path);
71 while ((d = readdir(dir)) != NULL) {
72 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
74 existing = find_linked_symref(symref, target, d->d_name);