stash.c 13.9 KB
Newer Older
nulltoken committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
nulltoken committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "repository.h"
#include "commit.h"
#include "tree.h"
#include "reflog.h"
#include "git2/diff.h"
#include "git2/stash.h"
#include "git2/status.h"
#include "git2/checkout.h"
17
#include "git2/index.h"
Ben Straub committed
18
#include "signature.h"
nulltoken committed
19 20 21 22 23 24 25 26 27 28 29

static int create_error(int error, const char *msg)
{
	giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg);
	return error;
}

static int retrieve_head(git_reference **out, git_repository *repo)
{
	int error = git_repository_head(out, repo);

30
	if (error == GIT_EUNBORNBRANCH)
nulltoken committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
		return create_error(error, "You do not have the initial commit yet.");

	return error;
}

static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit)
{
	char *formatted_oid;

	formatted_oid = git_oid_allocfmt(b_commit);
	GITERR_CHECK_ALLOC(formatted_oid);

	git_buf_put(out, formatted_oid, 7);
	git__free(formatted_oid);

	return git_buf_oom(out) ? -1 : 0;
}

static int append_commit_description(git_buf *out, git_commit* commit)
{
	const char *message;
52
	size_t pos = 0, len;
nulltoken committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
		return -1;

	message = git_commit_message(commit);
	len = strlen(message);

	/* TODO: Replace with proper commit short message
	 * when git_commit_message_short() is implemented.
	 */
	while (pos < len && message[pos] != '\n')
		pos++;

	git_buf_putc(out, ' ');
	git_buf_put(out, message, pos);
	git_buf_putc(out, '\n');

	return git_buf_oom(out) ? -1 : 0;
}

static int retrieve_base_commit_and_message(
	git_commit **b_commit,
	git_buf *stash_message,
	git_repository *repo)
{
	git_reference *head = NULL;
	int error;

	if ((error = retrieve_head(&head, repo)) < 0)
		return error;

	if (strcmp("HEAD", git_reference_name(head)) == 0)
85
		error = git_buf_puts(stash_message, "(no branch): ");
nulltoken committed
86
	else
87
		error = git_buf_printf(
nulltoken committed
88 89 90
			stash_message,
			"%s: ",
			git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
91
	if (error < 0)
nulltoken committed
92 93
		goto cleanup;

94 95
	if ((error = git_commit_lookup(
			 b_commit, repo, git_reference_target(head))) < 0)
nulltoken committed
96 97
		goto cleanup;

98 99
	if ((error = append_commit_description(stash_message, *b_commit)) < 0)
		goto cleanup;
nulltoken committed
100 101 102 103 104 105 106 107

cleanup:
	git_reference_free(head);
	return error;
}

static int build_tree_from_index(git_tree **out, git_index *index)
{
108
	int error;
nulltoken committed
109 110
	git_oid i_tree_oid;

111
	if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
nulltoken committed
112 113 114 115 116 117 118 119
		return -1;

	return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
}

static int commit_index(
	git_commit **i_commit,
	git_index *index,
120
	const git_signature *stasher,
nulltoken committed
121 122 123 124 125 126
	const char *message,
	const git_commit *parent)
{
	git_tree *i_tree = NULL;
	git_oid i_commit_oid;
	git_buf msg = GIT_BUF_INIT;
127
	int error;
nulltoken committed
128

129
	if ((error = build_tree_from_index(&i_tree, index)) < 0)
nulltoken committed
130 131
		goto cleanup;

132
	if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0)
nulltoken committed
133 134
		goto cleanup;

135
	if ((error = git_commit_create(
nulltoken committed
136 137 138 139 140 141 142 143 144
		&i_commit_oid,
		git_index_owner(index),
		NULL,
		stasher,
		stasher,
		NULL,
		git_buf_cstr(&msg),
		i_tree,
		1,
145 146
		&parent)) < 0)
		goto cleanup;
nulltoken committed
147 148 149 150 151 152 153 154 155 156 157 158

	error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);

cleanup:
	git_tree_free(i_tree);
	git_buf_free(&msg);
	return error;
}

struct cb_data {
	git_index *index;

159 160
	int error;

nulltoken committed
161 162 163 164 165 166 167
	bool include_changed;
	bool include_untracked;
	bool include_ignored;
};

static int update_index_cb(
	const git_diff_delta *delta,
168 169
	float progress,
	void *payload)
nulltoken committed
170
{
171
	struct cb_data *data = (struct cb_data *)payload;
172
	const char *add_path = NULL;
nulltoken committed
173 174 175 176 177

	GIT_UNUSED(progress);

	switch (delta->status) {
	case GIT_DELTA_IGNORED:
178 179 180
		if (data->include_ignored)
			add_path = delta->new_file.path;
		break;
nulltoken committed
181 182

	case GIT_DELTA_UNTRACKED:
183 184 185
		if (data->include_untracked)
			add_path = delta->new_file.path;
		break;
nulltoken committed
186 187 188

	case GIT_DELTA_ADDED:
	case GIT_DELTA_MODIFIED:
189 190 191
		if (data->include_changed)
			add_path = delta->new_file.path;
		break;
nulltoken committed
192 193 194 195

	case GIT_DELTA_DELETED:
		if (!data->include_changed)
			break;
196
		if (git_index_find(NULL, data->index, delta->old_file.path) == 0)
197 198 199
			data->error = git_index_remove(
				data->index, delta->old_file.path, 0);
		break;
nulltoken committed
200 201 202 203 204

	default:
		/* Unimplemented */
		giterr_set(
			GITERR_INVALID,
205
			"Cannot update index. Unimplemented status (%d)",
nulltoken committed
206
			delta->status);
207 208
		data->error = -1;
		break;
nulltoken committed
209 210
	}

211
	if (add_path != NULL)
212
		data->error = git_index_add_bypath(data->index, add_path);
213 214

	return data->error;
nulltoken committed
215 216 217 218 219 220 221 222 223 224
}

static int build_untracked_tree(
	git_tree **tree_out,
	git_index *index,
	git_commit *i_commit,
	uint32_t flags)
{
	git_tree *i_tree = NULL;
	git_diff_list *diff = NULL;
225
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
nulltoken committed
226
	struct cb_data data = {0};
227
	int error;
nulltoken committed
228 229 230 231 232 233

	git_index_clear(index);

	data.index = index;

	if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
234 235
		opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
			GIT_DIFF_RECURSE_UNTRACKED_DIRS;
nulltoken committed
236 237 238 239 240 241 242 243
		data.include_untracked = true;
	}

	if (flags & GIT_STASH_INCLUDE_IGNORED) {
		opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
		data.include_ignored = true;
	}

244
	if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
nulltoken committed
245 246
		goto cleanup;

247 248
	if ((error = git_diff_tree_to_workdir(
			&diff, git_index_owner(index), i_tree, &opts)) < 0)
nulltoken committed
249 250
		goto cleanup;

251 252 253 254 255
	if ((error = git_diff_foreach(
			diff, update_index_cb, NULL, NULL, &data)) < 0)
	{
		if (error == GIT_EUSER)
			error = data.error;
nulltoken committed
256
		goto cleanup;
257
	}
nulltoken committed
258

259
	error = build_tree_from_index(tree_out, index);
nulltoken committed
260 261 262 263 264 265 266 267 268 269

cleanup:
	git_diff_list_free(diff);
	git_tree_free(i_tree);
	return error;
}

static int commit_untracked(
	git_commit **u_commit,
	git_index *index,
270
	const git_signature *stasher,
nulltoken committed
271 272 273 274 275 276 277
	const char *message,
	git_commit *i_commit,
	uint32_t flags)
{
	git_tree *u_tree = NULL;
	git_oid u_commit_oid;
	git_buf msg = GIT_BUF_INIT;
278
	int error;
nulltoken committed
279

280
	if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0)
nulltoken committed
281 282
		goto cleanup;

283
	if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0)
nulltoken committed
284 285
		goto cleanup;

286
	if ((error = git_commit_create(
nulltoken committed
287 288 289 290 291 292 293 294 295
		&u_commit_oid,
		git_index_owner(index),
		NULL,
		stasher,
		stasher,
		NULL,
		git_buf_cstr(&msg),
		u_tree,
		0,
296 297
		NULL)) < 0)
		goto cleanup;
nulltoken committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311

	error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);

cleanup:
	git_tree_free(u_tree);
	git_buf_free(&msg);
	return error;
}

static int build_workdir_tree(
	git_tree **tree_out,
	git_index *index,
	git_commit *b_commit)
{
312
	git_repository *repo = git_index_owner(index);
nulltoken committed
313 314
	git_tree *b_tree = NULL;
	git_diff_list *diff = NULL, *diff2 = NULL;
315
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
nulltoken committed
316
	struct cb_data data = {0};
317
	int error;
nulltoken committed
318

319 320
	opts.flags = GIT_DIFF_IGNORE_SUBMODULES;

321
	if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
nulltoken committed
322 323
		goto cleanup;

324
	if ((error = git_diff_tree_to_index(&diff, repo, b_tree, NULL, &opts)) < 0)
nulltoken committed
325 326
		goto cleanup;

327
	if ((error = git_diff_index_to_workdir(&diff2, repo, NULL, &opts)) < 0)
nulltoken committed
328 329
		goto cleanup;

330
	if ((error = git_diff_merge(diff, diff2)) < 0)
nulltoken committed
331 332 333 334 335
		goto cleanup;

	data.index = index;
	data.include_changed = true;

336 337 338 339 340
	if ((error = git_diff_foreach(
			diff, update_index_cb, NULL, NULL, &data)) < 0)
	{
		if (error == GIT_EUSER)
			error = data.error;
nulltoken committed
341
		goto cleanup;
342
	}
nulltoken committed
343 344


345 346
	if ((error = build_tree_from_index(tree_out, index)) < 0)
		goto cleanup;
nulltoken committed
347 348 349 350 351

cleanup:
	git_diff_list_free(diff);
	git_diff_list_free(diff2);
	git_tree_free(b_tree);
352

nulltoken committed
353 354 355 356 357 358
	return error;
}

static int commit_worktree(
	git_oid *w_commit_oid,
	git_index *index,
359
	const git_signature *stasher,
nulltoken committed
360 361 362 363 364
	const char *message,
	git_commit *i_commit,
	git_commit *b_commit,
	git_commit *u_commit)
{
365
	int error = 0;
nulltoken committed
366 367 368 369 370 371 372
	git_tree *w_tree = NULL, *i_tree = NULL;
	const git_commit *parents[] = {	NULL, NULL,	NULL };

	parents[0] = b_commit;
	parents[1] = i_commit;
	parents[2] = u_commit;

373 374
	if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
		goto cleanup;
nulltoken committed
375

376
	if ((error = git_index_read_tree(index, i_tree)) < 0)
nulltoken committed
377 378
		goto cleanup;

379
	if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0)
nulltoken committed
380 381
		goto cleanup;

382
	error = git_commit_create(
nulltoken committed
383 384 385 386 387 388 389 390
		w_commit_oid,
		git_index_owner(index),
		NULL,
		stasher,
		stasher,
		NULL,
		message,
		w_tree,
391 392
		u_commit ? 3 : 2,
		parents);
nulltoken committed
393 394 395 396 397 398 399 400 401 402 403 404

cleanup:
	git_tree_free(i_tree);
	git_tree_free(w_tree);
	return error;
}

static int prepare_worktree_commit_message(
	git_buf* msg,
	const char *user_message)
{
	git_buf buf = GIT_BUF_INIT;
405 406
	int error;

407 408
	if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
		return error;
nulltoken committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424

	git_buf_clear(msg);

	if (!user_message)
		git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
	else {
		const char *colon;

		if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
			goto cleanup;

		git_buf_puts(msg, "On ");
		git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
		git_buf_printf(msg, ": %s\n", user_message);
	}

425
	error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
nulltoken committed
426 427 428

cleanup:
	git_buf_free(&buf);
429

nulltoken committed
430 431 432 433 434 435
	return error;
}

static int update_reflog(
	git_oid *w_commit_oid,
	git_repository *repo,
436
	const git_signature *stasher,
nulltoken committed
437 438 439 440 441 442
	const char *message)
{
	git_reference *stash = NULL;
	git_reflog *reflog = NULL;
	int error;

443
	if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0)
nulltoken committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
		goto cleanup;

	if ((error = git_reflog_read(&reflog, stash)) < 0)
		goto cleanup;

	if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0)
		goto cleanup;

	if ((error = git_reflog_write(reflog)) < 0)
		goto cleanup;

cleanup:
	git_reference_free(stash);
	git_reflog_free(reflog);
	return error;
}

static int is_dirty_cb(const char *path, unsigned int status, void *payload)
{
	GIT_UNUSED(path);
	GIT_UNUSED(status);
	GIT_UNUSED(payload);

	return 1;
}

static int ensure_there_are_changes_to_stash(
	git_repository *repo,
	bool include_untracked_files,
	bool include_ignored_files)
{
	int error;
476
	git_status_options opts = GIT_STATUS_OPTIONS_INIT;
nulltoken committed
477 478

	opts.show  = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
479 480
	opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES;

nulltoken committed
481
	if (include_untracked_files)
482
		opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
nulltoken committed
483 484 485
		GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;

	if (include_ignored_files)
486
		opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED;
nulltoken committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

	error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);

	if (error == GIT_EUSER)
		return 0;

	if (!error)
		return create_error(GIT_ENOTFOUND, "There is nothing to stash.");

	return error;
}

static int reset_index_and_workdir(
	git_repository *repo,
	git_commit *commit,
	bool remove_untracked)
{
504
	git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
nulltoken committed
505

506
	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
nulltoken committed
507 508 509 510 511 512 513 514 515 516

	if (remove_untracked)
		opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;

	return git_checkout_tree(repo, (git_object *)commit, &opts);
}

int git_stash_save(
	git_oid *out,
	git_repository *repo,
517
	const git_signature *stasher,
nulltoken committed
518 519 520 521 522 523 524 525 526 527
	const char *message,
	uint32_t flags)
{
	git_index *index = NULL;
	git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
	git_buf msg = GIT_BUF_INIT;
	int error;

	assert(out && repo && stasher);

528
	if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
nulltoken committed
529 530 531 532 533 534 535
		return error;

	if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
		goto cleanup;

	if ((error = ensure_there_are_changes_to_stash(
		repo,
536 537
		(flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
		(flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
nulltoken committed
538 539
		goto cleanup;

540
	if ((error = git_repository_index(&index, repo)) < 0)
nulltoken committed
541 542
		goto cleanup;

543 544
	if ((error = commit_index(
			&i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
nulltoken committed
545 546
		goto cleanup;

547 548 549 550
	if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
		(error = commit_untracked(
			&u_commit, index, stasher, git_buf_cstr(&msg),
			i_commit, flags)) < 0)
nulltoken committed
551 552
		goto cleanup;

553
	if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
nulltoken committed
554 555
		goto cleanup;

556 557 558
	if ((error = commit_worktree(
			out, index, stasher, git_buf_cstr(&msg),
			i_commit, b_commit, u_commit)) < 0)
nulltoken committed
559 560 561
		goto cleanup;

	git_buf_rtrim(&msg);
562 563

	if ((error = update_reflog(out, repo, stasher, git_buf_cstr(&msg))) < 0)
nulltoken committed
564 565
		goto cleanup;

566
	if ((error = reset_index_and_workdir(
nulltoken committed
567
		repo,
568 569
		((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit,
		(flags & GIT_STASH_INCLUDE_UNTRACKED) != 0)) < 0)
nulltoken committed
570 571 572
		goto cleanup;

cleanup:
573

nulltoken committed
574 575 576 577 578
	git_buf_free(&msg);
	git_commit_free(i_commit);
	git_commit_free(b_commit);
	git_commit_free(u_commit);
	git_index_free(index);
579

nulltoken committed
580 581
	return error;
}
582 583 584

int git_stash_foreach(
	git_repository *repo,
Ben Straub committed
585
	git_stash_cb callback,
586 587 588 589 590 591 592 593 594
	void *payload)
{
	git_reference *stash;
	git_reflog *reflog = NULL;
	int error;
	size_t i, max;
	const git_reflog_entry *entry;

	error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
595 596
	if (error == GIT_ENOTFOUND) {
		giterr_clear();
597
		return 0;
598
	}
599 600 601 602 603 604 605 606
	if (error < 0)
		goto cleanup;

	if ((error = git_reflog_read(&reflog, stash)) < 0)
		goto cleanup;

	max = git_reflog_entrycount(reflog);
	for (i = 0; i < max; i++) {
607
		entry = git_reflog_entry_byindex(reflog, i);
608

609
		if (callback(i,
610 611
			git_reflog_entry_message(entry),
			git_reflog_entry_id_new(entry),
612 613
			payload)) {
				error = GIT_EUSER;
614
				break;
615 616 617 618 619 620 621 622
		}
	}

cleanup:
	git_reference_free(stash);
	git_reflog_free(reflog);
	return error;
}
nulltoken committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646

int git_stash_drop(
	git_repository *repo,
	size_t index)
{
	git_reference *stash;
	git_reflog *reflog = NULL;
	size_t max;
	int error;

	if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
		return error;

	if ((error = git_reflog_read(&reflog, stash)) < 0)
		goto cleanup;

	max = git_reflog_entrycount(reflog);

	if (index > max - 1) {
		error = GIT_ENOTFOUND;
		giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
		goto cleanup;
	}

647
	if ((error = git_reflog_drop(reflog, index, true)) < 0)
nulltoken committed
648 649 650 651 652 653 654
		goto cleanup;

	if ((error = git_reflog_write(reflog)) < 0)
		goto cleanup;

	if (max == 1) {
		error = git_reference_delete(stash);
655
		git_reference_free(stash);
nulltoken committed
656
		stash = NULL;
657 658 659 660
	} else if (index == 0) {
		const git_reflog_entry *entry;

		entry = git_reflog_entry_byindex(reflog, 0);
661

662 663
		git_reference_free(stash);
		error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, &entry->oid_cur, 1);
nulltoken committed
664 665 666 667 668 669 670
	}

cleanup:
	git_reference_free(stash);
	git_reflog_free(reflog);
	return error;
}