]> git.proxmox.com Git - libgit2.git/blob - src/stash.c
Merge pull request #3097 from libgit2/cmn/submodule-config-state
[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_index_and_tree(
675 git_index **out,
676 git_repository *repo,
677 git_tree *ancestor_tree,
678 git_index *ours_index,
679 git_tree *theirs_tree)
680 {
681 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
682 const git_iterator_flag_t flags = GIT_ITERATOR_DONT_IGNORE_CASE;
683 int error;
684
685 if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, flags, NULL, NULL)) < 0 ||
686 (error = git_iterator_for_index(&ours, ours_index, flags, NULL, NULL)) < 0 ||
687 (error = git_iterator_for_tree(&theirs, theirs_tree, flags, NULL, NULL)) < 0)
688 goto done;
689
690 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
691
692 done:
693 git_iterator_free(ancestor);
694 git_iterator_free(ours);
695 git_iterator_free(theirs);
696 return error;
697 }
698
699 static void normalize_apply_options(
700 git_stash_apply_options *opts,
701 const git_stash_apply_options *given_apply_opts)
702 {
703 if (given_apply_opts != NULL) {
704 memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options));
705 } else {
706 git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT;
707 memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
708 }
709
710 if ((opts->checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
711 opts->checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
712
713 if (!opts->checkout_options.our_label)
714 opts->checkout_options.our_label = "Updated upstream";
715
716 if (!opts->checkout_options.their_label)
717 opts->checkout_options.their_label = "Stashed changes";
718 }
719
720 int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version)
721 {
722 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
723 opts, version, git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_INIT);
724 return 0;
725 }
726
727 #define NOTIFY_PROGRESS(opts, progress_type) \
728 do { \
729 if ((opts).progress_cb && \
730 (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \
731 error = (error < 0) ? error : -1; \
732 goto cleanup; \
733 } \
734 } while(false);
735
736 int git_stash_apply(
737 git_repository *repo,
738 size_t index,
739 const git_stash_apply_options *given_opts)
740 {
741 git_stash_apply_options opts;
742 unsigned int checkout_strategy;
743 git_commit *stash_commit = NULL;
744 git_tree *stash_tree = NULL;
745 git_tree *stash_parent_tree = NULL;
746 git_tree *index_tree = NULL;
747 git_tree *index_parent_tree = NULL;
748 git_tree *untracked_tree = NULL;
749 git_index *repo_index = NULL;
750 git_index *unstashed_index = NULL;
751 git_index *modified_index = NULL;
752 git_index *untracked_index = NULL;
753 int error;
754
755 GITERR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options");
756
757 normalize_apply_options(&opts, given_opts);
758 checkout_strategy = opts.checkout_options.checkout_strategy;
759
760 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH);
761
762 /* Retrieve commit corresponding to the given stash */
763 if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0)
764 goto cleanup;
765
766 /* Retrieve all trees in the stash */
767 if ((error = retrieve_stash_trees(
768 &stash_tree, &stash_parent_tree, &index_tree,
769 &index_parent_tree, &untracked_tree, stash_commit)) < 0)
770 goto cleanup;
771
772 /* Load repo index */
773 if ((error = git_repository_index(&repo_index, repo)) < 0)
774 goto cleanup;
775
776 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
777
778 /* Restore index if required */
779 if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) &&
780 git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
781
782 if ((error = merge_index_and_tree(
783 &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
784 goto cleanup;
785
786 if (git_index_has_conflicts(unstashed_index)) {
787 error = GIT_ECONFLICT;
788 goto cleanup;
789 }
790 }
791
792 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED);
793
794 /* Restore modified files in workdir */
795 if ((error = merge_index_and_tree(
796 &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
797 goto cleanup;
798
799 /* If applicable, restore untracked / ignored files in workdir */
800 if (untracked_tree) {
801 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED);
802
803 if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
804 goto cleanup;
805 }
806
807 if (untracked_index) {
808 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
809
810 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED);
811
812 if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0)
813 goto cleanup;
814
815 opts.checkout_options.checkout_strategy = checkout_strategy;
816 }
817
818
819 /* If there are conflicts in the modified index, then we need to actually
820 * check that out as the repo's index. Otherwise, we don't update the
821 * index.
822 */
823
824 if (!git_index_has_conflicts(modified_index))
825 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
826
827 /* Check out the modified index using the existing repo index as baseline,
828 * so that existing modifications in the index can be rewritten even when
829 * checking out safely.
830 */
831 opts.checkout_options.baseline_index = repo_index;
832
833 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED);
834
835 if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0)
836 goto cleanup;
837
838 if (unstashed_index && !git_index_has_conflicts(modified_index)) {
839 if ((error = git_index_read_index(repo_index, unstashed_index)) < 0)
840 goto cleanup;
841 }
842
843 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE);
844
845 error = git_index_write(repo_index);
846
847 cleanup:
848 git_index_free(untracked_index);
849 git_index_free(modified_index);
850 git_index_free(unstashed_index);
851 git_index_free(repo_index);
852 git_tree_free(untracked_tree);
853 git_tree_free(index_parent_tree);
854 git_tree_free(index_tree);
855 git_tree_free(stash_parent_tree);
856 git_tree_free(stash_tree);
857 git_commit_free(stash_commit);
858 return error;
859 }
860
861 int git_stash_foreach(
862 git_repository *repo,
863 git_stash_cb callback,
864 void *payload)
865 {
866 git_reference *stash;
867 git_reflog *reflog = NULL;
868 int error;
869 size_t i, max;
870 const git_reflog_entry *entry;
871
872 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
873 if (error == GIT_ENOTFOUND) {
874 giterr_clear();
875 return 0;
876 }
877 if (error < 0)
878 goto cleanup;
879
880 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
881 goto cleanup;
882
883 max = git_reflog_entrycount(reflog);
884 for (i = 0; i < max; i++) {
885 entry = git_reflog_entry_byindex(reflog, i);
886
887 error = callback(i,
888 git_reflog_entry_message(entry),
889 git_reflog_entry_id_new(entry),
890 payload);
891
892 if (error) {
893 giterr_set_after_callback(error);
894 break;
895 }
896 }
897
898 cleanup:
899 git_reference_free(stash);
900 git_reflog_free(reflog);
901 return error;
902 }
903
904 int git_stash_drop(
905 git_repository *repo,
906 size_t index)
907 {
908 git_transaction *tx;
909 git_reference *stash = NULL;
910 git_reflog *reflog = NULL;
911 size_t max;
912 int error;
913
914 if ((error = git_transaction_new(&tx, repo)) < 0)
915 return error;
916
917 if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0)
918 goto cleanup;
919
920 if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
921 goto cleanup;
922
923 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
924 goto cleanup;
925
926 max = git_reflog_entrycount(reflog);
927
928 if (!max || index > max - 1) {
929 error = GIT_ENOTFOUND;
930 giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
931 goto cleanup;
932 }
933
934 if ((error = git_reflog_drop(reflog, index, true)) < 0)
935 goto cleanup;
936
937 if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0)
938 goto cleanup;
939
940 if (max == 1) {
941 if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0)
942 goto cleanup;
943 } else if (index == 0) {
944 const git_reflog_entry *entry;
945
946 entry = git_reflog_entry_byindex(reflog, 0);
947 if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0)
948 goto cleanup;
949 }
950
951 error = git_transaction_commit(tx);
952
953 cleanup:
954 git_reference_free(stash);
955 git_transaction_free(tx);
956 git_reflog_free(reflog);
957 return error;
958 }
959
960 int git_stash_pop(
961 git_repository *repo,
962 size_t index,
963 const git_stash_apply_options *options)
964 {
965 int error;
966
967 if ((error = git_stash_apply(repo, index, options)) < 0)
968 return error;
969
970 return git_stash_drop(repo, index);
971 }