unpack-trees: create working copy of merge entry in merged_entry

Duplicate the merge entry right away and work with that instead of modifying the entry we got and duplicating it only at the end of the function. Then mark that pointer const to document that we don't modify the referenced cache_entry. This change is safe because all existing merge functions call merged_entry just before returning (or not at all), i.e. they don't care about changes to the referenced cache_entry after the call. unpack_nondirectories and unpack_index_entry, which call the merge functions through call_unpack_fn, aren't interested in such changes neither. The change complicates merged_entry a bit because we have to free the copy if we error out, but allows callers to pass a const pointer. Signed-off-by: René Scharfe <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jun 2, 2013 at 17:46 UTC f2fa35420511cc49e85413a2932a1a2bac88cc1b
1 file changed +12 -5
unpack-trees.c
+12 -5
@@ -1466,10 +1466,12 @@ static int verify_absent_sparse(struct cache_entry *ce,
1466 return verify_absent_1(ce, orphaned_error, o);
1467 }
1468
1469 -static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1470 - struct unpack_trees_options *o)
1469 +static int merged_entry(const struct cache_entry *ce,
1470 + struct cache_entry *old,
1471 + struct unpack_trees_options *o)
1472 {
1473 int update = CE_UPDATE;
1474 + struct cache_entry *merge = dup_entry(ce);
1475
1476 if (!old) {
1477 /*
@@ -1487,8 +1489,11 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1489 update |= CE_ADDED;
1490 merge->ce_flags |= CE_NEW_SKIP_WORKTREE;
1491
1490 - if (verify_absent(merge, ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o))
1492 + if (verify_absent(merge,
1493 + ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN, o)) {
1494 + free(merge);
1495 return -1;
1496 + }
1497 invalidate_ce_path(merge, o);
1498 } else if (!(old->ce_flags & CE_CONFLICTED)) {
1499 /*
@@ -1502,8 +1507,10 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1507 copy_cache_entry(merge, old);
1508 update = 0;
1509 } else {
1505 - if (verify_uptodate(old, o))
1510 + if (verify_uptodate(old, o)) {
1511 + free(merge);
1512 return -1;
1513 + }
1514 /* Migrate old flags over */
1515 update |= old->ce_flags & (CE_SKIP_WORKTREE | CE_NEW_SKIP_WORKTREE);
1516 invalidate_ce_path(old, o);
@@ -1516,7 +1523,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
1523 invalidate_ce_path(old, o);
1524 }
1525
1519 - add_entry(o, merge, update, CE_STAGEMASK);
1526 + do_add_entry(o, merge, update, CE_STAGEMASK);
1527 return 1;
1528 }
1529