]>
Commit | Line | Data |
---|---|---|
45f2b7a4 PS |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
dea7488e PS |
8 | #include "common.h" |
9 | ||
10 | #include "git2/branch.h" | |
11 | #include "git2/commit.h" | |
45f2b7a4 PS |
12 | #include "git2/worktree.h" |
13 | ||
45f2b7a4 | 14 | #include "repository.h" |
d3bc09e8 | 15 | #include "worktree.h" |
45f2b7a4 | 16 | |
3e9c5d8a | 17 | static bool is_worktree_dir(const char *dir) |
45f2b7a4 | 18 | { |
3e9c5d8a PS |
19 | git_buf buf = GIT_BUF_INIT; |
20 | int error; | |
21 | ||
22 | if (git_buf_sets(&buf, dir) < 0) | |
23 | return -1; | |
24 | ||
25 | error = git_path_contains_file(&buf, "commondir") | |
26 | && git_path_contains_file(&buf, "gitdir") | |
27 | && git_path_contains_file(&buf, "HEAD"); | |
28 | ||
29 | git_buf_free(&buf); | |
30 | return error; | |
45f2b7a4 PS |
31 | } |
32 | ||
33 | int git_worktree_list(git_strarray *wts, git_repository *repo) | |
34 | { | |
35 | git_vector worktrees = GIT_VECTOR_INIT; | |
36 | git_buf path = GIT_BUF_INIT; | |
37 | char *worktree; | |
38 | unsigned i, len; | |
39 | int error; | |
40 | ||
41 | assert(wts && repo); | |
42 | ||
43 | wts->count = 0; | |
44 | wts->strings = NULL; | |
45 | ||
46 | if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0) | |
47 | goto exit; | |
48 | if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr)) | |
49 | goto exit; | |
50 | if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0) | |
51 | goto exit; | |
52 | ||
53 | len = path.size; | |
54 | ||
55 | git_vector_foreach(&worktrees, i, worktree) { | |
56 | git_buf_truncate(&path, len); | |
57 | git_buf_puts(&path, worktree); | |
58 | ||
3e9c5d8a | 59 | if (!is_worktree_dir(path.ptr)) { |
45f2b7a4 PS |
60 | git_vector_remove(&worktrees, i); |
61 | git__free(worktree); | |
62 | } | |
63 | } | |
64 | ||
65 | wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees); | |
66 | ||
67 | exit: | |
68 | git_buf_free(&path); | |
69 | ||
70 | return error; | |
71 | } | |
d3bc09e8 | 72 | |
39abd3ad | 73 | char *git_worktree__read_link(const char *base, const char *file) |
d3bc09e8 PS |
74 | { |
75 | git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT; | |
76 | ||
77 | assert(base && file); | |
78 | ||
79 | if (git_buf_joinpath(&path, base, file) < 0) | |
80 | goto err; | |
81 | if (git_futils_readbuffer(&buf, path.ptr) < 0) | |
82 | goto err; | |
83 | git_buf_free(&path); | |
84 | ||
85 | git_buf_rtrim(&buf); | |
86 | ||
87 | if (!git_path_is_relative(buf.ptr)) | |
88 | return git_buf_detach(&buf); | |
89 | ||
90 | if (git_buf_sets(&path, base) < 0) | |
91 | goto err; | |
92 | if (git_path_apply_relative(&path, buf.ptr) < 0) | |
93 | goto err; | |
94 | git_buf_free(&buf); | |
95 | ||
96 | return git_buf_detach(&path); | |
97 | ||
98 | err: | |
99 | git_buf_free(&buf); | |
100 | git_buf_free(&path); | |
101 | ||
102 | return NULL; | |
103 | } | |
104 | ||
dea7488e PS |
105 | static int write_wtfile(const char *base, const char *file, const git_buf *buf) |
106 | { | |
107 | git_buf path = GIT_BUF_INIT; | |
108 | int err; | |
109 | ||
110 | assert(base && file && buf); | |
111 | ||
112 | if ((err = git_buf_joinpath(&path, base, file)) < 0) | |
113 | goto out; | |
114 | ||
115 | if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) | |
116 | goto out; | |
117 | ||
118 | out: | |
119 | git_buf_free(&path); | |
120 | ||
121 | return err; | |
122 | } | |
123 | ||
dfc98706 | 124 | static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name) |
d3bc09e8 | 125 | { |
dfc98706 | 126 | git_buf gitdir = GIT_BUF_INIT; |
d3bc09e8 | 127 | git_worktree *wt = NULL; |
dfc98706 | 128 | int error = 0; |
d3bc09e8 | 129 | |
dfc98706 | 130 | if (!is_worktree_dir(dir)) { |
d3bc09e8 PS |
131 | error = -1; |
132 | goto out; | |
133 | } | |
134 | ||
dfc98706 | 135 | if ((wt = git__calloc(1, sizeof(struct git_repository))) == NULL) { |
d3bc09e8 PS |
136 | error = -1; |
137 | goto out; | |
138 | } | |
139 | ||
140 | if ((wt->name = git__strdup(name)) == NULL | |
dfc98706 PS |
141 | || (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL |
142 | || (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL | |
143 | || (wt->parent_path = git__strdup(parent)) == NULL) { | |
d3bc09e8 PS |
144 | error = -1; |
145 | goto out; | |
146 | } | |
dfc98706 PS |
147 | |
148 | if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0) | |
149 | goto out; | |
150 | wt->gitdir_path = git_buf_detach(&gitdir); | |
151 | ||
2a503485 | 152 | wt->locked = !!git_worktree_is_locked(NULL, wt); |
d3bc09e8 | 153 | |
dfc98706 PS |
154 | *out = wt; |
155 | ||
156 | out: | |
157 | if (error) | |
158 | git_worktree_free(wt); | |
159 | git_buf_free(&gitdir); | |
160 | ||
161 | return error; | |
162 | } | |
163 | ||
164 | int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name) | |
165 | { | |
166 | git_buf path = GIT_BUF_INIT; | |
167 | git_worktree *wt = NULL; | |
168 | int error; | |
169 | ||
170 | assert(repo && name); | |
171 | ||
172 | *out = NULL; | |
173 | ||
174 | if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0) | |
175 | goto out; | |
176 | ||
20a368e2 | 177 | if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0) |
dfc98706 | 178 | goto out; |
d3bc09e8 PS |
179 | |
180 | out: | |
181 | git_buf_free(&path); | |
182 | ||
183 | if (error) | |
184 | git_worktree_free(wt); | |
185 | ||
186 | return error; | |
187 | } | |
188 | ||
3017ba94 PS |
189 | int git_worktree_open_from_repository(git_worktree **out, git_repository *repo) |
190 | { | |
191 | git_buf parent = GIT_BUF_INIT; | |
192 | const char *gitdir, *commondir; | |
193 | char *name = NULL; | |
194 | int error = 0; | |
195 | ||
196 | if (!git_repository_is_worktree(repo)) { | |
197 | giterr_set(GITERR_WORKTREE, "cannot open worktree of a non-worktree repo"); | |
198 | error = -1; | |
199 | goto out; | |
200 | } | |
201 | ||
202 | gitdir = git_repository_path(repo); | |
203 | commondir = git_repository_commondir(repo); | |
204 | ||
20a368e2 | 205 | if ((error = git_path_prettify_dir(&parent, "..", commondir)) < 0) |
3017ba94 PS |
206 | goto out; |
207 | ||
208 | /* The name is defined by the last component in '.git/worktree/%s' */ | |
209 | name = git_path_basename(gitdir); | |
210 | ||
211 | if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0) | |
212 | goto out; | |
213 | ||
214 | out: | |
215 | if (error) | |
216 | free(name); | |
217 | git_buf_free(&parent); | |
218 | ||
219 | return error; | |
220 | } | |
221 | ||
d3bc09e8 PS |
222 | void git_worktree_free(git_worktree *wt) |
223 | { | |
224 | if (!wt) | |
225 | return; | |
226 | ||
227 | git__free(wt->commondir_path); | |
228 | git__free(wt->gitlink_path); | |
229 | git__free(wt->gitdir_path); | |
230 | git__free(wt->parent_path); | |
231 | git__free(wt->name); | |
232 | git__free(wt); | |
233 | } | |
372dc9ff PS |
234 | |
235 | int git_worktree_validate(const git_worktree *wt) | |
236 | { | |
237 | git_buf buf = GIT_BUF_INIT; | |
238 | int err = 0; | |
239 | ||
240 | assert(wt); | |
241 | ||
242 | git_buf_puts(&buf, wt->gitdir_path); | |
3e9c5d8a | 243 | if (!is_worktree_dir(buf.ptr)) { |
372dc9ff PS |
244 | giterr_set(GITERR_WORKTREE, |
245 | "Worktree gitdir ('%s') is not valid", | |
246 | wt->gitlink_path); | |
247 | err = -1; | |
248 | goto out; | |
249 | } | |
250 | ||
251 | if (!git_path_exists(wt->parent_path)) { | |
252 | giterr_set(GITERR_WORKTREE, | |
253 | "Worktree parent directory ('%s') does not exist ", | |
254 | wt->parent_path); | |
255 | err = -2; | |
256 | goto out; | |
257 | } | |
258 | ||
259 | if (!git_path_exists(wt->commondir_path)) { | |
260 | giterr_set(GITERR_WORKTREE, | |
261 | "Worktree common directory ('%s') does not exist ", | |
262 | wt->commondir_path); | |
263 | err = -3; | |
264 | goto out; | |
265 | } | |
266 | ||
267 | out: | |
268 | git_buf_free(&buf); | |
269 | ||
270 | return err; | |
271 | } | |
dea7488e PS |
272 | |
273 | int git_worktree_add(git_worktree **out, git_repository *repo, const char *name, const char *worktree) | |
274 | { | |
7cf7a407 | 275 | git_buf gitdir = GIT_BUF_INIT, buf = GIT_BUF_INIT; |
dea7488e PS |
276 | git_reference *ref = NULL, *head = NULL; |
277 | git_commit *commit = NULL; | |
278 | git_repository *wt = NULL; | |
279 | git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT; | |
280 | int err; | |
281 | ||
282 | assert(out && repo && name && worktree); | |
283 | ||
284 | *out = NULL; | |
285 | ||
7cf7a407 PS |
286 | /* Create gitdir directory ".git/worktrees/<name>" */ |
287 | if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0) | |
dea7488e | 288 | goto out; |
7cf7a407 PS |
289 | if (!git_path_exists(gitdir.ptr)) |
290 | if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0) | |
dea7488e | 291 | goto out; |
7cf7a407 | 292 | if ((err = git_buf_joinpath(&gitdir, gitdir.ptr, name)) < 0) |
dea7488e | 293 | goto out; |
7cf7a407 | 294 | if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0) |
dea7488e PS |
295 | goto out; |
296 | ||
297 | /* Create worktree work dir */ | |
298 | if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0) | |
299 | goto out; | |
300 | ||
301 | /* Create worktree .git file */ | |
7cf7a407 | 302 | if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0) |
dea7488e PS |
303 | goto out; |
304 | if ((err = write_wtfile(worktree, ".git", &buf)) < 0) | |
305 | goto out; | |
306 | ||
7cf7a407 | 307 | /* Create gitdir files */ |
dea7488e PS |
308 | if ((err = git_buf_sets(&buf, repo->commondir)) < 0 |
309 | || (err = git_buf_putc(&buf, '\n')) < 0 | |
7cf7a407 | 310 | || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0) |
dea7488e PS |
311 | goto out; |
312 | if ((err = git_buf_joinpath(&buf, worktree, ".git")) < 0 | |
313 | || (err = git_buf_putc(&buf, '\n')) < 0 | |
7cf7a407 | 314 | || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0) |
dea7488e PS |
315 | goto out; |
316 | ||
317 | /* Create new branch */ | |
318 | if ((err = git_repository_head(&head, repo)) < 0) | |
319 | goto out; | |
320 | if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0) | |
321 | goto out; | |
322 | if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0) | |
323 | goto out; | |
324 | ||
325 | /* Set worktree's HEAD */ | |
7cf7a407 | 326 | if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0) |
dea7488e PS |
327 | goto out; |
328 | if ((err = git_repository_open(&wt, worktree)) < 0) | |
329 | goto out; | |
330 | ||
331 | /* Checkout worktree's HEAD */ | |
332 | coopts.checkout_strategy = GIT_CHECKOUT_FORCE; | |
333 | if ((err = git_checkout_head(wt, &coopts)) < 0) | |
334 | goto out; | |
335 | ||
336 | /* Load result */ | |
337 | if ((err = git_worktree_lookup(out, repo, name)) < 0) | |
338 | goto out; | |
339 | ||
340 | out: | |
7cf7a407 | 341 | git_buf_free(&gitdir); |
dea7488e PS |
342 | git_buf_free(&buf); |
343 | git_reference_free(ref); | |
344 | git_reference_free(head); | |
345 | git_commit_free(commit); | |
346 | git_repository_free(wt); | |
347 | ||
348 | return err; | |
349 | } | |
2a503485 PS |
350 | |
351 | int git_worktree_lock(git_worktree *wt, char *creason) | |
352 | { | |
353 | git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT; | |
354 | int err; | |
355 | ||
356 | assert(wt); | |
357 | ||
358 | if ((err = git_worktree_is_locked(NULL, wt)) < 0) | |
359 | goto out; | |
360 | ||
361 | if ((err = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0) | |
362 | goto out; | |
363 | ||
364 | if (creason) | |
365 | git_buf_attach_notowned(&buf, creason, strlen(creason)); | |
366 | ||
367 | if ((err = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0) | |
368 | goto out; | |
369 | ||
370 | wt->locked = 1; | |
371 | ||
372 | out: | |
373 | git_buf_free(&path); | |
374 | ||
375 | return err; | |
376 | } | |
377 | ||
378 | int git_worktree_unlock(git_worktree *wt) | |
379 | { | |
380 | git_buf path = GIT_BUF_INIT; | |
381 | ||
382 | assert(wt); | |
383 | ||
384 | if (!git_worktree_is_locked(NULL, wt)) | |
385 | return 0; | |
386 | ||
387 | if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0) | |
388 | return -1; | |
389 | ||
390 | if (p_unlink(path.ptr) != 0) { | |
391 | git_buf_free(&path); | |
392 | return -1; | |
393 | } | |
394 | ||
395 | wt->locked = 0; | |
396 | ||
397 | git_buf_free(&path); | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
402 | int git_worktree_is_locked(git_buf *reason, const git_worktree *wt) | |
403 | { | |
404 | git_buf path = GIT_BUF_INIT; | |
405 | int ret; | |
406 | ||
407 | assert(wt); | |
408 | ||
409 | if (reason) | |
410 | git_buf_clear(reason); | |
411 | ||
412 | if ((ret = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0) | |
413 | goto out; | |
414 | if ((ret = git_path_exists(path.ptr)) && reason) | |
415 | git_futils_readbuffer(reason, path.ptr); | |
416 | ||
417 | out: | |
418 | git_buf_free(&path); | |
419 | ||
420 | return ret; | |
421 | } | |
f0cfc341 | 422 | |
1ba242c9 | 423 | int git_worktree_is_prunable(git_worktree *wt, unsigned flags) |
f0cfc341 | 424 | { |
1ba242c9 | 425 | git_buf reason = GIT_BUF_INIT; |
f0cfc341 PS |
426 | |
427 | if ((flags & GIT_WORKTREE_PRUNE_LOCKED) == 0 && | |
428 | git_worktree_is_locked(&reason, wt)) | |
429 | { | |
430 | if (!reason.size) | |
431 | git_buf_attach_notowned(&reason, "no reason given", 15); | |
432 | giterr_set(GITERR_WORKTREE, "Not pruning locked working tree: '%s'", reason.ptr); | |
1ba242c9 | 433 | git_buf_free(&reason); |
f0cfc341 | 434 | |
1ba242c9 | 435 | return 0; |
f0cfc341 PS |
436 | } |
437 | ||
438 | if ((flags & GIT_WORKTREE_PRUNE_VALID) == 0 && | |
439 | git_worktree_validate(wt) == 0) | |
440 | { | |
441 | giterr_set(GITERR_WORKTREE, "Not pruning valid working tree"); | |
1ba242c9 PS |
442 | return 0; |
443 | } | |
444 | ||
445 | return 1; | |
446 | } | |
447 | ||
448 | int git_worktree_prune(git_worktree *wt, unsigned flags) | |
449 | { | |
450 | git_buf path = GIT_BUF_INIT; | |
451 | char *wtpath; | |
452 | int err; | |
453 | ||
454 | if (!git_worktree_is_prunable(wt, flags)) { | |
f0cfc341 PS |
455 | err = -1; |
456 | goto out; | |
457 | } | |
458 | ||
459 | /* Delete gitdir in parent repository */ | |
20a368e2 | 460 | if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0) |
f0cfc341 PS |
461 | goto out; |
462 | if (!git_path_exists(path.ptr)) | |
463 | { | |
464 | giterr_set(GITERR_WORKTREE, "Worktree gitdir '%s' does not exist", path.ptr); | |
465 | err = -1; | |
466 | goto out; | |
467 | } | |
468 | if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) | |
469 | goto out; | |
470 | ||
471 | /* Skip deletion of the actual working tree if it does | |
472 | * not exist or deletion was not requested */ | |
473 | if ((flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 || | |
474 | !git_path_exists(wt->gitlink_path)) | |
475 | { | |
476 | goto out; | |
477 | } | |
478 | ||
479 | if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL) | |
480 | goto out; | |
481 | git_buf_attach(&path, wtpath, 0); | |
482 | if (!git_path_exists(path.ptr)) | |
483 | { | |
484 | giterr_set(GITERR_WORKTREE, "Working tree '%s' does not exist", path.ptr); | |
485 | err = -1; | |
486 | goto out; | |
487 | } | |
488 | if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0) | |
489 | goto out; | |
490 | ||
491 | out: | |
f0cfc341 PS |
492 | git_buf_free(&path); |
493 | ||
494 | return err; | |
495 | } |