]> git.proxmox.com Git - libgit2.git/blob - src/stash.c
Imported Upstream version 0.24.0
[libgit2.git] / src / stash.c
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
8 #include "common.h"
9 #include "repository.h"
10 #include "commit.h"
11 #include "message.h"
12 #include "tree.h"
13 #include "reflog.h"
14 #include "git2/diff.h"
15 #include "git2/stash.h"
16 #include "git2/status.h"
17 #include "git2/checkout.h"
18 #include "git2/index.h"
19 #include "git2/transaction.h"
20 #include "git2/merge.h"
21 #include "index.h"
22 #include "signature.h"
23 #include "iterator.h"
24 #include "merge.h"
25 #include "diff.h"
26
27 static int create_error(int error, const char *msg)
28 {
29 giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg);
30 return error;
31 }
32
33 static int retrieve_head(git_reference **out, git_repository *repo)
34 {
35 int error = git_repository_head(out, repo);
36
37 if (error == GIT_EUNBORNBRANCH)
38 return create_error(error, "You do not have the initial commit yet.");
39
40 return error;
41 }
42
43 static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit)
44 {
45 char *formatted_oid;
46
47 formatted_oid = git_oid_allocfmt(b_commit);
48 GITERR_CHECK_ALLOC(formatted_oid);
49
50 git_buf_put(out, formatted_oid, 7);
51 git__free(formatted_oid);
52
53 return git_buf_oom(out) ? -1 : 0;
54 }
55
56 static int append_commit_description(git_buf *out, git_commit* commit)
57 {
58 const char *summary = git_commit_summary(commit);
59 GITERR_CHECK_ALLOC(summary);
60
61 if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
62 return -1;
63
64 git_buf_putc(out, ' ');
65 git_buf_puts(out, summary);
66 git_buf_putc(out, '\n');
67
68 return git_buf_oom(out) ? -1 : 0;
69 }
70
71 static int retrieve_base_commit_and_message(
72 git_commit **b_commit,
73 git_buf *stash_message,
74 git_repository *repo)
75 {
76 git_reference *head = NULL;
77 int error;
78
79 if ((error = retrieve_head(&head, repo)) < 0)
80 return error;
81
82 if (strcmp("HEAD", git_reference_name(head)) == 0)
83 error = git_buf_puts(stash_message, "(no branch): ");
84 else
85 error = git_buf_printf(
86 stash_message,
87 "%s: ",
88 git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
89 if (error < 0)
90 goto cleanup;
91
92 if ((error = git_commit_lookup(
93 b_commit, repo, git_reference_target(head))) < 0)
94 goto cleanup;
95
96 if ((error = append_commit_description(stash_message, *b_commit)) < 0)
97 goto cleanup;
98
99 cleanup:
100 git_reference_free(head);
101 return error;
102 }
103
104 static int build_tree_from_index(git_tree **out, git_index *index)
105 {
106 int error;
107 git_oid i_tree_oid;
108
109 if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
110 return error;
111
112 return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
113 }
114
115 static int commit_index(
116 git_commit **i_commit,
117 git_index *index,
118 const git_signature *stasher,
119 const char *message,
120 const git_commit *parent)
121 {
122 git_tree *i_tree = NULL;
123 git_oid i_commit_oid;
124 git_buf msg = GIT_BUF_INIT;
125 int error;
126
127 if ((error = build_tree_from_index(&i_tree, index)) < 0)
128 goto cleanup;
129
130 if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0)
131 goto cleanup;
132
133 if ((error = git_commit_create(
134 &i_commit_oid,
135 git_index_owner(index),
136 NULL,
137 stasher,
138 stasher,
139 NULL,
140 git_buf_cstr(&msg),
141 i_tree,
142 1,
143 &parent)) < 0)
144 goto cleanup;
145
146 error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
147
148 cleanup:
149 git_tree_free(i_tree);
150 git_buf_free(&msg);
151 return error;
152 }
153
154 struct stash_update_rules {
155 bool include_changed;
156 bool include_untracked;
157 bool include_ignored;
158 };
159
160 static int stash_update_index_from_diff(
161 git_index *index,
162 const git_diff *diff,
163 struct stash_update_rules *data)
164 {
165 int error = 0;
166 size_t d, max_d = git_diff_num_deltas(diff);
167
168 for (d = 0; !error && d < max_d; ++d) {
169 const char *add_path = NULL;
170 const git_diff_delta *delta = git_diff_get_delta(diff, d);
171
172 switch (delta->status) {
173 case GIT_DELTA_IGNORED:
174 if (data->include_ignored)
175 add_path = delta->new_file.path;
176 break;
177
178 case GIT_DELTA_UNTRACKED:
179 if (data->include_untracked &&
180 delta->new_file.mode != GIT_FILEMODE_TREE)
181 add_path = delta->new_file.path;
182 break;
183
184 case GIT_DELTA_ADDED:
185 case GIT_DELTA_MODIFIED:
186 if (data->include_changed)
187 add_path = delta->new_file.path;
188 break;
189
190 case GIT_DELTA_DELETED:
191 if (data->include_changed &&
192 !git_index_find(NULL, index, delta->old_file.path))
193 error = git_index_remove(index, delta->old_file.path, 0);
194 break;
195
196 default:
197 /* Unimplemented */
198 giterr_set(
199 GITERR_INVALID,
200 "Cannot update index. Unimplemented status (%d)",
201 delta->status);
202 return -1;
203 }
204
205 if (add_path != NULL)
206 error = git_index_add_bypath(index, add_path);
207 }
208
209 return error;
210 }
211
212 static int build_untracked_tree(
213 git_tree **tree_out,
214 git_index *index,
215 git_commit *i_commit,
216 uint32_t flags)
217 {
218 git_tree *i_tree = NULL;
219 git_diff *diff = NULL;
220 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
221 struct stash_update_rules data = {0};
222 int error;
223
224 git_index_clear(index);
225
226 if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
227 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
228 GIT_DIFF_RECURSE_UNTRACKED_DIRS;
229 data.include_untracked = true;
230 }
231
232 if (flags & GIT_STASH_INCLUDE_IGNORED) {
233 opts.flags |= GIT_DIFF_INCLUDE_IGNORED |
234 GIT_DIFF_RECURSE_IGNORED_DIRS;
235 data.include_ignored = true;
236 }
237
238 if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
239 goto cleanup;
240
241 if ((error = git_diff_tree_to_workdir(
242 &diff, git_index_owner(index), i_tree, &opts)) < 0)
243 goto cleanup;
244
245 if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
246 goto cleanup;
247
248 error = build_tree_from_index(tree_out, index);
249
250 cleanup:
251 git_diff_free(diff);
252 git_tree_free(i_tree);
253 return error;
254 }
255
256 static int commit_untracked(
257 git_commit **u_commit,
258 git_index *index,
259 const git_signature *stasher,
260 const char *message,
261 git_commit *i_commit,
262 uint32_t flags)
263 {
264 git_tree *u_tree = NULL;
265 git_oid u_commit_oid;
266 git_buf msg = GIT_BUF_INIT;
267 int error;
268
269 if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0)
270 goto cleanup;
271
272 if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0)
273 goto cleanup;
274
275 if ((error = git_commit_create(
276 &u_commit_oid,
277 git_index_owner(index),
278 NULL,
279 stasher,
280 stasher,
281 NULL,
282 git_buf_cstr(&msg),
283 u_tree,
284 0,
285 NULL)) < 0)
286 goto cleanup;
287
288 error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
289
290 cleanup:
291 git_tree_free(u_tree);
292 git_buf_free(&msg);
293 return error;
294 }
295
296 static git_diff_delta *stash_delta_merge(
297 const git_diff_delta *a,
298 const git_diff_delta *b,
299 git_pool *pool)
300 {
301 /* Special case for stash: if a file is deleted in the index, but exists
302 * in the working tree, we need to stash the workdir copy for the workdir.
303 */
304 if (a->status == GIT_DELTA_DELETED && b->status == GIT_DELTA_UNTRACKED) {
305 git_diff_delta *dup = git_diff__delta_dup(b, pool);
306
307 if (dup)
308 dup->status = GIT_DELTA_MODIFIED;
309 return dup;
310 }
311
312 return git_diff__merge_like_cgit(a, b, pool);
313 }
314
315 static int build_workdir_tree(
316 git_tree **tree_out,
317 git_index *index,
318 git_commit *b_commit)
319 {
320 git_repository *repo = git_index_owner(index);
321 git_tree *b_tree = NULL;
322 git_diff *diff = NULL, *idx_to_wd = NULL;
323 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
324 struct stash_update_rules data = {0};
325 int error;
326
327 opts.flags = GIT_DIFF_IGNORE_SUBMODULES | GIT_DIFF_INCLUDE_UNTRACKED;
328
329 if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
330 goto cleanup;
331
332 if ((error = git_diff_tree_to_index(&diff, repo, b_tree, index, &opts)) < 0 ||
333 (error = git_diff_index_to_workdir(&idx_to_wd, repo, index, &opts)) < 0 ||
334 (error = git_diff__merge(diff, idx_to_wd, stash_delta_merge)) < 0)
335 goto cleanup;
336
337 data.include_changed = true;
338
339 if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
340 goto cleanup;
341
342 error = build_tree_from_index(tree_out, index);
343
344 cleanup:
345 git_diff_free(idx_to_wd);
346 git_diff_free(diff);
347 git_tree_free(b_tree);
348
349 return error;
350 }
351
352 static int commit_worktree(
353 git_oid *w_commit_oid,
354 git_index *index,
355 const git_signature *stasher,
356 const char *message,
357 git_commit *i_commit,
358 git_commit *b_commit,
359 git_commit *u_commit)
360 {
361 int error = 0;
362 git_tree *w_tree = NULL, *i_tree = NULL;
363 const git_commit *parents[] = { NULL, NULL, NULL };
364
365 parents[0] = b_commit;
366 parents[1] = i_commit;
367 parents[2] = u_commit;
368
369 if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
370 goto cleanup;
371
372 if ((error = git_index_read_tree(index, i_tree)) < 0)
373 goto cleanup;
374
375 if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0)
376 goto cleanup;
377
378 error = git_commit_create(
379 w_commit_oid,
380 git_index_owner(index),
381 NULL,
382 stasher,
383 stasher,
384 NULL,
385 message,
386 w_tree,
387 u_commit ? 3 : 2,
388 parents);
389
390 cleanup:
391 git_tree_free(i_tree);
392 git_tree_free(w_tree);
393 return error;
394 }
395
396 static int prepare_worktree_commit_message(
397 git_buf* msg,
398 const char *user_message)
399 {
400 git_buf buf = GIT_BUF_INIT;
401 int error;
402
403 if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
404 return error;
405
406 git_buf_clear(msg);
407
408 if (!user_message)
409 git_buf_printf(msg, "WIP on %s", git_buf_cstr(&buf));
410 else {
411 const char *colon;
412
413 if ((colon = strchr(git_buf_cstr(&buf), ':')) == NULL)
414 goto cleanup;
415
416 git_buf_puts(msg, "On ");
417 git_buf_put(msg, git_buf_cstr(&buf), colon - buf.ptr);
418 git_buf_printf(msg, ": %s\n", user_message);
419 }
420
421 error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
422
423 cleanup:
424 git_buf_free(&buf);
425
426 return error;
427 }
428
429 static int update_reflog(
430 git_oid *w_commit_oid,
431 git_repository *repo,
432 const char *message)
433 {
434 git_reference *stash;
435 int error;
436
437 if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0)
438 return error;
439
440 error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, message);
441
442 git_reference_free(stash);
443
444 return error;
445 }
446
447 static int is_dirty_cb(const char *path, unsigned int status, void *payload)
448 {
449 GIT_UNUSED(path);
450 GIT_UNUSED(status);
451 GIT_UNUSED(payload);
452
453 return GIT_PASSTHROUGH;
454 }
455
456 static int ensure_there_are_changes_to_stash(
457 git_repository *repo,
458 bool include_untracked_files,
459 bool include_ignored_files)
460 {
461 int error;
462 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
463
464 opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
465 opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
466
467 if (include_untracked_files)
468 opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
469 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
470
471 if (include_ignored_files)
472 opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
473 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
474
475 error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
476
477 if (error == GIT_PASSTHROUGH)
478 return 0;
479
480 if (!error)
481 return create_error(GIT_ENOTFOUND, "There is nothing to stash.");
482
483 return error;
484 }
485
486 static int reset_index_and_workdir(
487 git_repository *repo,
488 git_commit *commit,
489 bool remove_untracked,
490 bool remove_ignored)
491 {
492 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
493
494 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
495
496 if (remove_untracked)
497 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
498
499 if (remove_ignored)
500 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_IGNORED;
501
502 return git_checkout_tree(repo, (git_object *)commit, &opts);
503 }
504
505 int git_stash_save(
506 git_oid *out,
507 git_repository *repo,
508 const git_signature *stasher,
509 const char *message,
510 uint32_t flags)
511 {
512 git_index *index = NULL;
513 git_commit *b_commit = NULL, *i_commit = NULL, *u_commit = NULL;
514 git_buf msg = GIT_BUF_INIT;
515 int error;
516
517 assert(out && repo && stasher);
518
519 if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
520 return error;
521
522 if ((error = retrieve_base_commit_and_message(&b_commit, &msg, repo)) < 0)
523 goto cleanup;
524
525 if ((error = ensure_there_are_changes_to_stash(
526 repo,
527 (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
528 (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
529 goto cleanup;
530
531 if ((error = git_repository_index(&index, repo)) < 0)
532 goto cleanup;
533
534 if ((error = commit_index(
535 &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
536 goto cleanup;
537
538 if ((flags & (GIT_STASH_INCLUDE_UNTRACKED | GIT_STASH_INCLUDE_IGNORED)) &&
539 (error = commit_untracked(
540 &u_commit, index, stasher, git_buf_cstr(&msg),
541 i_commit, flags)) < 0)
542 goto cleanup;
543
544 if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
545 goto cleanup;
546
547 if ((error = commit_worktree(
548 out, index, stasher, git_buf_cstr(&msg),
549 i_commit, b_commit, u_commit)) < 0)
550 goto cleanup;
551
552 git_buf_rtrim(&msg);
553
554 if ((error = update_reflog(out, repo, git_buf_cstr(&msg))) < 0)
555 goto cleanup;
556
557 if ((error = reset_index_and_workdir(
558 repo,
559 ((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit,
560 (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
561 (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
562 goto cleanup;
563
564 cleanup:
565
566 git_buf_free(&msg);
567 git_commit_free(i_commit);
568 git_commit_free(b_commit);
569 git_commit_free(u_commit);
570 git_index_free(index);
571
572 return error;
573 }
574
575 static int retrieve_stash_commit(
576 git_commit **commit,
577 git_repository *repo,
578 size_t index)
579 {
580 git_reference *stash = NULL;
581 git_reflog *reflog = NULL;
582 int error;
583 size_t max;
584 const git_reflog_entry *entry;
585
586 if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
587 goto cleanup;
588
589 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
590 goto cleanup;
591
592 max = git_reflog_entrycount(reflog);
593 if (!max || index > max - 1) {
594 error = GIT_ENOTFOUND;
595 giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
596 goto cleanup;
597 }
598
599 entry = git_reflog_entry_byindex(reflog, index);
600 if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0)
601 goto cleanup;
602
603 cleanup:
604 git_reference_free(stash);
605 git_reflog_free(reflog);
606 return error;
607 }
608
609 static int retrieve_stash_trees(
610 git_tree **out_stash_tree,
611 git_tree **out_base_tree,
612 git_tree **out_index_tree,
613 git_tree **out_index_parent_tree,
614 git_tree **out_untracked_tree,
615 git_commit *stash_commit)
616 {
617 git_tree *stash_tree = NULL;
618 git_commit *base_commit = NULL;
619 git_tree *base_tree = NULL;
620 git_commit *index_commit = NULL;
621 git_tree *index_tree = NULL;
622 git_commit *index_parent_commit = NULL;
623 git_tree *index_parent_tree = NULL;
624 git_commit *untracked_commit = NULL;
625 git_tree *untracked_tree = NULL;
626 int error;
627
628 if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0)
629 goto cleanup;
630
631 if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0)
632 goto cleanup;
633 if ((error = git_commit_tree(&base_tree, base_commit)) < 0)
634 goto cleanup;
635
636 if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0)
637 goto cleanup;
638 if ((error = git_commit_tree(&index_tree, index_commit)) < 0)
639 goto cleanup;
640
641 if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0)
642 goto cleanup;
643 if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0)
644 goto cleanup;
645
646 if (git_commit_parentcount(stash_commit) == 3) {
647 if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0)
648 goto cleanup;
649 if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 0)
650 goto cleanup;
651 }
652
653 *out_stash_tree = stash_tree;
654 *out_base_tree = base_tree;
655 *out_index_tree = index_tree;
656 *out_index_parent_tree = index_parent_tree;
657 *out_untracked_tree = untracked_tree;
658
659 cleanup:
660 git_commit_free(untracked_commit);
661 git_commit_free(index_parent_commit);
662 git_commit_free(index_commit);
663 git_commit_free(base_commit);
664 if (error < 0) {
665 git_tree_free(stash_tree);
666 git_tree_free(base_tree);
667 git_tree_free(index_tree);
668 git_tree_free(index_parent_tree);
669 git_tree_free(untracked_tree);
670 }
671 return error;
672 }
673
674 static int merge_indexes(
675 git_index **out,
676 git_repository *repo,
677 git_tree *ancestor_tree,
678 git_index *ours_index,
679 git_index *theirs_index)
680 {
681 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
682 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
683 int error;
684
685 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
686
687 if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 ||
688 (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 ||
689 (error = git_iterator_for_index(&theirs, repo, theirs_index, &iter_opts)) < 0)
690 goto done;
691
692 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
693
694 done:
695 git_iterator_free(ancestor);
696 git_iterator_free(ours);
697 git_iterator_free(theirs);
698 return error;
699 }
700
701 static int merge_index_and_tree(
702 git_index **out,
703 git_repository *repo,
704 git_tree *ancestor_tree,
705 git_index *ours_index,
706 git_tree *theirs_tree)
707 {
708 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
709 git_iterator_options iter_opts = GIT_ITERATOR_OPTIONS_INIT;
710 int error;
711
712 iter_opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE;
713
714 if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, &iter_opts)) < 0 ||
715 (error = git_iterator_for_index(&ours, repo, ours_index, &iter_opts)) < 0 ||
716 (error = git_iterator_for_tree(&theirs, theirs_tree, &iter_opts)) < 0)
717 goto done;
718
719 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
720
721 done:
722 git_iterator_free(ancestor);
723 git_iterator_free(ours);
724 git_iterator_free(theirs);
725 return error;
726 }
727
728 static void normalize_apply_options(
729 git_stash_apply_options *opts,
730 const git_stash_apply_options *given_apply_opts)
731 {
732 if (given_apply_opts != NULL) {
733 memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options));
734 } else {
735 git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT;
736 memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
737 }
738
739 if ((opts->checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
740 opts->checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
741
742 if (!opts->checkout_options.our_label)
743 opts->checkout_options.our_label = "Updated upstream";
744
745 if (!opts->checkout_options.their_label)
746 opts->checkout_options.their_label = "Stashed changes";
747 }
748
749 int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version)
750 {
751 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
752 opts, version, git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_INIT);
753 return 0;
754 }
755
756 #define NOTIFY_PROGRESS(opts, progress_type) \
757 do { \
758 if ((opts).progress_cb && \
759 (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \
760 error = (error < 0) ? error : -1; \
761 goto cleanup; \
762 } \
763 } while(false);
764
765 static int ensure_clean_index(git_repository *repo, git_index *index)
766 {
767 git_tree *head_tree = NULL;
768 git_diff *index_diff = NULL;
769 int error = 0;
770
771 if ((error = git_repository_head_tree(&head_tree, repo)) < 0 ||
772 (error = git_diff_tree_to_index(
773 &index_diff, repo, head_tree, index, NULL)) < 0)
774 goto done;
775
776 if (git_diff_num_deltas(index_diff) > 0) {
777 giterr_set(GITERR_STASH, "%" PRIuZ " uncommitted changes exist in the index",
778 git_diff_num_deltas(index_diff));
779 error = GIT_EUNCOMMITTED;
780 }
781
782 done:
783 git_diff_free(index_diff);
784 git_tree_free(head_tree);
785 return error;
786 }
787
788 static int stage_new_file(const git_index_entry **entries, void *data)
789 {
790 git_index *index = data;
791
792 if(entries[0] == NULL)
793 return git_index_add(index, entries[1]);
794 else
795 return git_index_add(index, entries[0]);
796 }
797
798 static int stage_new_files(
799 git_index **out,
800 git_tree *parent_tree,
801 git_tree *tree)
802 {
803 git_iterator *iterators[2] = { NULL, NULL };
804 git_iterator_options iterator_options = GIT_ITERATOR_OPTIONS_INIT;
805 git_index *index = NULL;
806 int error;
807
808 if ((error = git_index_new(&index)) < 0 ||
809 (error = git_iterator_for_tree(
810 &iterators[0], parent_tree, &iterator_options)) < 0 ||
811 (error = git_iterator_for_tree(
812 &iterators[1], tree, &iterator_options)) < 0)
813 goto done;
814
815 error = git_iterator_walk(iterators, 2, stage_new_file, index);
816
817 done:
818 if (error < 0)
819 git_index_free(index);
820 else
821 *out = index;
822
823 git_iterator_free(iterators[0]);
824 git_iterator_free(iterators[1]);
825
826 return error;
827 }
828
829 int git_stash_apply(
830 git_repository *repo,
831 size_t index,
832 const git_stash_apply_options *given_opts)
833 {
834 git_stash_apply_options opts;
835 unsigned int checkout_strategy;
836 git_commit *stash_commit = NULL;
837 git_tree *stash_tree = NULL;
838 git_tree *stash_parent_tree = NULL;
839 git_tree *index_tree = NULL;
840 git_tree *index_parent_tree = NULL;
841 git_tree *untracked_tree = NULL;
842 git_index *stash_adds = NULL;
843 git_index *repo_index = NULL;
844 git_index *unstashed_index = NULL;
845 git_index *modified_index = NULL;
846 git_index *untracked_index = NULL;
847 int error;
848
849 GITERR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options");
850
851 normalize_apply_options(&opts, given_opts);
852 checkout_strategy = opts.checkout_options.checkout_strategy;
853
854 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH);
855
856 /* Retrieve commit corresponding to the given stash */
857 if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0)
858 goto cleanup;
859
860 /* Retrieve all trees in the stash */
861 if ((error = retrieve_stash_trees(
862 &stash_tree, &stash_parent_tree, &index_tree,
863 &index_parent_tree, &untracked_tree, stash_commit)) < 0)
864 goto cleanup;
865
866 /* Load repo index */
867 if ((error = git_repository_index(&repo_index, repo)) < 0)
868 goto cleanup;
869
870 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
871
872 if ((error = ensure_clean_index(repo, repo_index)) < 0)
873 goto cleanup;
874
875 /* Restore index if required */
876 if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) &&
877 git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
878
879 if ((error = merge_index_and_tree(
880 &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
881 goto cleanup;
882
883 if (git_index_has_conflicts(unstashed_index)) {
884 error = GIT_ECONFLICT;
885 goto cleanup;
886 }
887
888 /* Otherwise, stage any new files in the stash tree. (Note: their
889 * previously unstaged contents are staged, not the previously staged.)
890 */
891 } else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) {
892 if ((error = stage_new_files(
893 &stash_adds, stash_parent_tree, stash_tree)) < 0 ||
894 (error = merge_indexes(
895 &unstashed_index, repo, stash_parent_tree, repo_index, stash_adds)) < 0)
896 goto cleanup;
897 }
898
899 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED);
900
901 /* Restore modified files in workdir */
902 if ((error = merge_index_and_tree(
903 &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
904 goto cleanup;
905
906 /* If applicable, restore untracked / ignored files in workdir */
907 if (untracked_tree) {
908 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED);
909
910 if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
911 goto cleanup;
912 }
913
914 if (untracked_index) {
915 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
916
917 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED);
918
919 if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0)
920 goto cleanup;
921
922 opts.checkout_options.checkout_strategy = checkout_strategy;
923 }
924
925
926 /* If there are conflicts in the modified index, then we need to actually
927 * check that out as the repo's index. Otherwise, we don't update the
928 * index.
929 */
930
931 if (!git_index_has_conflicts(modified_index))
932 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
933
934 /* Check out the modified index using the existing repo index as baseline,
935 * so that existing modifications in the index can be rewritten even when
936 * checking out safely.
937 */
938 opts.checkout_options.baseline_index = repo_index;
939
940 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED);
941
942 if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0)
943 goto cleanup;
944
945 if (unstashed_index && !git_index_has_conflicts(modified_index)) {
946 if ((error = git_index_read_index(repo_index, unstashed_index)) < 0)
947 goto cleanup;
948 }
949
950 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE);
951
952 error = git_index_write(repo_index);
953
954 cleanup:
955 git_index_free(untracked_index);
956 git_index_free(modified_index);
957 git_index_free(unstashed_index);
958 git_index_free(stash_adds);
959 git_index_free(repo_index);
960 git_tree_free(untracked_tree);
961 git_tree_free(index_parent_tree);
962 git_tree_free(index_tree);
963 git_tree_free(stash_parent_tree);
964 git_tree_free(stash_tree);
965 git_commit_free(stash_commit);
966 return error;
967 }
968
969 int git_stash_foreach(
970 git_repository *repo,
971 git_stash_cb callback,
972 void *payload)
973 {
974 git_reference *stash;
975 git_reflog *reflog = NULL;
976 int error;
977 size_t i, max;
978 const git_reflog_entry *entry;
979
980 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
981 if (error == GIT_ENOTFOUND) {
982 giterr_clear();
983 return 0;
984 }
985 if (error < 0)
986 goto cleanup;
987
988 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
989 goto cleanup;
990
991 max = git_reflog_entrycount(reflog);
992 for (i = 0; i < max; i++) {
993 entry = git_reflog_entry_byindex(reflog, i);
994
995 error = callback(i,
996 git_reflog_entry_message(entry),
997 git_reflog_entry_id_new(entry),
998 payload);
999
1000 if (error) {
1001 giterr_set_after_callback(error);
1002 break;
1003 }
1004 }
1005
1006 cleanup:
1007 git_reference_free(stash);
1008 git_reflog_free(reflog);
1009 return error;
1010 }
1011
1012 int git_stash_drop(
1013 git_repository *repo,
1014 size_t index)
1015 {
1016 git_transaction *tx;
1017 git_reference *stash = NULL;
1018 git_reflog *reflog = NULL;
1019 size_t max;
1020 int error;
1021
1022 if ((error = git_transaction_new(&tx, repo)) < 0)
1023 return error;
1024
1025 if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0)
1026 goto cleanup;
1027
1028 if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
1029 goto cleanup;
1030
1031 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
1032 goto cleanup;
1033
1034 max = git_reflog_entrycount(reflog);
1035
1036 if (!max || index > max - 1) {
1037 error = GIT_ENOTFOUND;
1038 giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
1039 goto cleanup;
1040 }
1041
1042 if ((error = git_reflog_drop(reflog, index, true)) < 0)
1043 goto cleanup;
1044
1045 if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0)
1046 goto cleanup;
1047
1048 if (max == 1) {
1049 if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0)
1050 goto cleanup;
1051 } else if (index == 0) {
1052 const git_reflog_entry *entry;
1053
1054 entry = git_reflog_entry_byindex(reflog, 0);
1055 if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0)
1056 goto cleanup;
1057 }
1058
1059 error = git_transaction_commit(tx);
1060
1061 cleanup:
1062 git_reference_free(stash);
1063 git_transaction_free(tx);
1064 git_reflog_free(reflog);
1065 return error;
1066 }
1067
1068 int git_stash_pop(
1069 git_repository *repo,
1070 size_t index,
1071 const git_stash_apply_options *options)
1072 {
1073 int error;
1074
1075 if ((error = git_stash_apply(repo, index, options)) < 0)
1076 return error;
1077
1078 return git_stash_drop(repo, index);
1079 }