2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "repository.h"
16 #include "git2/diff.h"
17 #include "git2/stash.h"
18 #include "git2/status.h"
19 #include "git2/checkout.h"
20 #include "git2/index.h"
21 #include "git2/transaction.h"
22 #include "git2/merge.h"
24 #include "signature.h"
28 #include "diff_generate.h"
30 static int create_error(int error
, const char *msg
)
32 git_error_set(GIT_ERROR_STASH
, "cannot stash changes - %s", msg
);
36 static int retrieve_head(git_reference
**out
, git_repository
*repo
)
38 int error
= git_repository_head(out
, repo
);
40 if (error
== GIT_EUNBORNBRANCH
)
41 return create_error(error
, "you do not have the initial commit yet.");
46 static int append_abbreviated_oid(git_buf
*out
, const git_oid
*b_commit
)
50 formatted_oid
= git_oid_allocfmt(b_commit
);
51 GIT_ERROR_CHECK_ALLOC(formatted_oid
);
53 git_buf_put(out
, formatted_oid
, 7);
54 git__free(formatted_oid
);
56 return git_buf_oom(out
) ? -1 : 0;
59 static int append_commit_description(git_buf
*out
, git_commit
* commit
)
61 const char *summary
= git_commit_summary(commit
);
62 GIT_ERROR_CHECK_ALLOC(summary
);
64 if (append_abbreviated_oid(out
, git_commit_id(commit
)) < 0)
67 git_buf_putc(out
, ' ');
68 git_buf_puts(out
, summary
);
69 git_buf_putc(out
, '\n');
71 return git_buf_oom(out
) ? -1 : 0;
74 static int retrieve_base_commit_and_message(
75 git_commit
**b_commit
,
76 git_buf
*stash_message
,
79 git_reference
*head
= NULL
;
82 if ((error
= retrieve_head(&head
, repo
)) < 0)
85 if (strcmp("HEAD", git_reference_name(head
)) == 0)
86 error
= git_buf_puts(stash_message
, "(no branch): ");
88 error
= git_buf_printf(
91 git_reference_name(head
) + strlen(GIT_REFS_HEADS_DIR
));
95 if ((error
= git_commit_lookup(
96 b_commit
, repo
, git_reference_target(head
))) < 0)
99 if ((error
= append_commit_description(stash_message
, *b_commit
)) < 0)
103 git_reference_free(head
);
107 static int build_tree_from_index(
109 git_repository
*repo
,
115 if ((error
= git_index_write_tree_to(&i_tree_oid
, index
, repo
)) < 0)
118 return git_tree_lookup(out
, repo
, &i_tree_oid
);
121 static int commit_index(
122 git_commit
**i_commit
,
123 git_repository
*repo
,
125 const git_signature
*stasher
,
127 const git_commit
*parent
)
129 git_tree
*i_tree
= NULL
;
130 git_oid i_commit_oid
;
131 git_buf msg
= GIT_BUF_INIT
;
134 if ((error
= build_tree_from_index(&i_tree
, repo
, index
)) < 0)
137 if ((error
= git_buf_printf(&msg
, "index on %s\n", message
)) < 0)
140 if ((error
= git_commit_create(
142 git_index_owner(index
),
153 error
= git_commit_lookup(i_commit
, git_index_owner(index
), &i_commit_oid
);
156 git_tree_free(i_tree
);
157 git_buf_dispose(&msg
);
161 struct stash_update_rules
{
162 bool include_changed
;
163 bool include_untracked
;
164 bool include_ignored
;
168 * Similar to git_index_add_bypath but able to operate on any
169 * index without making assumptions about the repository's index
171 static int stash_to_index(
172 git_repository
*repo
,
176 git_index
*repo_index
;
177 git_index_entry entry
= {{0}};
181 if (!git_repository_is_bare(repo
) &&
182 (error
= git_repository_index__weakptr(&repo_index
, repo
)) < 0)
185 if ((error
= git_blob__create_from_paths(
186 &entry
.id
, &st
, repo
, NULL
, path
, 0, true)) < 0)
189 git_index_entry__init_from_stat(&entry
, &st
,
190 (repo_index
!= NULL
|| !repo_index
->distrust_filemode
));
194 return git_index_add(index
, &entry
);
197 static int stash_update_index_from_diff(
198 git_repository
*repo
,
200 const git_diff
*diff
,
201 struct stash_update_rules
*data
)
204 size_t d
, max_d
= git_diff_num_deltas(diff
);
206 for (d
= 0; !error
&& d
< max_d
; ++d
) {
207 const char *add_path
= NULL
;
208 const git_diff_delta
*delta
= git_diff_get_delta(diff
, d
);
210 switch (delta
->status
) {
211 case GIT_DELTA_IGNORED
:
212 if (data
->include_ignored
)
213 add_path
= delta
->new_file
.path
;
216 case GIT_DELTA_UNTRACKED
:
217 if (data
->include_untracked
&&
218 delta
->new_file
.mode
!= GIT_FILEMODE_TREE
)
219 add_path
= delta
->new_file
.path
;
222 case GIT_DELTA_ADDED
:
223 case GIT_DELTA_MODIFIED
:
224 if (data
->include_changed
)
225 add_path
= delta
->new_file
.path
;
228 case GIT_DELTA_DELETED
:
229 if (data
->include_changed
&&
230 !git_index_find(NULL
, index
, delta
->old_file
.path
))
231 error
= git_index_remove(index
, delta
->old_file
.path
, 0);
238 "cannot update index. Unimplemented status (%d)",
243 if (add_path
!= NULL
)
244 error
= stash_to_index(repo
, index
, add_path
);
250 static int build_untracked_tree(
252 git_repository
*repo
,
253 git_commit
*i_commit
,
256 git_index
*i_index
= NULL
;
257 git_tree
*i_tree
= NULL
;
258 git_diff
*diff
= NULL
;
259 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
260 struct stash_update_rules data
= {0};
263 if ((error
= git_index_new(&i_index
)) < 0)
266 if (flags
& GIT_STASH_INCLUDE_UNTRACKED
) {
267 opts
.flags
|= GIT_DIFF_INCLUDE_UNTRACKED
|
268 GIT_DIFF_RECURSE_UNTRACKED_DIRS
;
269 data
.include_untracked
= true;
272 if (flags
& GIT_STASH_INCLUDE_IGNORED
) {
273 opts
.flags
|= GIT_DIFF_INCLUDE_IGNORED
|
274 GIT_DIFF_RECURSE_IGNORED_DIRS
;
275 data
.include_ignored
= true;
278 if ((error
= git_commit_tree(&i_tree
, i_commit
)) < 0)
281 if ((error
= git_diff_tree_to_workdir(&diff
, repo
, i_tree
, &opts
)) < 0)
284 if ((error
= stash_update_index_from_diff(repo
, i_index
, diff
, &data
)) < 0)
287 error
= build_tree_from_index(tree_out
, repo
, i_index
);
291 git_tree_free(i_tree
);
292 git_index_free(i_index
);
296 static int commit_untracked(
297 git_commit
**u_commit
,
298 git_repository
*repo
,
299 const git_signature
*stasher
,
301 git_commit
*i_commit
,
304 git_tree
*u_tree
= NULL
;
305 git_oid u_commit_oid
;
306 git_buf msg
= GIT_BUF_INIT
;
309 if ((error
= build_untracked_tree(&u_tree
, repo
, i_commit
, flags
)) < 0)
312 if ((error
= git_buf_printf(&msg
, "untracked files on %s\n", message
)) < 0)
315 if ((error
= git_commit_create(
328 error
= git_commit_lookup(u_commit
, repo
, &u_commit_oid
);
331 git_tree_free(u_tree
);
332 git_buf_dispose(&msg
);
336 static git_diff_delta
*stash_delta_merge(
337 const git_diff_delta
*a
,
338 const git_diff_delta
*b
,
341 /* Special case for stash: if a file is deleted in the index, but exists
342 * in the working tree, we need to stash the workdir copy for the workdir.
344 if (a
->status
== GIT_DELTA_DELETED
&& b
->status
== GIT_DELTA_UNTRACKED
) {
345 git_diff_delta
*dup
= git_diff__delta_dup(b
, pool
);
348 dup
->status
= GIT_DELTA_MODIFIED
;
352 return git_diff__merge_like_cgit(a
, b
, pool
);
355 static int build_workdir_tree(
357 git_repository
*repo
,
359 git_commit
*b_commit
)
361 git_tree
*b_tree
= NULL
;
362 git_diff
*diff
= NULL
, *idx_to_wd
= NULL
;
363 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
364 struct stash_update_rules data
= {0};
367 opts
.flags
= GIT_DIFF_IGNORE_SUBMODULES
| GIT_DIFF_INCLUDE_UNTRACKED
;
369 if ((error
= git_commit_tree(&b_tree
, b_commit
)) < 0)
372 if ((error
= git_diff_tree_to_index(&diff
, repo
, b_tree
, i_index
, &opts
)) < 0 ||
373 (error
= git_diff_index_to_workdir(&idx_to_wd
, repo
, i_index
, &opts
)) < 0 ||
374 (error
= git_diff__merge(diff
, idx_to_wd
, stash_delta_merge
)) < 0)
377 data
.include_changed
= true;
379 if ((error
= stash_update_index_from_diff(repo
, i_index
, diff
, &data
)) < 0)
382 error
= build_tree_from_index(tree_out
, repo
, i_index
);
385 git_diff_free(idx_to_wd
);
387 git_tree_free(b_tree
);
392 static int commit_worktree(
393 git_oid
*w_commit_oid
,
394 git_repository
*repo
,
395 const git_signature
*stasher
,
397 git_commit
*i_commit
,
398 git_commit
*b_commit
,
399 git_commit
*u_commit
)
401 const git_commit
*parents
[] = { NULL
, NULL
, NULL
};
402 git_index
*i_index
= NULL
, *r_index
= NULL
;
403 git_tree
*w_tree
= NULL
;
404 int error
= 0, ignorecase
;
406 parents
[0] = b_commit
;
407 parents
[1] = i_commit
;
408 parents
[2] = u_commit
;
410 if ((error
= git_repository_index(&r_index
, repo
) < 0) ||
411 (error
= git_index_new(&i_index
)) < 0 ||
412 (error
= git_index__fill(i_index
, &r_index
->entries
) < 0) ||
413 (error
= git_repository__configmap_lookup(&ignorecase
, repo
, GIT_CONFIGMAP_IGNORECASE
)) < 0)
416 git_index__set_ignore_case(i_index
, ignorecase
);
418 if ((error
= build_workdir_tree(&w_tree
, repo
, i_index
, b_commit
)) < 0)
421 error
= git_commit_create(
434 git_tree_free(w_tree
);
435 git_index_free(i_index
);
436 git_index_free(r_index
);
440 static int prepare_worktree_commit_message(git_buf
*out
, const char *user_message
)
442 git_buf buf
= GIT_BUF_INIT
;
446 git_buf_printf(&buf
, "WIP on %s", git_buf_cstr(out
));
450 if ((colon
= strchr(git_buf_cstr(out
), ':')) == NULL
)
453 git_buf_puts(&buf
, "On ");
454 git_buf_put(&buf
, git_buf_cstr(out
), colon
- out
->ptr
);
455 git_buf_printf(&buf
, ": %s\n", user_message
);
458 if (git_buf_oom(&buf
)) {
463 git_buf_swap(out
, &buf
);
466 git_buf_dispose(&buf
);
470 static int update_reflog(
471 git_oid
*w_commit_oid
,
472 git_repository
*repo
,
475 git_reference
*stash
;
478 if ((error
= git_reference_ensure_log(repo
, GIT_REFS_STASH_FILE
)) < 0)
481 error
= git_reference_create(&stash
, repo
, GIT_REFS_STASH_FILE
, w_commit_oid
, 1, message
);
483 git_reference_free(stash
);
488 static int is_dirty_cb(const char *path
, unsigned int status
, void *payload
)
494 return GIT_PASSTHROUGH
;
497 static int ensure_there_are_changes_to_stash(git_repository
*repo
, uint32_t flags
)
500 git_status_options opts
= GIT_STATUS_OPTIONS_INIT
;
502 opts
.show
= GIT_STATUS_SHOW_INDEX_AND_WORKDIR
;
503 opts
.flags
= GIT_STATUS_OPT_EXCLUDE_SUBMODULES
;
505 if (flags
& GIT_STASH_INCLUDE_UNTRACKED
)
506 opts
.flags
|= GIT_STATUS_OPT_INCLUDE_UNTRACKED
|
507 GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS
;
509 if (flags
& GIT_STASH_INCLUDE_IGNORED
)
510 opts
.flags
|= GIT_STATUS_OPT_INCLUDE_IGNORED
|
511 GIT_STATUS_OPT_RECURSE_IGNORED_DIRS
;
513 error
= git_status_foreach_ext(repo
, &opts
, is_dirty_cb
, NULL
);
515 if (error
== GIT_PASSTHROUGH
)
519 return create_error(GIT_ENOTFOUND
, "there is nothing to stash.");
524 static int reset_index_and_workdir(git_repository
*repo
, git_commit
*commit
, uint32_t flags
)
526 git_checkout_options opts
= GIT_CHECKOUT_OPTIONS_INIT
;
528 opts
.checkout_strategy
= GIT_CHECKOUT_FORCE
;
529 if (flags
& GIT_STASH_INCLUDE_UNTRACKED
)
530 opts
.checkout_strategy
|= GIT_CHECKOUT_REMOVE_UNTRACKED
;
531 if (flags
& GIT_STASH_INCLUDE_IGNORED
)
532 opts
.checkout_strategy
|= GIT_CHECKOUT_REMOVE_IGNORED
;
534 return git_checkout_tree(repo
, (git_object
*)commit
, &opts
);
539 git_repository
*repo
,
540 const git_signature
*stasher
,
544 git_index
*index
= NULL
;
545 git_commit
*b_commit
= NULL
, *i_commit
= NULL
, *u_commit
= NULL
;
546 git_buf msg
= GIT_BUF_INIT
;
549 assert(out
&& repo
&& stasher
);
551 if ((error
= git_repository__ensure_not_bare(repo
, "stash save")) < 0)
554 if ((error
= retrieve_base_commit_and_message(&b_commit
, &msg
, repo
)) < 0)
557 if ((error
= ensure_there_are_changes_to_stash(repo
, flags
)) < 0)
560 if ((error
= git_repository_index(&index
, repo
)) < 0)
563 if ((error
= commit_index(&i_commit
, repo
, index
, stasher
,
564 git_buf_cstr(&msg
), b_commit
)) < 0)
567 if ((flags
& (GIT_STASH_INCLUDE_UNTRACKED
| GIT_STASH_INCLUDE_IGNORED
)) &&
568 (error
= commit_untracked(&u_commit
, repo
, stasher
,
569 git_buf_cstr(&msg
), i_commit
, flags
)) < 0)
572 if ((error
= prepare_worktree_commit_message(&msg
, message
)) < 0)
575 if ((error
= commit_worktree(out
, repo
, stasher
, git_buf_cstr(&msg
),
576 i_commit
, b_commit
, u_commit
)) < 0)
581 if ((error
= update_reflog(out
, repo
, git_buf_cstr(&msg
))) < 0)
584 if ((error
= reset_index_and_workdir(repo
, (flags
& GIT_STASH_KEEP_INDEX
) ? i_commit
: b_commit
,
590 git_buf_dispose(&msg
);
591 git_commit_free(i_commit
);
592 git_commit_free(b_commit
);
593 git_commit_free(u_commit
);
594 git_index_free(index
);
599 static int retrieve_stash_commit(
601 git_repository
*repo
,
604 git_reference
*stash
= NULL
;
605 git_reflog
*reflog
= NULL
;
608 const git_reflog_entry
*entry
;
610 if ((error
= git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
)) < 0)
613 if ((error
= git_reflog_read(&reflog
, repo
, GIT_REFS_STASH_FILE
)) < 0)
616 max
= git_reflog_entrycount(reflog
);
617 if (!max
|| index
> max
- 1) {
618 error
= GIT_ENOTFOUND
;
619 git_error_set(GIT_ERROR_STASH
, "no stashed state at position %" PRIuZ
, index
);
623 entry
= git_reflog_entry_byindex(reflog
, index
);
624 if ((error
= git_commit_lookup(commit
, repo
, git_reflog_entry_id_new(entry
))) < 0)
628 git_reference_free(stash
);
629 git_reflog_free(reflog
);
633 static int retrieve_stash_trees(
634 git_tree
**out_stash_tree
,
635 git_tree
**out_base_tree
,
636 git_tree
**out_index_tree
,
637 git_tree
**out_index_parent_tree
,
638 git_tree
**out_untracked_tree
,
639 git_commit
*stash_commit
)
641 git_tree
*stash_tree
= NULL
;
642 git_commit
*base_commit
= NULL
;
643 git_tree
*base_tree
= NULL
;
644 git_commit
*index_commit
= NULL
;
645 git_tree
*index_tree
= NULL
;
646 git_commit
*index_parent_commit
= NULL
;
647 git_tree
*index_parent_tree
= NULL
;
648 git_commit
*untracked_commit
= NULL
;
649 git_tree
*untracked_tree
= NULL
;
652 if ((error
= git_commit_tree(&stash_tree
, stash_commit
)) < 0)
655 if ((error
= git_commit_parent(&base_commit
, stash_commit
, 0)) < 0)
657 if ((error
= git_commit_tree(&base_tree
, base_commit
)) < 0)
660 if ((error
= git_commit_parent(&index_commit
, stash_commit
, 1)) < 0)
662 if ((error
= git_commit_tree(&index_tree
, index_commit
)) < 0)
665 if ((error
= git_commit_parent(&index_parent_commit
, index_commit
, 0)) < 0)
667 if ((error
= git_commit_tree(&index_parent_tree
, index_parent_commit
)) < 0)
670 if (git_commit_parentcount(stash_commit
) == 3) {
671 if ((error
= git_commit_parent(&untracked_commit
, stash_commit
, 2)) < 0)
673 if ((error
= git_commit_tree(&untracked_tree
, untracked_commit
)) < 0)
677 *out_stash_tree
= stash_tree
;
678 *out_base_tree
= base_tree
;
679 *out_index_tree
= index_tree
;
680 *out_index_parent_tree
= index_parent_tree
;
681 *out_untracked_tree
= untracked_tree
;
684 git_commit_free(untracked_commit
);
685 git_commit_free(index_parent_commit
);
686 git_commit_free(index_commit
);
687 git_commit_free(base_commit
);
689 git_tree_free(stash_tree
);
690 git_tree_free(base_tree
);
691 git_tree_free(index_tree
);
692 git_tree_free(index_parent_tree
);
693 git_tree_free(untracked_tree
);
698 static int merge_indexes(
700 git_repository
*repo
,
701 git_tree
*ancestor_tree
,
702 git_index
*ours_index
,
703 git_index
*theirs_index
)
705 git_iterator
*ancestor
= NULL
, *ours
= NULL
, *theirs
= NULL
;
706 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
709 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
711 if ((error
= git_iterator_for_tree(&ancestor
, ancestor_tree
, &iter_opts
)) < 0 ||
712 (error
= git_iterator_for_index(&ours
, repo
, ours_index
, &iter_opts
)) < 0 ||
713 (error
= git_iterator_for_index(&theirs
, repo
, theirs_index
, &iter_opts
)) < 0)
716 error
= git_merge__iterators(out
, repo
, ancestor
, ours
, theirs
, NULL
);
719 git_iterator_free(ancestor
);
720 git_iterator_free(ours
);
721 git_iterator_free(theirs
);
725 static int merge_index_and_tree(
727 git_repository
*repo
,
728 git_tree
*ancestor_tree
,
729 git_index
*ours_index
,
730 git_tree
*theirs_tree
)
732 git_iterator
*ancestor
= NULL
, *ours
= NULL
, *theirs
= NULL
;
733 git_iterator_options iter_opts
= GIT_ITERATOR_OPTIONS_INIT
;
736 iter_opts
.flags
= GIT_ITERATOR_DONT_IGNORE_CASE
;
738 if ((error
= git_iterator_for_tree(&ancestor
, ancestor_tree
, &iter_opts
)) < 0 ||
739 (error
= git_iterator_for_index(&ours
, repo
, ours_index
, &iter_opts
)) < 0 ||
740 (error
= git_iterator_for_tree(&theirs
, theirs_tree
, &iter_opts
)) < 0)
743 error
= git_merge__iterators(out
, repo
, ancestor
, ours
, theirs
, NULL
);
746 git_iterator_free(ancestor
);
747 git_iterator_free(ours
);
748 git_iterator_free(theirs
);
752 static void normalize_apply_options(
753 git_stash_apply_options
*opts
,
754 const git_stash_apply_options
*given_apply_opts
)
756 if (given_apply_opts
!= NULL
) {
757 memcpy(opts
, given_apply_opts
, sizeof(git_stash_apply_options
));
759 git_stash_apply_options default_apply_opts
= GIT_STASH_APPLY_OPTIONS_INIT
;
760 memcpy(opts
, &default_apply_opts
, sizeof(git_stash_apply_options
));
763 opts
->checkout_options
.checkout_strategy
|= GIT_CHECKOUT_NO_REFRESH
;
765 if (!opts
->checkout_options
.our_label
)
766 opts
->checkout_options
.our_label
= "Updated upstream";
768 if (!opts
->checkout_options
.their_label
)
769 opts
->checkout_options
.their_label
= "Stashed changes";
772 int git_stash_apply_options_init(git_stash_apply_options
*opts
, unsigned int version
)
774 GIT_INIT_STRUCTURE_FROM_TEMPLATE(
775 opts
, version
, git_stash_apply_options
, GIT_STASH_APPLY_OPTIONS_INIT
);
779 int git_stash_apply_init_options(git_stash_apply_options
*opts
, unsigned int version
)
781 return git_stash_apply_options_init(opts
, version
);
784 #define NOTIFY_PROGRESS(opts, progress_type) \
786 if ((opts).progress_cb && \
787 (error = (opts).progress_cb((progress_type), (opts).progress_payload))) { \
788 error = (error < 0) ? error : -1; \
793 static int ensure_clean_index(git_repository
*repo
, git_index
*index
)
795 git_tree
*head_tree
= NULL
;
796 git_diff
*index_diff
= NULL
;
799 if ((error
= git_repository_head_tree(&head_tree
, repo
)) < 0 ||
800 (error
= git_diff_tree_to_index(
801 &index_diff
, repo
, head_tree
, index
, NULL
)) < 0)
804 if (git_diff_num_deltas(index_diff
) > 0) {
805 git_error_set(GIT_ERROR_STASH
, "%" PRIuZ
" uncommitted changes exist in the index",
806 git_diff_num_deltas(index_diff
));
807 error
= GIT_EUNCOMMITTED
;
811 git_diff_free(index_diff
);
812 git_tree_free(head_tree
);
816 static int stage_new_file(const git_index_entry
**entries
, void *data
)
818 git_index
*index
= data
;
820 if(entries
[0] == NULL
)
821 return git_index_add(index
, entries
[1]);
823 return git_index_add(index
, entries
[0]);
826 static int stage_new_files(
828 git_tree
*parent_tree
,
831 git_iterator
*iterators
[2] = { NULL
, NULL
};
832 git_iterator_options iterator_options
= GIT_ITERATOR_OPTIONS_INIT
;
833 git_index
*index
= NULL
;
836 if ((error
= git_index_new(&index
)) < 0 ||
837 (error
= git_iterator_for_tree(
838 &iterators
[0], parent_tree
, &iterator_options
)) < 0 ||
839 (error
= git_iterator_for_tree(
840 &iterators
[1], tree
, &iterator_options
)) < 0)
843 error
= git_iterator_walk(iterators
, 2, stage_new_file
, index
);
847 git_index_free(index
);
851 git_iterator_free(iterators
[0]);
852 git_iterator_free(iterators
[1]);
858 git_repository
*repo
,
860 const git_stash_apply_options
*given_opts
)
862 git_stash_apply_options opts
;
863 unsigned int checkout_strategy
;
864 git_commit
*stash_commit
= NULL
;
865 git_tree
*stash_tree
= NULL
;
866 git_tree
*stash_parent_tree
= NULL
;
867 git_tree
*index_tree
= NULL
;
868 git_tree
*index_parent_tree
= NULL
;
869 git_tree
*untracked_tree
= NULL
;
870 git_index
*stash_adds
= NULL
;
871 git_index
*repo_index
= NULL
;
872 git_index
*unstashed_index
= NULL
;
873 git_index
*modified_index
= NULL
;
874 git_index
*untracked_index
= NULL
;
877 GIT_ERROR_CHECK_VERSION(given_opts
, GIT_STASH_APPLY_OPTIONS_VERSION
, "git_stash_apply_options");
879 normalize_apply_options(&opts
, given_opts
);
880 checkout_strategy
= opts
.checkout_options
.checkout_strategy
;
882 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_LOADING_STASH
);
884 /* Retrieve commit corresponding to the given stash */
885 if ((error
= retrieve_stash_commit(&stash_commit
, repo
, index
)) < 0)
888 /* Retrieve all trees in the stash */
889 if ((error
= retrieve_stash_trees(
890 &stash_tree
, &stash_parent_tree
, &index_tree
,
891 &index_parent_tree
, &untracked_tree
, stash_commit
)) < 0)
894 /* Load repo index */
895 if ((error
= git_repository_index(&repo_index
, repo
)) < 0)
898 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX
);
900 if ((error
= ensure_clean_index(repo
, repo_index
)) < 0)
903 /* Restore index if required */
904 if ((opts
.flags
& GIT_STASH_APPLY_REINSTATE_INDEX
) &&
905 git_oid_cmp(git_tree_id(stash_parent_tree
), git_tree_id(index_tree
))) {
907 if ((error
= merge_index_and_tree(
908 &unstashed_index
, repo
, index_parent_tree
, repo_index
, index_tree
)) < 0)
911 if (git_index_has_conflicts(unstashed_index
)) {
912 error
= GIT_ECONFLICT
;
916 /* Otherwise, stage any new files in the stash tree. (Note: their
917 * previously unstaged contents are staged, not the previously staged.)
919 } else if ((opts
.flags
& GIT_STASH_APPLY_REINSTATE_INDEX
) == 0) {
920 if ((error
= stage_new_files(
921 &stash_adds
, stash_parent_tree
, stash_tree
)) < 0 ||
922 (error
= merge_indexes(
923 &unstashed_index
, repo
, stash_parent_tree
, repo_index
, stash_adds
)) < 0)
927 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED
);
929 /* Restore modified files in workdir */
930 if ((error
= merge_index_and_tree(
931 &modified_index
, repo
, stash_parent_tree
, repo_index
, stash_tree
)) < 0)
934 /* If applicable, restore untracked / ignored files in workdir */
935 if (untracked_tree
) {
936 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED
);
938 if ((error
= merge_index_and_tree(&untracked_index
, repo
, NULL
, repo_index
, untracked_tree
)) < 0)
942 if (untracked_index
) {
943 opts
.checkout_options
.checkout_strategy
|= GIT_CHECKOUT_DONT_UPDATE_INDEX
;
945 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED
);
947 if ((error
= git_checkout_index(repo
, untracked_index
, &opts
.checkout_options
)) < 0)
950 opts
.checkout_options
.checkout_strategy
= checkout_strategy
;
954 /* If there are conflicts in the modified index, then we need to actually
955 * check that out as the repo's index. Otherwise, we don't update the
959 if (!git_index_has_conflicts(modified_index
))
960 opts
.checkout_options
.checkout_strategy
|= GIT_CHECKOUT_DONT_UPDATE_INDEX
;
962 /* Check out the modified index using the existing repo index as baseline,
963 * so that existing modifications in the index can be rewritten even when
964 * checking out safely.
966 opts
.checkout_options
.baseline_index
= repo_index
;
968 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED
);
970 if ((error
= git_checkout_index(repo
, modified_index
, &opts
.checkout_options
)) < 0)
973 if (unstashed_index
&& !git_index_has_conflicts(modified_index
)) {
974 if ((error
= git_index_read_index(repo_index
, unstashed_index
)) < 0)
978 NOTIFY_PROGRESS(opts
, GIT_STASH_APPLY_PROGRESS_DONE
);
980 error
= git_index_write(repo_index
);
983 git_index_free(untracked_index
);
984 git_index_free(modified_index
);
985 git_index_free(unstashed_index
);
986 git_index_free(stash_adds
);
987 git_index_free(repo_index
);
988 git_tree_free(untracked_tree
);
989 git_tree_free(index_parent_tree
);
990 git_tree_free(index_tree
);
991 git_tree_free(stash_parent_tree
);
992 git_tree_free(stash_tree
);
993 git_commit_free(stash_commit
);
997 int git_stash_foreach(
998 git_repository
*repo
,
999 git_stash_cb callback
,
1002 git_reference
*stash
;
1003 git_reflog
*reflog
= NULL
;
1006 const git_reflog_entry
*entry
;
1008 error
= git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
);
1009 if (error
== GIT_ENOTFOUND
) {
1016 if ((error
= git_reflog_read(&reflog
, repo
, GIT_REFS_STASH_FILE
)) < 0)
1019 max
= git_reflog_entrycount(reflog
);
1020 for (i
= 0; i
< max
; i
++) {
1021 entry
= git_reflog_entry_byindex(reflog
, i
);
1024 git_reflog_entry_message(entry
),
1025 git_reflog_entry_id_new(entry
),
1029 git_error_set_after_callback(error
);
1035 git_reference_free(stash
);
1036 git_reflog_free(reflog
);
1041 git_repository
*repo
,
1044 git_transaction
*tx
;
1045 git_reference
*stash
= NULL
;
1046 git_reflog
*reflog
= NULL
;
1050 if ((error
= git_transaction_new(&tx
, repo
)) < 0)
1053 if ((error
= git_transaction_lock_ref(tx
, GIT_REFS_STASH_FILE
)) < 0)
1056 if ((error
= git_reference_lookup(&stash
, repo
, GIT_REFS_STASH_FILE
)) < 0)
1059 if ((error
= git_reflog_read(&reflog
, repo
, GIT_REFS_STASH_FILE
)) < 0)
1062 max
= git_reflog_entrycount(reflog
);
1064 if (!max
|| index
> max
- 1) {
1065 error
= GIT_ENOTFOUND
;
1066 git_error_set(GIT_ERROR_STASH
, "no stashed state at position %" PRIuZ
, index
);
1070 if ((error
= git_reflog_drop(reflog
, index
, true)) < 0)
1073 if ((error
= git_transaction_set_reflog(tx
, GIT_REFS_STASH_FILE
, reflog
)) < 0)
1077 if ((error
= git_transaction_remove(tx
, GIT_REFS_STASH_FILE
)) < 0)
1079 } else if (index
== 0) {
1080 const git_reflog_entry
*entry
;
1082 entry
= git_reflog_entry_byindex(reflog
, 0);
1083 if ((error
= git_transaction_set_target(tx
, GIT_REFS_STASH_FILE
, &entry
->oid_cur
, NULL
, NULL
)) < 0)
1087 error
= git_transaction_commit(tx
);
1090 git_reference_free(stash
);
1091 git_transaction_free(tx
);
1092 git_reflog_free(reflog
);
1097 git_repository
*repo
,
1099 const git_stash_apply_options
*options
)
1103 if ((error
= git_stash_apply(repo
, index
, options
)) < 0)
1106 return git_stash_drop(repo
, index
);