worktree.c 13.5 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8
#include "worktree.h"
9 10 11

#include "git2/branch.h"
#include "git2/commit.h"
12 13 14 15
#include "git2/worktree.h"

#include "repository.h"

16
static bool is_worktree_dir(const char *dir)
17
{
18 19 20 21 22 23 24 25 26 27
	git_buf buf = GIT_BUF_INIT;
	int error;

	if (git_buf_sets(&buf, dir) < 0)
		return -1;

	error = git_path_contains_file(&buf, "commondir")
		&& git_path_contains_file(&buf, "gitdir")
		&& git_path_contains_file(&buf, "HEAD");

28
	git_buf_dispose(&buf);
29
	return error;
30 31 32 33 34 35 36
}

int git_worktree_list(git_strarray *wts, git_repository *repo)
{
	git_vector worktrees = GIT_VECTOR_INIT;
	git_buf path = GIT_BUF_INIT;
	char *worktree;
37
	size_t i, len;
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	int error;

	assert(wts && repo);

	wts->count = 0;
	wts->strings = NULL;

	if ((error = git_buf_printf(&path, "%s/worktrees/", repo->commondir)) < 0)
		goto exit;
	if (!git_path_exists(path.ptr) || git_path_is_empty_dir(path.ptr))
		goto exit;
	if ((error = git_path_dirload(&worktrees, path.ptr, path.size, 0x0)) < 0)
		goto exit;

	len = path.size;

	git_vector_foreach(&worktrees, i, worktree) {
		git_buf_truncate(&path, len);
		git_buf_puts(&path, worktree);

58
		if (!is_worktree_dir(path.ptr)) {
59 60 61 62 63 64 65 66
			git_vector_remove(&worktrees, i);
			git__free(worktree);
		}
	}

	wts->strings = (char **)git_vector_detach(&wts->count, NULL, &worktrees);

exit:
67
	git_buf_dispose(&path);
68 69 70

	return error;
}
71

72
char *git_worktree__read_link(const char *base, const char *file)
73 74 75 76 77 78 79 80 81
{
	git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;

	assert(base && file);

	if (git_buf_joinpath(&path, base, file) < 0)
		goto err;
	if (git_futils_readbuffer(&buf, path.ptr) < 0)
		goto err;
82
	git_buf_dispose(&path);
83 84 85 86 87 88 89 90 91 92

	git_buf_rtrim(&buf);

	if (!git_path_is_relative(buf.ptr))
		return git_buf_detach(&buf);

	if (git_buf_sets(&path, base) < 0)
		goto err;
	if (git_path_apply_relative(&path, buf.ptr) < 0)
		goto err;
93
	git_buf_dispose(&buf);
94 95 96 97

	return git_buf_detach(&path);

err:
98 99
	git_buf_dispose(&buf);
	git_buf_dispose(&path);
100 101 102 103

	return NULL;
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117
static int write_wtfile(const char *base, const char *file, const git_buf *buf)
{
	git_buf path = GIT_BUF_INIT;
	int err;

	assert(base && file && buf);

	if ((err = git_buf_joinpath(&path, base, file)) < 0)
		goto out;

	if ((err = git_futils_writebuffer(buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
		goto out;

out:
118
	git_buf_dispose(&path);
119 120 121 122

	return err;
}

123
static int open_worktree_dir(git_worktree **out, const char *parent, const char *dir, const char *name)
124
{
125
	git_buf gitdir = GIT_BUF_INIT;
126
	git_worktree *wt = NULL;
127
	int error = 0;
128

129
	if (!is_worktree_dir(dir)) {
130 131 132 133
		error = -1;
		goto out;
	}

134
	if ((wt = git__calloc(1, sizeof(*wt))) == NULL) {
135 136 137 138
		error = -1;
		goto out;
	}

139 140 141 142 143
	if ((wt->name = git__strdup(name)) == NULL ||
	    (wt->commondir_path = git_worktree__read_link(dir, "commondir")) == NULL ||
	    (wt->gitlink_path = git_worktree__read_link(dir, "gitdir")) == NULL ||
	    (parent && (wt->parent_path = git__strdup(parent)) == NULL) ||
	    (wt->worktree_path = git_path_dirname(wt->gitlink_path)) == NULL) {
144 145 146
		error = -1;
		goto out;
	}
147 148 149 150 151

	if ((error = git_path_prettify_dir(&gitdir, dir, NULL)) < 0)
		goto out;
	wt->gitdir_path = git_buf_detach(&gitdir);

152 153 154 155
	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
		goto out;
	wt->locked = !!error;
	error = 0;
156

157 158 159 160 161
	*out = wt;

out:
	if (error)
		git_worktree_free(wt);
162
	git_buf_dispose(&gitdir);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	return error;
}

int git_worktree_lookup(git_worktree **out, git_repository *repo, const char *name)
{
	git_buf path = GIT_BUF_INIT;
	git_worktree *wt = NULL;
	int error;

	assert(repo && name);

	*out = NULL;

	if ((error = git_buf_printf(&path, "%s/worktrees/%s", repo->commondir, name)) < 0)
		goto out;

180
	if ((error = (open_worktree_dir(out, git_repository_workdir(repo), path.ptr, name))) < 0)
181
		goto out;
182 183

out:
184
	git_buf_dispose(&path);
185 186 187 188 189 190 191

	if (error)
		git_worktree_free(wt);

	return error;
}

192 193 194 195 196 197 198 199
int git_worktree_open_from_repository(git_worktree **out, git_repository *repo)
{
	git_buf parent = GIT_BUF_INIT;
	const char *gitdir, *commondir;
	char *name = NULL;
	int error = 0;

	if (!git_repository_is_worktree(repo)) {
200
		git_error_set(GIT_ERROR_WORKTREE, "cannot open worktree of a non-worktree repo");
201 202 203 204 205 206 207
		error = -1;
		goto out;
	}

	gitdir = git_repository_path(repo);
	commondir = git_repository_commondir(repo);

208
	if ((error = git_path_prettify_dir(&parent, "..", commondir)) < 0)
209 210 211 212 213 214 215 216 217
		goto out;

	/* The name is defined by the last component in '.git/worktree/%s' */
	name = git_path_basename(gitdir);

	if ((error = open_worktree_dir(out, parent.ptr, gitdir, name)) < 0)
		goto out;

out:
218
	git__free(name);
219
	git_buf_dispose(&parent);
220 221 222 223

	return error;
}

224 225 226 227 228 229
void git_worktree_free(git_worktree *wt)
{
	if (!wt)
		return;

	git__free(wt->commondir_path);
230
	git__free(wt->worktree_path);
231 232 233 234 235 236
	git__free(wt->gitlink_path);
	git__free(wt->gitdir_path);
	git__free(wt->parent_path);
	git__free(wt->name);
	git__free(wt);
}
237 238 239 240 241

int git_worktree_validate(const git_worktree *wt)
{
	assert(wt);

242
	if (!is_worktree_dir(wt->gitdir_path)) {
243
		git_error_set(GIT_ERROR_WORKTREE,
Edward Thomson committed
244
			"worktree gitdir ('%s') is not valid",
245
			wt->gitlink_path);
246
		return GIT_ERROR;
247 248
	}

249
	if (wt->parent_path && !git_path_exists(wt->parent_path)) {
250
		git_error_set(GIT_ERROR_WORKTREE,
Edward Thomson committed
251
			"worktree parent directory ('%s') does not exist ",
252
			wt->parent_path);
253
		return GIT_ERROR;
254 255 256
	}

	if (!git_path_exists(wt->commondir_path)) {
257
		git_error_set(GIT_ERROR_WORKTREE,
Edward Thomson committed
258
			"worktree common directory ('%s') does not exist ",
259
			wt->commondir_path);
260
		return GIT_ERROR;
261 262
	}

263
	return 0;
264
}
265

266
int git_worktree_add_options_init(git_worktree_add_options *opts,
267 268 269 270 271 272 273
	unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
		git_worktree_add_options, GIT_WORKTREE_ADD_OPTIONS_INIT);
	return 0;
}

274 275 276 277 278 279
int git_worktree_add_init_options(git_worktree_add_options *opts,
	unsigned int version)
{
	return git_worktree_add_options_init(opts, version);
}

280 281 282
int git_worktree_add(git_worktree **out, git_repository *repo,
	const char *name, const char *worktree,
	const git_worktree_add_options *opts)
283
{
284
	git_buf gitdir = GIT_BUF_INIT, wddir = GIT_BUF_INIT, buf = GIT_BUF_INIT;
285 286 287 288
	git_reference *ref = NULL, *head = NULL;
	git_commit *commit = NULL;
	git_repository *wt = NULL;
	git_checkout_options coopts = GIT_CHECKOUT_OPTIONS_INIT;
289
	git_worktree_add_options wtopts = GIT_WORKTREE_ADD_OPTIONS_INIT;
290 291
	int err;

292
	GIT_ERROR_CHECK_VERSION(
293 294 295 296 297
		opts, GIT_WORKTREE_ADD_OPTIONS_VERSION, "git_worktree_add_options");

	if (opts)
		memcpy(&wtopts, opts, sizeof(wtopts));

298 299 300 301
	assert(out && repo && name && worktree);

	*out = NULL;

302 303 304 305 306 307 308 309 310 311 312 313 314 315
	if (wtopts.ref) {
		if (!git_reference_is_branch(wtopts.ref)) {
			git_error_set(GIT_ERROR_WORKTREE, "reference is not a branch");
			err = -1;
			goto out;
		}

		if (git_branch_is_checked_out(wtopts.ref)) {
			git_error_set(GIT_ERROR_WORKTREE, "reference is already checked out");
			err = -1;
			goto out;
		}
	}

316 317
	/* Create gitdir directory ".git/worktrees/<name>" */
	if ((err = git_buf_joinpath(&gitdir, repo->commondir, "worktrees")) < 0)
318
		goto out;
319 320
	if (!git_path_exists(gitdir.ptr))
		if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
321
			goto out;
322
	if ((err = git_buf_joinpath(&gitdir, gitdir.ptr, name)) < 0)
323
		goto out;
324
	if ((err = git_futils_mkdir(gitdir.ptr, 0755, GIT_MKDIR_EXCL)) < 0)
325
		goto out;
326 327
	if ((err = git_path_prettify_dir(&gitdir, gitdir.ptr, NULL)) < 0)
		goto out;
328 329 330 331

	/* Create worktree work dir */
	if ((err = git_futils_mkdir(worktree, 0755, GIT_MKDIR_EXCL)) < 0)
		goto out;
332 333
	if ((err = git_path_prettify_dir(&wddir, worktree, NULL)) < 0)
		goto out;
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
	if (wtopts.lock) {
		int fd;

		if ((err = git_buf_joinpath(&buf, gitdir.ptr, "locked")) < 0)
			goto out;

		if ((fd = p_creat(buf.ptr, 0644)) < 0) {
			err = fd;
			goto out;
		}

		p_close(fd);
		git_buf_clear(&buf);
	}

350
	/* Create worktree .git file */
351
	if ((err = git_buf_printf(&buf, "gitdir: %s\n", gitdir.ptr)) < 0)
352
		goto out;
353
	if ((err = write_wtfile(wddir.ptr, ".git", &buf)) < 0)
354 355
		goto out;

356
	/* Create gitdir files */
357
	if ((err = git_path_prettify_dir(&buf, repo->commondir, NULL) < 0)
358
	    || (err = git_buf_putc(&buf, '\n')) < 0
359
	    || (err = write_wtfile(gitdir.ptr, "commondir", &buf)) < 0)
360
		goto out;
361
	if ((err = git_buf_joinpath(&buf, wddir.ptr, ".git")) < 0
362
	    || (err = git_buf_putc(&buf, '\n')) < 0
363
	    || (err = write_wtfile(gitdir.ptr, "gitdir", &buf)) < 0)
364 365
		goto out;

366 367 368 369 370 371 372 373 374 375 376 377
	/* Set up worktree reference */
	if (wtopts.ref) {
		if ((err = git_reference_dup(&ref, wtopts.ref)) < 0)
			goto out;
	} else {
		if ((err = git_repository_head(&head, repo)) < 0)
			goto out;
		if ((err = git_commit_lookup(&commit, repo, &head->target.oid)) < 0)
			goto out;
		if ((err = git_branch_create(&ref, repo, name, commit, false)) < 0)
			goto out;
	}
378 379

	/* Set worktree's HEAD */
380
	if ((err = git_repository_create_head(gitdir.ptr, git_reference_name(ref))) < 0)
381
		goto out;
382
	if ((err = git_repository_open(&wt, wddir.ptr)) < 0)
383 384 385 386 387 388 389 390 391 392 393 394
		goto out;

	/* Checkout worktree's HEAD */
	coopts.checkout_strategy = GIT_CHECKOUT_FORCE;
	if ((err = git_checkout_head(wt, &coopts)) < 0)
		goto out;

	/* Load result */
	if ((err = git_worktree_lookup(out, repo, name)) < 0)
		goto out;

out:
395 396 397
	git_buf_dispose(&gitdir);
	git_buf_dispose(&wddir);
	git_buf_dispose(&buf);
398 399 400 401 402 403 404
	git_reference_free(ref);
	git_reference_free(head);
	git_commit_free(commit);
	git_repository_free(wt);

	return err;
}
405

406
int git_worktree_lock(git_worktree *wt, const char *reason)
407 408
{
	git_buf buf = GIT_BUF_INIT, path = GIT_BUF_INIT;
409
	int error;
410 411 412

	assert(wt);

413 414 415 416
	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
		goto out;
	if (error) {
		error = GIT_ELOCKED;
417
		goto out;
418
	}
419

420
	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
421 422
		goto out;

423 424
	if (reason)
		git_buf_attach_notowned(&buf, reason, strlen(reason));
425

426
	if ((error = git_futils_writebuffer(&buf, path.ptr, O_CREAT|O_EXCL|O_WRONLY, 0644)) < 0)
427 428 429 430 431
		goto out;

	wt->locked = 1;

out:
432
	git_buf_dispose(&path);
433

434
	return error;
435 436 437 438 439
}

int git_worktree_unlock(git_worktree *wt)
{
	git_buf path = GIT_BUF_INIT;
440
	int error;
441 442 443

	assert(wt);

444 445 446
	if ((error = git_worktree_is_locked(NULL, wt)) < 0)
		return error;
	if (!error)
447
		return 1;
448 449 450 451 452

	if (git_buf_joinpath(&path, wt->gitdir_path, "locked") < 0)
		return -1;

	if (p_unlink(path.ptr) != 0) {
453
		git_buf_dispose(&path);
454 455 456 457 458
		return -1;
	}

	wt->locked = 0;

459
	git_buf_dispose(&path);
460 461 462 463 464 465 466

	return 0;
}

int git_worktree_is_locked(git_buf *reason, const git_worktree *wt)
{
	git_buf path = GIT_BUF_INIT;
467
	int error, locked;
468 469 470 471 472 473

	assert(wt);

	if (reason)
		git_buf_clear(reason);

474 475 476 477 478
	if ((error = git_buf_joinpath(&path, wt->gitdir_path, "locked")) < 0)
		goto out;
	locked = git_path_exists(path.ptr);
	if (locked && reason &&
	    (error = git_futils_readbuffer(reason, path.ptr)) < 0)
479 480
		goto out;

481
	error = locked;
482
out:
483
	git_buf_dispose(&path);
484

485
	return error;
486
}
487

488 489 490 491 492 493 494 495 496 497 498 499
const char *git_worktree_name(const git_worktree *wt)
{
	assert(wt);
	return wt->name;
}

const char *git_worktree_path(const git_worktree *wt)
{
	assert(wt);
	return wt->worktree_path;
}

500
int git_worktree_prune_options_init(
501 502 503 504 505 506 507 508
	git_worktree_prune_options *opts,
	unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(opts, version,
		git_worktree_prune_options, GIT_WORKTREE_PRUNE_OPTIONS_INIT);
	return 0;
}

509
int git_worktree_prune_init_options(git_worktree_prune_options *opts,
510 511 512 513 514
	unsigned int version)
{
	return git_worktree_prune_options_init(opts, version);
}

515 516
int git_worktree_is_prunable(git_worktree *wt,
	git_worktree_prune_options *opts)
517
{
518 519
	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;

520
	GIT_ERROR_CHECK_VERSION(
521 522 523 524 525
		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
		"git_worktree_prune_options");

	if (opts)
		memcpy(&popts, opts, sizeof(popts));
526

527 528 529
	if ((popts.flags & GIT_WORKTREE_PRUNE_LOCKED) == 0) {
		git_buf reason = GIT_BUF_INIT;
		int error;
530

531 532 533 534 535 536 537 538 539 540
		if ((error = git_worktree_is_locked(&reason, wt)) < 0)
			return error;

		if (error) {
			if (!reason.size)
				git_buf_attach_notowned(&reason, "no reason given", 15);
			git_error_set(GIT_ERROR_WORKTREE, "not pruning locked working tree: '%s'", reason.ptr);
			git_buf_dispose(&reason);
			return 0;
		}
541 542
	}

543
	if ((popts.flags & GIT_WORKTREE_PRUNE_VALID) == 0 &&
544
	    git_worktree_validate(wt) == 0) {
Edward Thomson committed
545
		git_error_set(GIT_ERROR_WORKTREE, "not pruning valid working tree");
546 547 548 549 550 551
		return 0;
	}

	return 1;
}

552 553
int git_worktree_prune(git_worktree *wt,
	git_worktree_prune_options *opts)
554
{
555
	git_worktree_prune_options popts = GIT_WORKTREE_PRUNE_OPTIONS_INIT;
556 557 558 559
	git_buf path = GIT_BUF_INIT;
	char *wtpath;
	int err;

560
	GIT_ERROR_CHECK_VERSION(
561 562 563 564 565 566 567
		opts, GIT_WORKTREE_PRUNE_OPTIONS_VERSION,
		"git_worktree_prune_options");

	if (opts)
		memcpy(&popts, opts, sizeof(popts));

	if (!git_worktree_is_prunable(wt, &popts)) {
568 569 570 571 572
		err = -1;
		goto out;
	}

	/* Delete gitdir in parent repository */
573
	if ((err = git_buf_printf(&path, "%s/worktrees/%s", wt->commondir_path, wt->name)) < 0)
574 575 576
		goto out;
	if (!git_path_exists(path.ptr))
	{
Edward Thomson committed
577
		git_error_set(GIT_ERROR_WORKTREE, "worktree gitdir '%s' does not exist", path.ptr);
578 579 580 581 582 583 584 585
		err = -1;
		goto out;
	}
	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
		goto out;

	/* Skip deletion of the actual working tree if it does
	 * not exist or deletion was not requested */
586
	if ((popts.flags & GIT_WORKTREE_PRUNE_WORKING_TREE) == 0 ||
587 588 589 590 591 592 593 594 595 596
		!git_path_exists(wt->gitlink_path))
	{
		goto out;
	}

	if ((wtpath = git_path_dirname(wt->gitlink_path)) == NULL)
		goto out;
	git_buf_attach(&path, wtpath, 0);
	if (!git_path_exists(path.ptr))
	{
Edward Thomson committed
597
		git_error_set(GIT_ERROR_WORKTREE, "working tree '%s' does not exist", path.ptr);
598 599 600 601 602 603 604
		err = -1;
		goto out;
	}
	if ((err = git_futils_rmdir_r(path.ptr, NULL, GIT_RMDIR_REMOVE_FILES)) < 0)
		goto out;

out:
605
	git_buf_dispose(&path);
606 607 608

	return err;
}