7 void free_worktrees(struct worktree
**worktrees
)
11 for (i
= 0; worktrees
[i
]; i
++) {
12 free(worktrees
[i
]->path
);
13 free(worktrees
[i
]->id
);
14 free(worktrees
[i
]->head_ref
);
21 * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
22 * set is_detached to 1 (0) if the ref is detatched (is not detached).
24 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
25 * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
26 * git_path). Parse the ref ourselves.
28 * return -1 if the ref is not a proper ref, 0 otherwise (success)
30 static int parse_ref(char *path_to_ref
, struct strbuf
*ref
, int *is_detached
)
34 if (!strbuf_readlink(ref
, path_to_ref
, 0)) {
35 /* HEAD is symbolic link */
36 if (!starts_with(ref
->buf
, "refs/") ||
37 check_refname_format(ref
->buf
, 0))
39 } else if (strbuf_read_file(ref
, path_to_ref
, 0) >= 0) {
40 /* textual symref or detached */
41 if (!starts_with(ref
->buf
, "ref:")) {
45 strbuf_remove(ref
, 0, strlen("ref:"));
47 if (check_refname_format(ref
->buf
, 0))
56 * Add the head_sha1 and head_ref (if not detached) to the given worktree
58 static void add_head_info(struct strbuf
*head_ref
, struct worktree
*worktree
)
61 if (worktree
->is_detached
) {
62 get_sha1_hex(head_ref
->buf
, worktree
->head_sha1
);
64 resolve_ref_unsafe(head_ref
->buf
, 0, worktree
->head_sha1
, NULL
);
65 worktree
->head_ref
= strbuf_detach(head_ref
, NULL
);
71 * get the main worktree
73 static struct worktree
*get_main_worktree(void)
75 struct worktree
*worktree
= NULL
;
76 struct strbuf path
= STRBUF_INIT
;
77 struct strbuf worktree_path
= STRBUF_INIT
;
78 struct strbuf head_ref
= STRBUF_INIT
;
82 strbuf_addstr(&worktree_path
, absolute_path(get_git_common_dir()));
83 is_bare
= !strbuf_strip_suffix(&worktree_path
, "/.git");
85 strbuf_strip_suffix(&worktree_path
, "/.");
87 strbuf_addf(&path
, "%s/HEAD", get_git_common_dir());
89 if (parse_ref(path
.buf
, &head_ref
, &is_detached
) < 0)
92 worktree
= xmalloc(sizeof(struct worktree
));
93 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
95 worktree
->is_bare
= is_bare
;
96 worktree
->head_ref
= NULL
;
97 worktree
->is_detached
= is_detached
;
98 worktree
->is_current
= 0;
99 add_head_info(&head_ref
, worktree
);
102 strbuf_release(&path
);
103 strbuf_release(&worktree_path
);
104 strbuf_release(&head_ref
);
108 static struct worktree
*get_linked_worktree(const char *id
)
110 struct worktree
*worktree
= NULL
;
111 struct strbuf path
= STRBUF_INIT
;
112 struct strbuf worktree_path
= STRBUF_INIT
;
113 struct strbuf head_ref
= STRBUF_INIT
;
117 die("Missing linked worktree name");
119 strbuf_git_common_path(&path
, "worktrees/%s/gitdir", id
);
120 if (strbuf_read_file(&worktree_path
, path
.buf
, 0) <= 0)
121 /* invalid gitdir file */
124 strbuf_rtrim(&worktree_path
);
125 if (!strbuf_strip_suffix(&worktree_path
, "/.git")) {
126 strbuf_reset(&worktree_path
);
127 strbuf_addstr(&worktree_path
, absolute_path("."));
128 strbuf_strip_suffix(&worktree_path
, "/.");
132 strbuf_addf(&path
, "%s/worktrees/%s/HEAD", get_git_common_dir(), id
);
134 if (parse_ref(path
.buf
, &head_ref
, &is_detached
) < 0)
137 worktree
= xmalloc(sizeof(struct worktree
));
138 worktree
->path
= strbuf_detach(&worktree_path
, NULL
);
139 worktree
->id
= xstrdup(id
);
140 worktree
->is_bare
= 0;
141 worktree
->head_ref
= NULL
;
142 worktree
->is_detached
= is_detached
;
143 worktree
->is_current
= 0;
144 add_head_info(&head_ref
, worktree
);
147 strbuf_release(&path
);
148 strbuf_release(&worktree_path
);
149 strbuf_release(&head_ref
);
153 static void mark_current_worktree(struct worktree
**worktrees
)
155 struct strbuf git_dir
= STRBUF_INIT
;
156 struct strbuf path
= STRBUF_INIT
;
159 strbuf_addstr(&git_dir
, absolute_path(get_git_dir()));
160 for (i
= 0; worktrees
[i
]; i
++) {
161 struct worktree
*wt
= worktrees
[i
];
162 strbuf_addstr(&path
, absolute_path(get_worktree_git_dir(wt
)));
163 wt
->is_current
= !fspathcmp(git_dir
.buf
, path
.buf
);
168 strbuf_release(&git_dir
);
169 strbuf_release(&path
);
172 struct worktree
**get_worktrees(void)
174 struct worktree
**list
= NULL
;
175 struct strbuf path
= STRBUF_INIT
;
178 int counter
= 0, alloc
= 2;
180 list
= xmalloc(alloc
* sizeof(struct worktree
*));
182 if ((list
[counter
] = get_main_worktree()))
185 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
186 dir
= opendir(path
.buf
);
187 strbuf_release(&path
);
189 while ((d
= readdir(dir
)) != NULL
) {
190 struct worktree
*linked
= NULL
;
191 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
194 if ((linked
= get_linked_worktree(d
->d_name
))) {
195 ALLOC_GROW(list
, counter
+ 1, alloc
);
196 list
[counter
++] = linked
;
201 ALLOC_GROW(list
, counter
+ 1, alloc
);
202 list
[counter
] = NULL
;
204 mark_current_worktree(list
);
208 const char *get_worktree_git_dir(const struct worktree
*wt
)
211 return get_git_dir();
213 return get_git_common_dir();
215 return git_common_path("worktrees/%s", wt
->id
);
218 const struct worktree
*find_shared_symref(const char *symref
,
221 const struct worktree
*existing
= NULL
;
222 struct strbuf path
= STRBUF_INIT
;
223 struct strbuf sb
= STRBUF_INIT
;
224 static struct worktree
**worktrees
;
228 free_worktrees(worktrees
);
229 worktrees
= get_worktrees();
231 for (i
= 0; worktrees
[i
]; i
++) {
234 strbuf_addf(&path
, "%s/%s",
235 get_worktree_git_dir(worktrees
[i
]),
238 if (parse_ref(path
.buf
, &sb
, NULL
)) {
242 if (!strcmp(sb
.buf
, target
)) {
243 existing
= worktrees
[i
];
248 strbuf_release(&path
);