summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2015-06-20 13:44:22 -0700
committerEdward Thomson <ethomson@edwardthomson.com>2015-06-25 18:34:36 -0400
commit82b1c93d088319c4e385c11ce738b68103eab96c (patch)
treeab3c961fea42fde1a8d739be70d8744e5e8ae324 /src
parent1db6a0ab52e54397e450c2796dce65eced8c4c25 (diff)
downloadlibgit2-82b1c93d088319c4e385c11ce738b68103eab96c.tar.gz
stash: don't allow apply with staged changes
Diffstat (limited to 'src')
-rw-r--r--src/stash.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/stash.c b/src/stash.c
index 9010c476d..14cbeb642 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -733,6 +733,29 @@ int git_stash_apply_init_options(git_stash_apply_options *opts, unsigned int ver
} \
} while(false);
+static int ensure_clean_index(git_repository *repo, git_index *index)
+{
+ git_tree *head_tree = NULL;
+ git_diff *index_diff = NULL;
+ int error = 0;
+
+ if ((error = git_repository_head_tree(&head_tree, repo)) < 0 ||
+ (error = git_diff_tree_to_index(
+ &index_diff, repo, head_tree, index, NULL)) < 0)
+ goto done;
+
+ if (git_diff_num_deltas(index_diff) > 0) {
+ giterr_set(GITERR_STASH, "%d uncommitted changes exist in the index",
+ git_diff_num_deltas(index_diff));
+ error = GIT_EUNCOMMITTED;
+ }
+
+done:
+ git_diff_free(index_diff);
+ git_tree_free(head_tree);
+ return error;
+}
+
int git_stash_apply(
git_repository *repo,
size_t index,
@@ -775,6 +798,9 @@ int git_stash_apply(
NOTIFY_PROGRESS(opts, GIT_STASH_APPLY_PROGRESS_ANALYZE_INDEX);
+ if ((error = ensure_clean_index(repo, repo_index)) < 0)
+ goto cleanup;
+
/* 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))) {