stash: refactor to use merge_iterators
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
diff --git a/include/git2/stash.h b/include/git2/stash.h
index f69ba30..428d9f2 100644
--- a/include/git2/stash.h
+++ b/include/git2/stash.h
@@ -97,10 +97,9 @@ typedef enum {
* the workdir and index will be left untouched.
*
* @param repo The owning repository.
- *
* @param index The position within the stash list. 0 points to the
- * most recent stashed state.
- *
+ * most recent stashed state.
+ * @param checkout_options Options to control how files are checked out
* @param flags Flags to control the applying process. (see GIT_APPLY_* above)
*
* @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
@@ -109,6 +108,7 @@ typedef enum {
GIT_EXTERN(int) git_stash_apply(
git_repository *repo,
size_t index,
+ const git_checkout_options *checkout_options,
unsigned int flags);
/**
@@ -167,10 +167,9 @@ GIT_EXTERN(int) git_stash_drop(
* if successful.
*
* @param repo The owning repository.
- *
* @param index The position within the stash list. 0 points to the
- * most recent stashed state.
- *
+ * most recent stashed state.
+ * @param checkout_options Options to control how files are checked out
* @param flags Flags to control the applying process. (see GIT_APPLY_* above)
*
* @return 0 on success, GIT_ENOTFOUND if there's no stashed state for the given
@@ -179,6 +178,7 @@ GIT_EXTERN(int) git_stash_drop(
GIT_EXTERN(int) git_stash_pop(
git_repository *repo,
size_t index,
+ const git_checkout_options *checkout_options,
unsigned int flags);
/** @} */
diff --git a/src/stash.c b/src/stash.c
index 899156d..d53c22c 100644
--- a/src/stash.c
+++ b/src/stash.c
@@ -18,7 +18,10 @@
#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)
{
@@ -645,187 +648,81 @@ cleanup:
return error;
}
-static int apply_index(
- git_tree **unstashed_tree,
+static int merge_index_and_tree(
+ git_index **out,
git_repository *repo,
- git_tree *start_index_tree,
- git_tree *index_parent_tree,
- git_tree *index_tree)
+ git_tree *ancestor_tree,
+ git_index *ours_index,
+ git_tree *theirs_tree)
{
- git_index *unstashed_index = NULL;
- git_merge_options options = GIT_MERGE_OPTIONS_INIT;
+ git_iterator *ancestor = NULL, *ours = NULL, *theirs = NULL;
+ const git_iterator_flag_t flags = GIT_ITERATOR_DONT_IGNORE_CASE;
int error;
- git_oid oid;
-
- if ((error = git_merge_trees(
- &unstashed_index, repo, index_parent_tree,
- start_index_tree, index_tree, &options)) < 0)
- goto cleanup;
-
- if ((error = git_index_write_tree_to(&oid, unstashed_index, repo)) < 0)
- goto cleanup;
-
- if ((error = git_tree_lookup(unstashed_tree, repo, &oid)) < 0)
- goto cleanup;
-
-cleanup:
- git_index_free(unstashed_index);
- return error;
-}
-
-static int apply_untracked(
- git_repository *repo,
- git_tree *start_index_tree,
- git_tree *untracked_tree)
-{
- git_checkout_options options = GIT_CHECKOUT_OPTIONS_INIT;
- git_index *merged_index = NULL;
- int error;
-
- options.checkout_strategy =
- GIT_CHECKOUT_SAFE | GIT_CHECKOUT_DONT_UPDATE_INDEX;
-
- if ((error = git_merge_trees(&merged_index,
- repo, NULL, start_index_tree, untracked_tree, NULL)) == 0)
- error = git_checkout_index(repo, merged_index, &options);
-
- git_index_free(merged_index);
- return error;
-}
-static int checkout_modified_notify_callback(
- git_checkout_notify_t why,
- const char *path,
- const git_diff_file *baseline,
- const git_diff_file *target,
- const git_diff_file *workdir,
- void *payload)
-{
- unsigned int status;
- int error;
-
- GIT_UNUSED(why);
- GIT_UNUSED(baseline);
- GIT_UNUSED(target);
- GIT_UNUSED(workdir);
-
- if ((error = git_status_file(&status, payload, path)) < 0)
- return error;
-
- if (status & GIT_STATUS_WT_MODIFIED) {
- giterr_set(GITERR_STASH, "Local changes to '%s' would be overwritten", path);
- return GIT_EMERGECONFLICT;
- }
-
- return 0;
-}
-
-static int apply_modified(
- int *has_conflicts,
- git_repository *repo,
- git_tree *base_tree,
- git_tree *start_index_tree,
- git_tree *stash_tree,
- unsigned int flags)
-{
- git_index *index = NULL;
- git_merge_options merge_options = GIT_MERGE_OPTIONS_INIT;
- git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
- int error;
-
- if ((error = git_merge_trees(
- &index, repo, base_tree,
- start_index_tree, stash_tree, &merge_options)) < 0)
- goto cleanup;
-
- checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
- if ((flags & GIT_APPLY_REINSTATE_INDEX) && !git_index_has_conflicts(index)) {
- /* No need to update the index if it will be overridden later on */
- checkout_options.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
- }
- checkout_options.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
- checkout_options.notify_cb = checkout_modified_notify_callback;
- checkout_options.notify_payload = repo;
- checkout_options.our_label = "Updated upstream";
- checkout_options.their_label = "Stashed changes";
- if ((error = git_checkout_index(repo, index, &checkout_options)) < 0)
- goto cleanup;
+ 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;
- *has_conflicts = git_index_has_conflicts(index);
+ error = git_merge__iterators(out, repo, ancestor, ours, theirs, NULL);
-cleanup:
- git_index_free(index);
+done:
+ git_iterator_free(ancestor);
+ git_iterator_free(ours);
+ git_iterator_free(theirs);
return error;
}
-static int unstage_modified_files(
- git_repository *repo,
- git_index *repo_index,
- git_tree *unstashed_tree,
- git_tree *start_index_tree)
-{
- git_diff *diff = NULL;
- git_diff_options options = GIT_DIFF_OPTIONS_INIT;
- size_t i, count;
- int error;
-
- if (unstashed_tree) {
- if ((error = git_index_read_tree(repo_index, unstashed_tree)) < 0)
- goto cleanup;
+static void normalize_checkout_options(
+ git_checkout_options *checkout_opts,
+ const git_checkout_options *given_checkout_opts)
+{
+ if (given_checkout_opts != NULL) {
+ memcpy(checkout_opts, given_checkout_opts, sizeof(git_checkout_options));
} else {
- options.flags = GIT_DIFF_FORCE_BINARY;
- if ((error = git_diff_tree_to_index(&diff, repo, start_index_tree,
- repo_index, &options)) < 0)
- goto cleanup;
-
- /*
- This behavior is not 100% similar to "git stash apply" as the latter uses
- "git-read-tree --reset {treeish}" which preserves the stat()s from the
- index instead of replacing them with the tree ones for identical files.
- */
+ git_checkout_options default_checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
+ default_checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
- if ((error = git_index_read_tree(repo_index, start_index_tree)) < 0)
- goto cleanup;
-
- for (i = 0, count = git_diff_num_deltas(diff); i < count; ++i) {
- const git_diff_delta* delta = git_diff_get_delta(diff, i);
- if (delta->status == GIT_DELTA_ADDED) {
- if ((error = git_index_add_bypath(
- repo_index, delta->new_file.path)) < 0)
- goto cleanup;
- }
- }
+ memcpy(checkout_opts, &default_checkout_opts, sizeof(git_checkout_options));
}
-cleanup:
- git_diff_free(diff);
- return error;
+ if (!checkout_opts->our_label)
+ checkout_opts->our_label = "Updated upstream";
+
+ if (!checkout_opts->their_label)
+ checkout_opts->their_label = "Stashed changes";
}
int git_stash_apply(
git_repository *repo,
size_t index,
+ const git_checkout_options *given_checkout_opts,
unsigned int flags)
{
+ git_checkout_options checkout_opts;
+ unsigned int checkout_strategy;
git_commit *stash_commit = NULL;
git_tree *stash_tree = NULL;
- git_tree *base_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_tree *start_index_tree = NULL;
- git_tree *unstashed_tree = NULL;
- int has_conflicts;
+ git_index *unstashed_index = NULL;
+ git_index *modified_index = NULL;
+ git_index *untracked_index = NULL;
int error;
+ normalize_checkout_options(&checkout_opts, given_checkout_opts);
+ checkout_strategy = checkout_opts.checkout_strategy;
+
/* 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, &base_tree, &index_tree,
+ &stash_tree, &stash_parent_tree, &index_tree,
&index_parent_tree, &untracked_tree, stash_commit)) < 0)
goto cleanup;
@@ -833,50 +730,73 @@ int git_stash_apply(
if ((error = git_repository_index(&repo_index, repo)) < 0)
goto cleanup;
- /* Create tree from index */
- if ((error = build_tree_from_index(&start_index_tree, repo_index)) < 0)
- goto cleanup;
-
/* Restore index if required */
if ((flags & GIT_APPLY_REINSTATE_INDEX) &&
- git_oid_cmp(git_tree_id(base_tree), git_tree_id(index_tree)) &&
- git_oid_cmp(git_tree_id(start_index_tree), git_tree_id(index_tree))) {
+ git_oid_cmp(git_tree_id(stash_parent_tree), git_tree_id(index_tree))) {
- if ((error = apply_index(
- &unstashed_tree, repo, start_index_tree,
- index_parent_tree, index_tree)) < 0)
+ if ((error = merge_index_and_tree(
+ &unstashed_index, repo, index_parent_tree, repo_index, index_tree)) < 0)
goto cleanup;
- }
- /* If applicable, restore untracked / ignored files in workdir */
- if (untracked_tree) {
- if ((error = apply_untracked(repo, start_index_tree, untracked_tree)) < 0)
+
+ /* TODO: GIT_EMERGECONFLICT */
+ if (git_index_has_conflicts(unstashed_index)) {
+ error = GIT_EUNMERGED;
goto cleanup;
+ }
}
/* Restore modified files in workdir */
- if ((error = apply_modified(
- &has_conflicts, repo, base_tree, start_index_tree,
- stash_tree, flags)) < 0)
+ if ((error = merge_index_and_tree(
+ &modified_index, repo, stash_parent_tree, repo_index, stash_tree)) < 0)
goto cleanup;
- /* Unstage modified files from index unless there were merge conflicts */
- if (!has_conflicts && (error = unstage_modified_files(
- repo, repo_index, unstashed_tree, start_index_tree)) < 0)
+ /* If applicable, restore untracked / ignored files in workdir */
+ if (untracked_tree &&
+ (error = merge_index_and_tree(&untracked_index, repo, NULL, repo_index, untracked_tree)) < 0)
goto cleanup;
- /* Write updated index */
- if ((error = git_index_write(repo_index)) < 0)
+ if (untracked_index) {
+ checkout_opts.checkout_strategy |= GIT_CHECKOUT_DONT_UPDATE_INDEX;
+
+ if ((error = git_checkout_index(repo, untracked_index, &checkout_opts)) < 0)
+ goto cleanup;
+
+ checkout_opts.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))
+ checkout_opts.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.
+ */
+ checkout_opts.baseline_index = repo_index;
+
+ if ((error = git_checkout_index(repo, modified_index, &checkout_opts)) < 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;
+ }
+
cleanup:
- git_tree_free(unstashed_tree);
- git_tree_free(start_index_tree);
+ 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(base_tree);
+ git_tree_free(stash_parent_tree);
git_tree_free(stash_tree);
git_commit_free(stash_commit);
return error;
@@ -984,11 +904,12 @@ cleanup:
int git_stash_pop(
git_repository *repo,
size_t index,
+ const git_checkout_options *checkout_options,
unsigned int flags)
{
int error;
- if ((error = git_stash_apply(repo, index, flags)) < 0)
+ if ((error = git_stash_apply(repo, index, checkout_options, flags)) < 0)
return error;
return git_stash_drop(repo, index);
diff --git a/tests/stash/apply.c b/tests/stash/apply.c
index c0cdcab..efe7852 100644
--- a/tests/stash/apply.c
+++ b/tests/stash/apply.c
@@ -63,7 +63,7 @@ void test_stash_apply__cleanup(void)
void test_stash_apply__with_default(void)
{
- cl_git_pass(git_stash_apply(repo, 0, GIT_APPLY_DEFAULT));
+ cl_git_pass(git_stash_apply(repo, 0, NULL, GIT_APPLY_DEFAULT));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
@@ -74,7 +74,7 @@ void test_stash_apply__with_default(void)
void test_stash_apply__with_reinstate_index(void)
{
- cl_git_pass(git_stash_apply(repo, 0, GIT_APPLY_REINSTATE_INDEX));
+ cl_git_pass(git_stash_apply(repo, 0, NULL, GIT_APPLY_REINSTATE_INDEX));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
@@ -93,7 +93,7 @@ void test_stash_apply__conflict_index_with_default(void)
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_write(repo_index));
- cl_git_pass(git_stash_apply(repo, 0, GIT_APPLY_DEFAULT));
+ cl_git_pass(git_stash_apply(repo, 0, NULL, GIT_APPLY_DEFAULT));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
assert_status(repo, "what", GIT_STATUS_INDEX_MODIFIED);
@@ -108,7 +108,7 @@ void test_stash_apply__conflict_index_with_reinstate_index(void)
cl_git_pass(git_index_add_bypath(repo_index, "who"));
cl_git_pass(git_index_write(repo_index));
- cl_git_fail_with(git_stash_apply(repo, 0, GIT_APPLY_REINSTATE_INDEX), GIT_EUNMERGED);
+ cl_git_fail_with(git_stash_apply(repo, 0, NULL, GIT_APPLY_REINSTATE_INDEX), GIT_EUNMERGED);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_CURRENT);
@@ -121,7 +121,7 @@ void test_stash_apply__conflict_untracked_with_default(void)
{
cl_git_mkfile("stash/when", "nothing\n");
- cl_git_fail_with(git_stash_apply(repo, 0, GIT_APPLY_DEFAULT), GIT_EMERGECONFLICT);
+ cl_git_fail_with(git_stash_apply(repo, 0, NULL, GIT_APPLY_DEFAULT), GIT_EMERGECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_CURRENT);
@@ -134,7 +134,7 @@ void test_stash_apply__conflict_untracked_with_reinstate_index(void)
{
cl_git_mkfile("stash/when", "nothing\n");
- cl_git_fail_with(git_stash_apply(repo, 0, GIT_APPLY_REINSTATE_INDEX), GIT_EMERGECONFLICT);
+ cl_git_fail_with(git_stash_apply(repo, 0, NULL, GIT_APPLY_REINSTATE_INDEX), GIT_EMERGECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_CURRENT);
@@ -147,7 +147,7 @@ void test_stash_apply__conflict_workdir_with_default(void)
{
cl_git_rewritefile("stash/what", "ciao\n");
- cl_git_fail_with(git_stash_apply(repo, 0, GIT_APPLY_DEFAULT), GIT_EMERGECONFLICT);
+ cl_git_fail_with(git_stash_apply(repo, 0, NULL, GIT_APPLY_DEFAULT), GIT_EMERGECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
@@ -160,7 +160,7 @@ void test_stash_apply__conflict_workdir_with_reinstate_index(void)
{
cl_git_rewritefile("stash/what", "ciao\n");
- cl_git_fail_with(git_stash_apply(repo, 0, GIT_APPLY_REINSTATE_INDEX), GIT_EMERGECONFLICT);
+ cl_git_fail_with(git_stash_apply(repo, 0, NULL, GIT_APPLY_REINSTATE_INDEX), GIT_EMERGECONFLICT);
cl_assert_equal_i(git_index_has_conflicts(repo_index), 0);
assert_status(repo, "what", GIT_STATUS_WT_MODIFIED);
@@ -179,7 +179,7 @@ void test_stash_apply__conflict_commit_with_default(void)
cl_git_pass(git_index_add_bypath(repo_index, "what"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
- cl_git_pass(git_stash_apply(repo, 0, GIT_APPLY_DEFAULT));
+ cl_git_pass(git_stash_apply(repo, 0, NULL, GIT_APPLY_DEFAULT));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "what")); /* unmerged */
@@ -198,7 +198,7 @@ void test_stash_apply__conflict_commit_with_reinstate_index(void)
cl_git_pass(git_index_add_bypath(repo_index, "what"));
cl_repo_commit_from_index(NULL, repo, signature, 0, "Other commit");
- cl_git_pass(git_stash_apply(repo, 0, GIT_APPLY_REINSTATE_INDEX));
+ cl_git_pass(git_stash_apply(repo, 0, NULL, GIT_APPLY_REINSTATE_INDEX));
cl_assert_equal_i(git_index_has_conflicts(repo_index), 1);
cl_git_pass(git_index_conflict_get(&ancestor, &our, &their, repo_index, "what")); /* unmerged */
@@ -209,7 +209,7 @@ void test_stash_apply__conflict_commit_with_reinstate_index(void)
void test_stash_apply__pop(void)
{
- cl_git_pass(git_stash_pop(repo, 0, GIT_APPLY_DEFAULT));
+ cl_git_pass(git_stash_pop(repo, 0, NULL, GIT_APPLY_DEFAULT));
- cl_git_fail_with(git_stash_pop(repo, 0, GIT_APPLY_DEFAULT), GIT_ENOTFOUND);
+ cl_git_fail_with(git_stash_pop(repo, 0, NULL, GIT_APPLY_DEFAULT), GIT_ENOTFOUND);
}