summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2015-05-12 10:29:58 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2015-05-12 10:29:58 +0200
commit77a15fe8e12c2ac3aa1ab93d8fd86b6d3e06ce35 (patch)
tree0c41536f61ef7d421008982c7475ef1937c23d7c /src
parent9cdd657887d5fc7cf5a5819e60900d67513ca1ff (diff)
parent1f1f5c639e7e7df3f0c65dbdbcc6b884041a8647 (diff)
downloadlibgit2-77a15fe8e12c2ac3aa1ab93d8fd86b6d3e06ce35.tar.gz
Merge pull request #3018 from ethomson/stash_apply
Stash apply
Diffstat (limited to 'src')
-rw-r--r--src/checkout.c19
-rw-r--r--src/index.c98
-rw-r--r--src/index.h2
-rw-r--r--src/merge.c86
-rw-r--r--src/merge.h18
-rw-r--r--src/stash.c317
6 files changed, 497 insertions, 43 deletions
diff --git a/src/checkout.c b/src/checkout.c
index a647ce0b9..6a1d28136 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -2397,7 +2397,7 @@ static int checkout_data_init(
&data->can_symlink, repo, GIT_CVAR_SYMLINKS)) < 0)
goto cleanup;
- if (!data->opts.baseline) {
+ if (!data->opts.baseline && !data->opts.baseline_index) {
data->opts_free_baseline = true;
error = checkout_lookup_head_tree(&data->opts.baseline, repo);
@@ -2501,12 +2501,21 @@ int git_checkout_iterator(
(error = git_iterator_for_workdir_ext(
&workdir, data.repo, data.opts.target_directory, index, NULL,
iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
- data.pfx, data.pfx)) < 0 ||
- (error = git_iterator_for_tree(
- &baseline, data.opts.baseline,
- iterflags, data.pfx, data.pfx)) < 0)
+ data.pfx, data.pfx)) < 0)
goto cleanup;
+ if (data.opts.baseline_index) {
+ if ((error = git_iterator_for_index(
+ &baseline, data.opts.baseline_index,
+ iterflags, data.pfx, data.pfx)) < 0)
+ goto cleanup;
+ } else {
+ if ((error = git_iterator_for_tree(
+ &baseline, data.opts.baseline,
+ iterflags, data.pfx, data.pfx)) < 0)
+ goto cleanup;
+ }
+
/* Should not have case insensitivity mismatch */
assert(git_iterator_ignore_case(workdir) == git_iterator_ignore_case(baseline));
diff --git a/src/index.c b/src/index.c
index 0d2a03e72..7c0c31052 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2451,6 +2451,104 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
return error;
}
+int git_index_read_index(
+ git_index *index,
+ const git_index *new_index)
+{
+ git_vector new_entries = GIT_VECTOR_INIT,
+ remove_entries = GIT_VECTOR_INIT;
+ git_iterator *index_iterator = NULL;
+ git_iterator *new_iterator = NULL;
+ const git_index_entry *old_entry, *new_entry;
+ git_index_entry *entry;
+ size_t i;
+ int error;
+
+ if ((error = git_vector_init(&new_entries, new_index->entries.length, index->entries._cmp)) < 0 ||
+ (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0)
+ goto done;
+
+ if ((error = git_iterator_for_index(&index_iterator,
+ index, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_index(&new_iterator,
+ (git_index *)new_index, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0)
+ goto done;
+
+ if (((error = git_iterator_current(&old_entry, index_iterator)) < 0 &&
+ error != GIT_ITEROVER) ||
+ ((error = git_iterator_current(&new_entry, new_iterator)) < 0 &&
+ error != GIT_ITEROVER))
+ goto done;
+
+ while (true) {
+ int diff;
+
+ if (old_entry && new_entry)
+ diff = git_index_entry_cmp(old_entry, new_entry);
+ else if (!old_entry && new_entry)
+ diff = 1;
+ else if (old_entry && !new_entry)
+ diff = -1;
+ else
+ break;
+
+ if (diff < 0) {
+ git_vector_insert(&remove_entries, (git_index_entry *)old_entry);
+ } else if (diff > 0) {
+ if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+ goto done;
+
+ git_vector_insert(&new_entries, entry);
+ } else {
+ /* Path and stage are equal, if the OID is equal, keep it to
+ * keep the stat cache data.
+ */
+ if (git_oid_equal(&old_entry->id, &new_entry->id)) {
+ git_vector_insert(&new_entries, (git_index_entry *)old_entry);
+ } else {
+ if ((error = index_entry_dup(&entry, git_index_owner(index), new_entry)) < 0)
+ goto done;
+
+ git_vector_insert(&new_entries, entry);
+ git_vector_insert(&remove_entries, (git_index_entry *)old_entry);
+ }
+ }
+
+ if (diff <= 0) {
+ if ((error = git_iterator_advance(&old_entry, index_iterator)) < 0 &&
+ error != GIT_ITEROVER)
+ goto done;
+ }
+
+ if (diff >= 0) {
+ if ((error = git_iterator_advance(&new_entry, new_iterator)) < 0 &&
+ error != GIT_ITEROVER)
+ goto done;
+ }
+ }
+
+ git_index_name_clear(index);
+ git_index_reuc_clear(index);
+
+ git_vector_swap(&new_entries, &index->entries);
+
+ git_vector_foreach(&remove_entries, i, entry) {
+ if (index->tree)
+ git_tree_cache_invalidate_path(index->tree, entry->path);
+
+ index_entry_free(entry);
+ }
+
+ error = 0;
+
+done:
+ git_vector_free(&new_entries);
+ git_vector_free(&remove_entries);
+ git_iterator_free(index_iterator);
+ git_iterator_free(new_iterator);
+ return error;
+}
+
git_repository *git_index_owner(const git_index *index)
{
return INDEX_OWNER(index);
diff --git a/src/index.h b/src/index.h
index 6d2904fdc..0f6f4e86e 100644
--- a/src/index.h
+++ b/src/index.h
@@ -93,6 +93,8 @@ extern int git_index_snapshot_find(
size_t *at_pos, git_vector *snap, git_vector_cmp entry_srch,
const char *path, size_t path_len, int stage);
+/* Replace an index with a new index */
+int git_index_read_index(git_index *index, const git_index *new_index);
typedef struct {
git_index *index;
diff --git a/src/merge.c b/src/merge.c
index bd676aacf..5e7727429 100644
--- a/src/merge.c
+++ b/src/merge.c
@@ -1451,11 +1451,11 @@ static int merge_diff_list_insert_unmodified(
int git_merge_diff_list__find_differences(
git_merge_diff_list *diff_list,
- const git_tree *ancestor_tree,
- const git_tree *our_tree,
- const git_tree *their_tree)
+ git_iterator *ancestor_iter,
+ git_iterator *our_iter,
+ git_iterator *their_iter)
{
- git_iterator *iterators[3] = {0};
+ git_iterator *iterators[3] = { ancestor_iter, our_iter, their_iter };
const git_index_entry *items[3] = {0}, *best_cur_item, *cur_items[3];
git_vector_cmp entry_compare = git_index_entry_cmp;
struct merge_diff_df_data df_data = {0};
@@ -1463,12 +1463,7 @@ int git_merge_diff_list__find_differences(
size_t i, j;
int error = 0;
- assert(diff_list && (our_tree || their_tree));
-
- if ((error = git_iterator_for_tree(&iterators[TREE_IDX_ANCESTOR], (git_tree *)ancestor_tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
- (error = git_iterator_for_tree(&iterators[TREE_IDX_OURS], (git_tree *)our_tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
- (error = git_iterator_for_tree(&iterators[TREE_IDX_THEIRS], (git_tree *)their_tree, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0)
- goto done;
+ assert(diff_list && (our_iter || their_iter));
/* Set up the iterators */
for (i = 0; i < 3; i++) {
@@ -1544,9 +1539,6 @@ int git_merge_diff_list__find_differences(
}
done:
- for (i = 0; i < 3; i++)
- git_iterator_free(iterators[i]);
-
if (error == GIT_ITEROVER)
error = 0;
@@ -1757,14 +1749,28 @@ on_error:
return error;
}
-int git_merge_trees(
+static git_iterator *iterator_given_or_empty(git_iterator **empty, git_iterator *given)
+{
+ if (given)
+ return given;
+
+ if (git_iterator_for_nothing(empty, GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL) < 0)
+ return NULL;
+
+ return *empty;
+}
+
+int git_merge__iterators(
git_index **out,
git_repository *repo,
- const git_tree *ancestor_tree,
- const git_tree *our_tree,
- const git_tree *their_tree,
+ git_iterator *ancestor_iter,
+ git_iterator *our_iter,
+ git_iterator *theirs_iter,
const git_merge_options *given_opts)
{
+ git_iterator *empty_ancestor = NULL,
+ *empty_ours = NULL,
+ *empty_theirs = NULL;
git_merge_diff_list *diff_list;
git_merge_options opts;
git_merge_diff *conflict;
@@ -1772,11 +1778,12 @@ int git_merge_trees(
size_t i;
int error = 0;
- assert(out && repo && (our_tree || their_tree));
+ assert(out && repo);
*out = NULL;
- GITERR_CHECK_VERSION(given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options");
+ GITERR_CHECK_VERSION(
+ given_opts, GIT_MERGE_OPTIONS_VERSION, "git_merge_options");
if ((error = merge_normalize_opts(repo, &opts, given_opts)) < 0)
return error;
@@ -1784,7 +1791,12 @@ int git_merge_trees(
diff_list = git_merge_diff_list__alloc(repo);
GITERR_CHECK_ALLOC(diff_list);
- if ((error = git_merge_diff_list__find_differences(diff_list, ancestor_tree, our_tree, their_tree)) < 0 ||
+ ancestor_iter = iterator_given_or_empty(&empty_ancestor, ancestor_iter);
+ our_iter = iterator_given_or_empty(&empty_ours, our_iter);
+ theirs_iter = iterator_given_or_empty(&empty_theirs, theirs_iter);
+
+ if ((error = git_merge_diff_list__find_differences(
+ diff_list, ancestor_iter, our_iter, theirs_iter)) < 0 ||
(error = git_merge_diff_list__find_renames(repo, diff_list, &opts)) < 0)
goto done;
@@ -1808,10 +1820,44 @@ int git_merge_trees(
done:
git_merge_diff_list__free(diff_list);
+ git_iterator_free(empty_ancestor);
+ git_iterator_free(empty_ours);
+ git_iterator_free(empty_theirs);
+
+ return error;
+}
+
+int git_merge_trees(
+ git_index **out,
+ git_repository *repo,
+ const git_tree *ancestor_tree,
+ const git_tree *our_tree,
+ const git_tree *their_tree,
+ const git_merge_options *merge_opts)
+{
+ git_iterator *ancestor_iter = NULL, *our_iter = NULL, *their_iter = NULL;
+ int error;
+
+ if ((error = git_iterator_for_tree(&ancestor_iter, (git_tree *)ancestor_tree,
+ GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_tree(&our_iter, (git_tree *)our_tree,
+ GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_tree(&their_iter, (git_tree *)their_tree,
+ GIT_ITERATOR_DONT_IGNORE_CASE, NULL, NULL)) < 0)
+ goto done;
+
+ error = git_merge__iterators(
+ out, repo, ancestor_iter, our_iter, their_iter, merge_opts);
+
+done:
+ git_iterator_free(ancestor_iter);
+ git_iterator_free(our_iter);
+ git_iterator_free(their_iter);
return error;
}
+
int git_merge_commits(
git_index **out,
git_repository *repo,
diff --git a/src/merge.h b/src/merge.h
index fe4505f8b..3caf617c6 100644
--- a/src/merge.h
+++ b/src/merge.h
@@ -10,6 +10,7 @@
#include "vector.h"
#include "commit_list.h"
#include "pool.h"
+#include "iterator.h"
#include "git2/merge.h"
#include "git2/types.h"
@@ -121,10 +122,11 @@ int git_merge__bases_many(
git_merge_diff_list *git_merge_diff_list__alloc(git_repository *repo);
-int git_merge_diff_list__find_differences(git_merge_diff_list *merge_diff_list,
- const git_tree *ancestor_tree,
- const git_tree *ours_tree,
- const git_tree *theirs_tree);
+int git_merge_diff_list__find_differences(
+ git_merge_diff_list *merge_diff_list,
+ git_iterator *ancestor_iterator,
+ git_iterator *ours_iter,
+ git_iterator *theirs_iter);
int git_merge_diff_list__find_renames(git_repository *repo, git_merge_diff_list *merge_diff_list, const git_merge_options *opts);
@@ -138,6 +140,14 @@ int git_merge__setup(
const git_annotated_commit *heads[],
size_t heads_len);
+int git_merge__iterators(
+ git_index **out,
+ git_repository *repo,
+ git_iterator *ancestor_iter,
+ git_iterator *our_iter,
+ git_iterator *their_iter,
+ const git_merge_options *given_opts);
+
int git_merge__check_result(git_repository *repo, git_index *index_new);
int git_merge__append_conflicts_to_merge_msg(git_repository *repo, git_index *index);
diff --git a/src/stash.c b/src/stash.c
index 8aa48cafe..c79068edf 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -8,6 +8,7 @@
#include "common.h"
#include "repository.h"
#include "commit.h"
+#include "message.h"
#include "tree.h"
#include "reflog.h"
#include "git2/diff.h"
@@ -16,7 +17,11 @@
#include "git2/checkout.h"
#include "git2/index.h"
#include "git2/transaction.h"
+#include "git2/merge.h"
+#include "index.h"
#include "signature.h"
+#include "iterator.h"
+#include "merge.h"
static int create_error(int error, const char *msg)
{
@@ -49,23 +54,14 @@ static int append_abbreviated_oid(git_buf *out, const git_oid *b_commit)
static int append_commit_description(git_buf *out, git_commit* commit)
{
- const char *message;
- size_t pos = 0, len;
+ const char *summary = git_commit_summary(commit);
+ GITERR_CHECK_ALLOC(summary);
if (append_abbreviated_oid(out, git_commit_id(commit)) < 0)
return -1;
- message = git_commit_message(commit);
- len = strlen(message);
-
- /* TODO: Replace with proper commit short message
- * when git_commit_message_short() is implemented.
- */
- while (pos < len && message[pos] != '\n')
- pos++;
-
git_buf_putc(out, ' ');
- git_buf_put(out, message, pos);
+ git_buf_puts(out, summary);
git_buf_putc(out, '\n');
return git_buf_oom(out) ? -1 : 0;
@@ -110,7 +106,7 @@ static int build_tree_from_index(git_tree **out, git_index *index)
git_oid i_tree_oid;
if ((error = git_index_write_tree(&i_tree_oid, index)) < 0)
- return -1;
+ return error;
return git_tree_lookup(out, git_index_owner(index), &i_tree_oid);
}
@@ -553,6 +549,286 @@ cleanup:
return error;
}
+static int retrieve_stash_commit(
+ git_commit **commit,
+ git_repository *repo,
+ size_t index)
+{
+ git_reference *stash = NULL;
+ git_reflog *reflog = NULL;
+ int error;
+ size_t max;
+ const git_reflog_entry *entry;
+
+ if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0)
+ goto cleanup;
+
+ if ((error = git_reflog_read(&reflog, repo, GIT_REFS_STASH_FILE)) < 0)
+ goto cleanup;
+
+ max = git_reflog_entrycount(reflog);
+ if (!max || index > max - 1) {
+ error = GIT_ENOTFOUND;
+ giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
+ goto cleanup;
+ }
+
+ entry = git_reflog_entry_byindex(reflog, index);
+ if ((error = git_commit_lookup(commit, repo, git_reflog_entry_id_new(entry))) < 0)
+ goto cleanup;
+
+cleanup:
+ git_reference_free(stash);
+ git_reflog_free(reflog);
+ return error;
+}
+
+static int retrieve_stash_trees(
+ git_tree **out_stash_tree,
+ git_tree **out_base_tree,
+ git_tree **out_index_tree,
+ git_tree **out_index_parent_tree,
+ git_tree **out_untracked_tree,
+ git_commit *stash_commit)
+{
+ git_tree *stash_tree = NULL;
+ git_commit *base_commit = NULL;
+ git_tree *base_tree = NULL;
+ git_commit *index_commit = NULL;
+ git_tree *index_tree = NULL;
+ git_commit *index_parent_commit = NULL;
+ git_tree *index_parent_tree = NULL;
+ git_commit *untracked_commit = NULL;
+ git_tree *untracked_tree = NULL;
+ int error;
+
+ if ((error = git_commit_tree(&stash_tree, stash_commit)) < 0)
+ goto cleanup;
+
+ if ((error = git_commit_parent(&base_commit, stash_commit, 0)) < 0)
+ goto cleanup;
+ if ((error = git_commit_tree(&base_tree, base_commit)) < 0)
+ goto cleanup;
+
+ if ((error = git_commit_parent(&index_commit, stash_commit, 1)) < 0)
+ goto cleanup;
+ if ((error = git_commit_tree(&index_tree, index_commit)) < 0)
+ goto cleanup;
+
+ if ((error = git_commit_parent(&index_parent_commit, index_commit, 0)) < 0)
+ goto cleanup;
+ if ((error = git_commit_tree(&index_parent_tree, index_parent_commit)) < 0)
+ goto cleanup;
+
+ if (git_commit_parentcount(stash_commit) == 3) {
+ if ((error = git_commit_parent(&untracked_commit, stash_commit, 2)) < 0)
+ goto cleanup;
+ if ((error = git_commit_tree(&untracked_tree, untracked_commit)) < 0)
+ goto cleanup;
+ }
+
+ *out_stash_tree = stash_tree;
+ *out_base_tree = base_tree;
+ *out_index_tree = index_tree;
+ *out_index_parent_tree = index_parent_tree;
+ *out_untracked_tree = untracked_tree;
+
+cleanup:
+ git_commit_free(untracked_commit);
+ git_commit_free(index_parent_commit);
+ git_commit_free(index_commit);
+ git_commit_free(base_commit);
+ if (error < 0) {
+ git_tree_free(stash_tree);
+ git_tree_free(base_tree);
+ git_tree_free(index_tree);
+ git_tree_free(index_parent_tree);
+ git_tree_free(untracked_tree);
+ }
+ return error;
+}
+
+static int merge_index_and_tree(
+ git_index **out,
+ git_repository *repo,
+ git_tree *ancestor_tree,
+ git_index *ours_index,
+ git_tree *theirs_tree)
+{
+ git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
+ const git_iterator_flag_t flags = GIT_ITERATOR_DONT_IGNORE_CASE;
+ int error;
+
+ if ((error = git_iterator_for_tree(&ancestor, ancestor_tree, flags, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_index(&ours, ours_index, flags, NULL, NULL)) < 0 ||
+ (error = git_iterator_for_tree(&theirs, theirs_tree, flags, NULL, NULL)) < 0)
+ goto done;
+
+ error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
+
+done:
+ git_iterator_free(ancestor);
+ git_iterator_free(ours);
+ git_iterator_free(theirs);
+ return error;
+}
+
+static void normalize_apply_options(
+ git_stash_apply_options *opts,
+ const git_stash_apply_options *given_apply_opts)
+{
+ if (given_apply_opts != NULL) {
+ memcpy(opts, given_apply_opts, sizeof(git_stash_apply_options));
+ } else {
+ git_stash_apply_options default_apply_opts = GIT_STASH_APPLY_OPTIONS_INIT;
+ memcpy(opts, &default_apply_opts, sizeof(git_stash_apply_options));
+ }
+
+ if ((opts->checkout_options.checkout_strategy & (GIT_CHECKOUT_SAFE | GIT_CHECKOUT_FORCE)) == 0)
+ opts->checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
+
+ if (!opts->checkout_options.our_label)
+ opts->checkout_options.our_label = "Updated upstream";
+
+ if (!opts->checkout_options.their_label)
+ opts->checkout_options.their_label = "Stashed changes";
+}
+
+int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int version)
+{
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+ opts, version, git_stash_apply_options, GIT_STASH_APPLY_OPTIONS_INIT);
+ return 0;
+}
+
+#define NOTIFY_PROGRESS(opts, progress_type) \
+ if ((opts).progress_cb && \
+ (error = (opts).progress_cb((progress_type), (opts).progress_payload))) \
+ return (error < 0) ? error : -1;
+
+int git_stash_apply(
+ git_repository *repo,
+ size_t index,
+ const git_stash_apply_options *given_opts)
+{
+ git_stash_apply_options opts;
+ unsigned int checkout_strategy;
+ git_commit *stash_commit = NULL;
+ git_tree *stash_tree = NULL;
+ git_tree *stash_parent_tree = NULL;
+ git_tree *index_tree = NULL;
+ git_tree *index_parent_tree = NULL;
+ git_tree *untracked_tree = NULL;
+ git_index *repo_index = NULL;
+ git_index *unstashed_index = NULL;
+ git_index *modified_index = NULL;
+ git_index *untracked_index = NULL;
+ int error;
+
+ GITERR_CHECK_VERSION(given_opts, GIT_STASH_APPLY_OPTIONS_VERSION, "git_stash_apply_options");
+
+ normalize_apply_options(&opts, given_opts);
+ checkout_strategy = opts.checkout_options.checkout_strategy;
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_LOADING_STASH);
+
+ /* Retrieve commit corresponding to the given stash */
+ if ((error = retrieve_stash_commit(&stash_commit, repo, index)) < 0)
+ goto cleanup;
+
+ /* Retrieve all trees in the stash */
+ if ((error = retrieve_stash_trees(
+ &stash_tree, &stash_parent_tree, &index_tree,
+ &index_parent_tree, &untracked_tree, stash_commit)) < 0)
+ goto cleanup;
+
+ /* Load repo index */
+ if ((error = git_repository_index(&repo_index, repo)) < 0)
+ goto cleanup;
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
+
+ /* Restore index if required */
+ if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) &&
+ git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
+
+ if ((error = merge_index_and_tree(
+ &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
+ goto cleanup;
+
+ if (git_index_has_conflicts(unstashed_index)) {
+ error = GIT_EMERGECONFLICT;
+ goto cleanup;
+ }
+ }
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_MODIFIED);
+
+ /* Restore modified files in workdir */
+ if ((error = merge_index_and_tree(
+ &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
+ goto cleanup;
+
+ /* If applicable, restore untracked / ignored files in workdir */
+ if (untracked_tree) {
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_UNTRACKED);
+
+ if ((error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
+ goto cleanup;
+ }
+
+ if (untracked_index) {
+ opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_UNTRACKED);
+
+ if ((error = git_checkout_index(repo, untracked_index, &opts.checkout_options)) < 0)
+ goto cleanup;
+
+ opts.checkout_options.checkout_strategy = checkout_strategy;
+ }
+
+
+ /* If there are conflicts in the modified index, then we need to actually
+ * check that out as the repo's index. Otherwise, we don't update the
+ * index.
+ */
+
+ if (!git_index_has_conflicts(modified_index))
+ opts.checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
+
+ /* Check out the modified index using the existing repo index as baseline,
+ * so that existing modifications in the index can be rewritten even when
+ * checking out safely.
+ */
+ opts.checkout_options.baseline_index = repo_index;
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_CHECKOUT_MODIFIED);
+
+ if ((error = git_checkout_index(repo, modified_index, &opts.checkout_options)) < 0)
+ goto cleanup;
+
+ if (unstashed_index && !git_index_has_conflicts(modified_index)) {
+ if ((error = git_index_read_index(repo_index, unstashed_index)) < 0)
+ goto cleanup;
+ }
+
+ NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_DONE);
+
+cleanup:
+ git_index_free(untracked_index);
+ git_index_free(modified_index);
+ git_index_free(unstashed_index);
+ git_index_free(repo_index);
+ git_tree_free(untracked_tree);
+ git_tree_free(index_parent_tree);
+ git_tree_free(index_tree);
+ git_tree_free(stash_parent_tree);
+ git_tree_free(stash_tree);
+ git_commit_free(stash_commit);
+ return error;
+}
+
int git_stash_foreach(
git_repository *repo,
git_stash_cb callback,
@@ -620,7 +896,7 @@ int git_stash_drop(
max = git_reflog_entrycount(reflog);
- if (index > max - 1) {
+ if (!max || index > max - 1) {
error = GIT_ENOTFOUND;
giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index);
goto cleanup;
@@ -651,3 +927,16 @@ cleanup:
git_reflog_free(reflog);
return error;
}
+
+int git_stash_pop(
+ git_repository *repo,
+ size_t index,
+ const git_stash_apply_options *options)
+{
+ int error;
+
+ if ((error = git_stash_apply(repo, index, options)) < 0)
+ return error;
+
+ return git_stash_drop(repo, index);
+}