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