7 * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
8 * set is_detached to 1 (0) if the ref is detatched (is not detached).
10 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
11 * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
12 * git_path). Parse the ref ourselves.
14 * return -1 if the ref is not a proper ref, 0 otherwise (success)
16 static int parse_ref(char *path_to_ref
, struct strbuf
*ref
, int *is_detached
)
20 if (!strbuf_readlink(ref
, path_to_ref
, 0)) {
21 /* HEAD is symbolic link */
22 if (!starts_with(ref
->buf
, "refs/") ||
23 check_refname_format(ref
->buf
, 0))
25 } else if (strbuf_read_file(ref
, path_to_ref
, 0) >= 0) {
26 /* textual symref or detached */
27 if (!starts_with(ref
->buf
, "ref:")) {
31 strbuf_remove(ref
, 0, strlen("ref:"));
33 if (check_refname_format(ref
->buf
, 0))
41 static char *find_main_symref(const char *symref
, const char *branch
)
43 struct strbuf sb
= STRBUF_INIT
;
44 struct strbuf path
= STRBUF_INIT
;
45 struct strbuf gitdir
= STRBUF_INIT
;
46 char *existing
= NULL
;
48 strbuf_addf(&path
, "%s/%s", get_git_common_dir(), symref
);
49 if (parse_ref(path
.buf
, &sb
, NULL
) < 0)
51 if (strcmp(sb
.buf
, branch
))
53 strbuf_addstr(&gitdir
, get_git_common_dir());
54 strbuf_strip_suffix(&gitdir
, ".git");
55 existing
= strbuf_detach(&gitdir
, NULL
);
57 strbuf_release(&path
);
59 strbuf_release(&gitdir
);
64 static char *find_linked_symref(const char *symref
, const char *branch
,
67 struct strbuf sb
= STRBUF_INIT
;
68 struct strbuf path
= STRBUF_INIT
;
69 struct strbuf gitdir
= STRBUF_INIT
;
70 char *existing
= NULL
;
73 die("Missing linked worktree name");
75 strbuf_addf(&path
, "%s/worktrees/%s/%s", get_git_common_dir(), id
, symref
);
77 if (parse_ref(path
.buf
, &sb
, NULL
) < 0)
79 if (strcmp(sb
.buf
, branch
))
82 strbuf_addf(&path
, "%s/worktrees/%s/gitdir", get_git_common_dir(), id
);
83 if (strbuf_read_file(&gitdir
, path
.buf
, 0) <= 0)
85 strbuf_rtrim(&gitdir
);
86 strbuf_strip_suffix(&gitdir
, ".git");
88 existing
= strbuf_detach(&gitdir
, NULL
);
90 strbuf_release(&path
);
92 strbuf_release(&gitdir
);
97 char *find_shared_symref(const char *symref
, const char *target
)
99 struct strbuf path
= STRBUF_INIT
;
104 if ((existing
= find_main_symref(symref
, target
)))
107 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
108 dir
= opendir(path
.buf
);
109 strbuf_release(&path
);
113 while ((d
= readdir(dir
)) != NULL
) {
114 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
116 existing
= find_linked_symref(symref
, target
, d
->d_name
);