]> git.proxmox.com Git - libgit2.git/blame - src/stash.c
Merge pull request #3139 from ethomson/diff_conflicts
[libgit2.git] / src / stash.c
CommitLineData
590fb68b 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
590fb68b 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"
7f26b1b9 11#include "message.h"
590fb68b 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"
114f5a6c 18#include "git2/index.h"
f99ca523 19#include "git2/transaction.h"
bf8dd3f5 20#include "git2/merge.h"
f0957589 21#include "index.h"
4ec197f3 22#include "signature.h"
f0957589
ET
23#include "iterator.h"
24#include "merge.h"
590fb68b 25
26static int create_error(int error, const char *msg)
27{
28 giterr_set(GITERR_STASH, "Cannot stash changes - %s", msg);
29 return error;
30}
31
590fb68b 32static int retrieve_head(git_reference **out, git_repository *repo)
33{
34 int error = git_repository_head(out, repo);
35
605da51a 36 if (error == GIT_EUNBORNBRANCH)
590fb68b 37 return create_error(error, "You do not have the initial commit yet.");
38
39 return error;
40}
41
42static 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
55static int append_commit_description(git_buf *out, git_commit* commit)
56{
7f26b1b9
ET
57 const char *summary = git_commit_summary(commit);
58 GITERR_CHECK_ALLOC(summary);
590fb68b 59
60 if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
61 return -1;
62
590fb68b 63 git_buf_putc(out, ' ');
7f26b1b9 64 git_buf_puts(out, summary);
590fb68b 65 git_buf_putc(out, '\n');
66
67 return git_buf_oom(out) ? -1 : 0;
68}
69
70static 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
590fb68b 81 if (strcmp("HEAD", git_reference_name(head)) == 0)
a6a82e1a 82 error = git_buf_puts(stash_message, "(no branch): ");
590fb68b 83 else
a6a82e1a 84 error = git_buf_printf(
590fb68b 85 stash_message,
86 "%s: ",
87 git_reference_name(head) + strlen(GIT_REFS_HEADS_DIR));
a6a82e1a 88 if (error < 0)
590fb68b 89 goto cleanup;
90
a6a82e1a
RB
91 if ((error = git_commit_lookup(
92 b_commit, repo, git_reference_target(head))) < 0)
590fb68b 93 goto cleanup;
94
a6a82e1a
RB
95 if ((error = append_commit_description(stash_message, *b_commit)) < 0)
96 goto cleanup;
590fb68b 97
98cleanup:
99 git_reference_free(head);
100 return error;
101}
102
103static int build_tree_from_index(git_tree **out, git_index *index)
104{
a6a82e1a 105 int error;
590fb68b 106 git_oid i_tree_oid;
107
a6a82e1a 108 if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
44972873 109 return error;
590fb68b 110
111 return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
112}
113
114static int commit_index(
115 git_commit **i_commit,
116 git_index *index,
2274993b 117 const git_signature *stasher,
590fb68b 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;
a6a82e1a 124 int error;
590fb68b 125
a6a82e1a 126 if ((error = build_tree_from_index(&i_tree, index)) < 0)
590fb68b 127 goto cleanup;
128
a6a82e1a 129 if ((error = git_buf_printf(&msg, "index on %s\n", message)) < 0)
590fb68b 130 goto cleanup;
131
a6a82e1a 132 if ((error = git_commit_create(
590fb68b 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,
a6a82e1a
RB
142 &parent)) < 0)
143 goto cleanup;
590fb68b 144
145 error = git_commit_lookup(i_commit, git_index_owner(index), &i_commit_oid);
146
147cleanup:
148 git_tree_free(i_tree);
149 git_buf_free(&msg);
150 return error;
151}
152
10672e3e 153struct stash_update_rules {
590fb68b 154 bool include_changed;
155 bool include_untracked;
156 bool include_ignored;
157};
158
10672e3e
RB
159static int stash_update_index_from_diff(
160 git_index *index,
161 const git_diff *diff,
162 struct stash_update_rules *data)
590fb68b 163{
10672e3e
RB
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:
24d17de2
RB
178 if (data->include_untracked &&
179 delta->new_file.mode != GIT_FILEMODE_TREE)
10672e3e 180 add_path = delta->new_file.path;
590fb68b 181 break;
590fb68b 182
10672e3e
RB
183 case GIT_DELTA_ADDED:
184 case GIT_DELTA_MODIFIED:
185 if (data->include_changed)
186 add_path = delta->new_file.path;
187 break;
a6a82e1a 188
10672e3e
RB
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;
590fb68b 209}
210
211static 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;
3ff1d123 218 git_diff *diff = NULL;
2f8d30be 219 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
10672e3e 220 struct stash_update_rules data = {0};
a6a82e1a 221 int error;
590fb68b 222
223 git_index_clear(index);
224
590fb68b 225 if (flags & GIT_STASH_INCLUDE_UNTRACKED) {
a6a82e1a
RB
226 opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED |
227 GIT_DIFF_RECURSE_UNTRACKED_DIRS;
590fb68b 228 data.include_untracked = true;
229 }
230
231 if (flags & GIT_STASH_INCLUDE_IGNORED) {
7b7aa75f
JG
232 opts.flags |= GIT_DIFF_INCLUDE_IGNORED |
233 GIT_DIFF_RECURSE_IGNORED_DIRS;
590fb68b 234 data.include_ignored = true;
235 }
236
a6a82e1a 237 if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
590fb68b 238 goto cleanup;
239
a6a82e1a
RB
240 if ((error = git_diff_tree_to_workdir(
241 &diff, git_index_owner(index), i_tree, &opts)) < 0)
590fb68b 242 goto cleanup;
243
10672e3e 244 if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
590fb68b 245 goto cleanup;
246
a6a82e1a 247 error = build_tree_from_index(tree_out, index);
590fb68b 248
249cleanup:
3ff1d123 250 git_diff_free(diff);
590fb68b 251 git_tree_free(i_tree);
252 return error;
253}
254
255static int commit_untracked(
256 git_commit **u_commit,
257 git_index *index,
2274993b 258 const git_signature *stasher,
590fb68b 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;
a6a82e1a 266 int error;
590fb68b 267
a6a82e1a 268 if ((error = build_untracked_tree(&u_tree, index, i_commit, flags)) < 0)
590fb68b 269 goto cleanup;
270
a6a82e1a 271 if ((error = git_buf_printf(&msg, "untracked files on %s\n", message)) < 0)
590fb68b 272 goto cleanup;
273
a6a82e1a 274 if ((error = git_commit_create(
590fb68b 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,
a6a82e1a
RB
284 NULL)) < 0)
285 goto cleanup;
590fb68b 286
287 error = git_commit_lookup(u_commit, git_index_owner(index), &u_commit_oid);
288
289cleanup:
290 git_tree_free(u_tree);
291 git_buf_free(&msg);
292 return error;
293}
294
295static int build_workdir_tree(
296 git_tree **tree_out,
297 git_index *index,
298 git_commit *b_commit)
299{
5735bf5e 300 git_repository *repo = git_index_owner(index);
590fb68b 301 git_tree *b_tree = NULL;
10672e3e 302 git_diff *diff = NULL;
2f8d30be 303 git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
10672e3e 304 struct stash_update_rules data = {0};
a6a82e1a 305 int error;
590fb68b 306
4fe0b0b3
JSS
307 opts.flags = GIT_DIFF_IGNORE_SUBMODULES;
308
a6a82e1a 309 if ((error = git_commit_tree(&b_tree, b_commit)) < 0)
590fb68b 310 goto cleanup;
311
09866d6f 312 if ((error = git_diff_tree_to_workdir(&diff, repo, b_tree, &opts)) < 0)
590fb68b 313 goto cleanup;
314
590fb68b 315 data.include_changed = true;
316
10672e3e 317 if ((error = stash_update_index_from_diff(index, diff, &data)) < 0)
590fb68b 318 goto cleanup;
319
10672e3e 320 error = build_tree_from_index(tree_out, index);
590fb68b 321
322cleanup:
3ff1d123 323 git_diff_free(diff);
590fb68b 324 git_tree_free(b_tree);
a6a82e1a 325
590fb68b 326 return error;
327}
328
329static int commit_worktree(
330 git_oid *w_commit_oid,
331 git_index *index,
2274993b 332 const git_signature *stasher,
590fb68b 333 const char *message,
334 git_commit *i_commit,
335 git_commit *b_commit,
336 git_commit *u_commit)
337{
a6a82e1a 338 int error = 0;
590fb68b 339 git_tree *w_tree = NULL, *i_tree = NULL;
590fb68b 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
a6a82e1a
RB
346 if ((error = git_commit_tree(&i_tree, i_commit)) < 0)
347 goto cleanup;
590fb68b 348
a6a82e1a 349 if ((error = git_index_read_tree(index, i_tree)) < 0)
590fb68b 350 goto cleanup;
351
a6a82e1a 352 if ((error = build_workdir_tree(&w_tree, index, b_commit)) < 0)
590fb68b 353 goto cleanup;
354
a6a82e1a 355 error = git_commit_create(
590fb68b 356 w_commit_oid,
357 git_index_owner(index),
358 NULL,
359 stasher,
360 stasher,
361 NULL,
362 message,
363 w_tree,
a6a82e1a
RB
364 u_commit ? 3 : 2,
365 parents);
590fb68b 366
367cleanup:
368 git_tree_free(i_tree);
369 git_tree_free(w_tree);
370 return error;
371}
372
373static int prepare_worktree_commit_message(
374 git_buf* msg,
375 const char *user_message)
376{
377 git_buf buf = GIT_BUF_INIT;
a6a82e1a
RB
378 int error;
379
6f58332f
RB
380 if ((error = git_buf_set(&buf, git_buf_cstr(msg), git_buf_len(msg))) < 0)
381 return error;
590fb68b 382
590fb68b 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
6f58332f 398 error = (git_buf_oom(msg) || git_buf_oom(&buf)) ? -1 : 0;
590fb68b 399
400cleanup:
401 git_buf_free(&buf);
a6a82e1a 402
590fb68b 403 return error;
404}
405
406static int update_reflog(
407 git_oid *w_commit_oid,
408 git_repository *repo,
590fb68b 409 const char *message)
410{
b976f3c2 411 git_reference *stash;
590fb68b 412 int error;
413
8d5ec910
CMN
414 if ((error = git_reference_ensure_log(repo, GIT_REFS_STASH_FILE)) < 0)
415 return error;
590fb68b 416
659cf202 417 error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1, message);
590fb68b 418
b976f3c2 419 git_reference_free(stash);
590fb68b 420
590fb68b 421 return error;
422}
423
424static 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
25e0b157 430 return GIT_PASSTHROUGH;
590fb68b 431}
432
433static 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;
79cfa20d 439 git_status_options opts = GIT_STATUS_OPTIONS_INIT;
590fb68b 440
590fb68b 441 opts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
4fe0b0b3
JSS
442 opts.flags = GIT_STATUS_OPT_EXCLUDE_SUBMODULES;
443
590fb68b 444 if (include_untracked_files)
4fe0b0b3 445 opts.flags |= GIT_STATUS_OPT_INCLUDE_UNTRACKED |
7b7aa75f 446 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
590fb68b 447
448 if (include_ignored_files)
7b7aa75f
JG
449 opts.flags |= GIT_STATUS_OPT_INCLUDE_IGNORED |
450 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS;
590fb68b 451
452 error = git_status_foreach_ext(repo, &opts, is_dirty_cb, NULL);
453
25e0b157 454 if (error == GIT_PASSTHROUGH)
590fb68b 455 return 0;
456
457 if (!error)
458 return create_error(GIT_ENOTFOUND, "There is nothing to stash.");
459
460 return error;
461}
462
463static int reset_index_and_workdir(
464 git_repository *repo,
465 git_commit *commit,
4636ca93
JG
466 bool remove_untracked,
467 bool remove_ignored)
590fb68b 468{
6affd71f 469 git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
590fb68b 470
cf208031 471 opts.checkout_strategy = GIT_CHECKOUT_FORCE;
590fb68b 472
473 if (remove_untracked)
474 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_UNTRACKED;
475
4636ca93
JG
476 if (remove_ignored)
477 opts.checkout_strategy |= GIT_CHECKOUT_REMOVE_IGNORED;
478
590fb68b 479 return git_checkout_tree(repo, (git_object *)commit, &opts);
480}
481
482int git_stash_save(
483 git_oid *out,
484 git_repository *repo,
2274993b 485 const git_signature *stasher,
590fb68b 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
a6a82e1a 496 if ((error = git_repository__ensure_not_bare(repo, "stash save")) < 0)
590fb68b 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,
a6a82e1a
RB
504 (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
505 (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
590fb68b 506 goto cleanup;
507
a6a82e1a 508 if ((error = git_repository_index(&index, repo)) < 0)
590fb68b 509 goto cleanup;
510
a6a82e1a
RB
511 if ((error = commit_index(
512 &i_commit, index, stasher, git_buf_cstr(&msg), b_commit)) < 0)
590fb68b 513 goto cleanup;
514
a6a82e1a
RB
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)
590fb68b 519 goto cleanup;
520
a6a82e1a 521 if ((error = prepare_worktree_commit_message(&msg, message)) < 0)
590fb68b 522 goto cleanup;
523
a6a82e1a
RB
524 if ((error = commit_worktree(
525 out, index, stasher, git_buf_cstr(&msg),
526 i_commit, b_commit, u_commit)) < 0)
590fb68b 527 goto cleanup;
528
529 git_buf_rtrim(&msg);
a6a82e1a 530
659cf202 531 if ((error = update_reflog(out, repo, git_buf_cstr(&msg))) < 0)
590fb68b 532 goto cleanup;
533
a6a82e1a 534 if ((error = reset_index_and_workdir(
590fb68b 535 repo,
a6a82e1a 536 ((flags & GIT_STASH_KEEP_INDEX) != 0) ? i_commit : b_commit,
4636ca93
JG
537 (flags & GIT_STASH_INCLUDE_UNTRACKED) != 0,
538 (flags & GIT_STASH_INCLUDE_IGNORED) != 0)) < 0)
590fb68b 539 goto cleanup;
540
590fb68b 541cleanup:
a6a82e1a 542
590fb68b 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);
a6a82e1a 548
590fb68b 549 return error;
550}
23388413 551
bf8dd3f5
POL
552static 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);
90f8408d 570 if (!max || index > max - 1) {
bf8dd3f5
POL
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
580cleanup:
581 git_reference_free(stash);
582 git_reflog_free(reflog);
583 return error;
584}
585
586static 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
636cleanup:
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
f0957589
ET
651static int merge_index_and_tree(
652 git_index **out,
bf8dd3f5 653 git_repository *repo,
f0957589
ET
654 git_tree *ancestor_tree,
655 git_index *ours_index,
656 git_tree *theirs_tree)
bf8dd3f5 657{
f0957589
ET
658 git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
659 const git_iterator_flag_t flags = GIT_ITERATOR_DONT_IGNORE_CASE;
bf8dd3f5 660 int error;
bf8dd3f5 661
f0957589
ET
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;
bf8dd3f5 666
f0957589 667 error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
bf8dd3f5 668
f0957589
ET
669done:
670 git_iterator_free(ancestor);
671 git_iterator_free(ours);
672 git_iterator_free(theirs);
bf8dd3f5
POL
673 return error;
674}
675
19c80a6f
ET
676static void normalize_apply_options(
677 git_stash_apply_options *opts,
678 const git_stash_apply_options *given_apply_opts)
f0957589 679{
19c80a6f
ET
680 if (given_apply_opts != NULL) {
681 memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options));
bf8dd3f5 682 } else {
19c80a6f
ET
683 git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT;
684 memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
bf8dd3f5
POL
685 }
686
19c80a6f
ET
687 if ((opts->checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
688 opts->checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
12149a20 689
19c80a6f
ET
690 if (!opts->checkout_options.our_label)
691 opts->checkout_options.our_label = "Updated upstream";
f0957589 692
19c80a6f
ET
693 if (!opts->checkout_options.their_label)
694 opts->checkout_options.their_label = "Stashed changes";
695}
696
697int 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;
bf8dd3f5
POL
702}
703
3e529e9d
CMN
704#define NOTIFY_PROGRESS(opts, progress_type) \
705 do { \
706 if ((opts).progress_cb && \
707 (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \
708 error = (error < 0) ? error : -1; \
709 goto cleanup; \
710 } \
711 } while(false);
4ea3eebf 712
bf8dd3f5
POL
713int git_stash_apply(
714 git_repository *repo,
715 size_t index,
19c80a6f 716 const git_stash_apply_options *given_opts)
bf8dd3f5 717{
19c80a6f 718 git_stash_apply_options opts;
f0957589 719 unsigned int checkout_strategy;
bf8dd3f5
POL
720 git_commit *stash_commit = NULL;
721 git_tree *stash_tree = NULL;
f0957589 722 git_tree *stash_parent_tree = NULL;
bf8dd3f5
POL
723 git_tree *index_tree = NULL;
724 git_tree *index_parent_tree = NULL;
725 git_tree *untracked_tree = NULL;
726 git_index *repo_index = NULL;
f0957589
ET
727 git_index *unstashed_index = NULL;
728 git_index *modified_index = NULL;
729 git_index *untracked_index = NULL;
bf8dd3f5
POL
730 int error;
731
19c80a6f
ET
732 GITERR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options");
733
734 normalize_apply_options(&opts, given_opts);
735 checkout_strategy = opts.checkout_options.checkout_strategy;
f0957589 736
4ea3eebf
ET
737 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH);
738
bf8dd3f5
POL
739 /* Retrieve commit corresponding to the given stash */
740 if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0)
741 goto cleanup;
742
743 /* Retrieve all trees in the stash */
744 if ((error = retrieve_stash_trees(
f0957589 745 &stash_tree, &stash_parent_tree, &index_tree,
bf8dd3f5
POL
746 &index_parent_tree, &untracked_tree, stash_commit)) < 0)
747 goto cleanup;
748
749 /* Load repo index */
750 if ((error = git_repository_index(&repo_index, repo)) < 0)
751 goto cleanup;
752
4ea3eebf
ET
753 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
754
bf8dd3f5 755 /* Restore index if required */
19c80a6f 756 if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) &&
f0957589 757 git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
bf8dd3f5 758
f0957589
ET
759 if ((error = merge_index_and_tree(
760 &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
bf8dd3f5 761 goto cleanup;
bf8dd3f5 762
f0957589 763 if (git_index_has_conflicts(unstashed_index)) {
f78bb2af 764 error = GIT_EMERGECONFLICT;
bf8dd3f5 765 goto cleanup;
f0957589 766 }
bf8dd3f5
POL
767 }
768
4ea3eebf
ET
769 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED);
770
bf8dd3f5 771 /* Restore modified files in workdir */
f0957589
ET
772 if ((error = merge_index_and_tree(
773 &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
bf8dd3f5
POL
774 goto cleanup;
775
f0957589 776 /* If applicable, restore untracked / ignored files in workdir */
4ea3eebf
ET
777 if (untracked_tree) {
778 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED);
779
780 if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
781 goto cleanup;
782 }
bf8dd3f5 783
f0957589 784 if (untracked_index) {
19c80a6f 785 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
f0957589 786
4ea3eebf
ET
787 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED);
788
19c80a6f 789 if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0)
f0957589
ET
790 goto cleanup;
791
19c80a6f 792 opts.checkout_options.checkout_strategy = checkout_strategy;
f0957589
ET
793 }
794
795
796 /* If there are conflicts in the modified index, then we need to actually
797 * check that out as the repo's index. Otherwise, we don't update the
798 * index.
799 */
800
801 if (!git_index_has_conflicts(modified_index))
19c80a6f 802 opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
f0957589
ET
803
804 /* Check out the modified index using the existing repo index as baseline,
805 * so that existing modifications in the index can be rewritten even when
806 * checking out safely.
807 */
19c80a6f 808 opts.checkout_options.baseline_index = repo_index;
f0957589 809
4ea3eebf
ET
810 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED);
811
19c80a6f 812 if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0)
bf8dd3f5
POL
813 goto cleanup;
814
f0957589
ET
815 if (unstashed_index && !git_index_has_conflicts(modified_index)) {
816 if ((error = git_index_read_index(repo_index, unstashed_index)) < 0)
817 goto cleanup;
818 }
819
4ea3eebf
ET
820 NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE);
821
bf8dd3f5 822cleanup:
f0957589
ET
823 git_index_free(untracked_index);
824 git_index_free(modified_index);
825 git_index_free(unstashed_index);
bf8dd3f5
POL
826 git_index_free(repo_index);
827 git_tree_free(untracked_tree);
828 git_tree_free(index_parent_tree);
829 git_tree_free(index_tree);
f0957589 830 git_tree_free(stash_parent_tree);
bf8dd3f5
POL
831 git_tree_free(stash_tree);
832 git_commit_free(stash_commit);
833 return error;
834}
835
23388413 836int git_stash_foreach(
837 git_repository *repo,
1d8ec670 838 git_stash_cb callback,
23388413 839 void *payload)
840{
841 git_reference *stash;
842 git_reflog *reflog = NULL;
843 int error;
844 size_t i, max;
845 const git_reflog_entry *entry;
846
847 error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE);
52c52737
RB
848 if (error == GIT_ENOTFOUND) {
849 giterr_clear();
23388413 850 return 0;
52c52737 851 }
23388413 852 if (error < 0)
853 goto cleanup;
854
b976f3c2 855 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
23388413 856 goto cleanup;
857
858 max = git_reflog_entrycount(reflog);
859 for (i = 0; i < max; i++) {
b15df1d9 860 entry = git_reflog_entry_byindex(reflog, i);
a6a82e1a 861
c7b3e1b3
RB
862 error = callback(i,
863 git_reflog_entry_message(entry),
864 git_reflog_entry_id_new(entry),
865 payload);
866
867 if (error) {
26c1cb91 868 giterr_set_after_callback(error);
dab89f9b 869 break;
c7b3e1b3 870 }
23388413 871 }
872
23388413 873cleanup:
874 git_reference_free(stash);
875 git_reflog_free(reflog);
876 return error;
877}
e4c64cf2 878
879int git_stash_drop(
880 git_repository *repo,
881 size_t index)
882{
f99ca523
CMN
883 git_transaction *tx;
884 git_reference *stash = NULL;
e4c64cf2 885 git_reflog *reflog = NULL;
886 size_t max;
887 int error;
888
f99ca523 889 if ((error = git_transaction_new(&tx, repo)) < 0)
e4c64cf2 890 return error;
891
c327d5db 892 if ((error = git_transaction_lock_ref(tx, GIT_REFS_STASH_FILE)) < 0)
f99ca523
CMN
893 goto cleanup;
894
895 if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
896 goto cleanup;
897
b976f3c2 898 if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
e4c64cf2 899 goto cleanup;
900
901 max = git_reflog_entrycount(reflog);
902
90f8408d 903 if (!max || index > max - 1) {
e4c64cf2 904 error = GIT_ENOTFOUND;
905 giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
906 goto cleanup;
907 }
908
b15df1d9 909 if ((error = git_reflog_drop(reflog, index, true)) < 0)
e4c64cf2 910 goto cleanup;
911
f99ca523 912 if ((error = git_transaction_set_reflog(tx, GIT_REFS_STASH_FILE, reflog)) < 0)
e4c64cf2 913 goto cleanup;
914
915 if (max == 1) {
f99ca523
CMN
916 if ((error = git_transaction_remove(tx, GIT_REFS_STASH_FILE)) < 0)
917 goto cleanup;
9ccab8df 918 } else if (index == 0) {
919 const git_reflog_entry *entry;
920
921 entry = git_reflog_entry_byindex(reflog, 0);
f99ca523 922 if ((error = git_transaction_set_target(tx, GIT_REFS_STASH_FILE, &entry->oid_cur, NULL, NULL)) < 0)
a57dd3b7 923 goto cleanup;
e4c64cf2 924 }
925
f99ca523
CMN
926 error = git_transaction_commit(tx);
927
e4c64cf2 928cleanup:
929 git_reference_free(stash);
f99ca523 930 git_transaction_free(tx);
e4c64cf2 931 git_reflog_free(reflog);
932 return error;
933}
bf8dd3f5
POL
934
935int git_stash_pop(
936 git_repository *repo,
937 size_t index,
19c80a6f 938 const git_stash_apply_options *options)
bf8dd3f5
POL
939{
940 int error;
941
19c80a6f 942 if ((error = git_stash_apply(repo, index, options)) < 0)
bf8dd3f5
POL
943 return error;
944
945 return git_stash_drop(repo, index);
946}