Skip to content

Commit 8aa3856

Browse files
committed
resolve-undo: teach "update-index --unresolve" to use resolve-undo info
The update-index plumbing command had a hacky --unresolve implementation that was written back in the days when merge was the only way for users to end up with higher stages in the index, and assumed that stage #2 must have come from HEAD, stage #3 from MERGE_HEAD and didn't bother to compute the stage #1 information. There were several issues with this approach: - These days, merge is not the only command, and conflicts coming from commands like cherry-pick, "am -3", etc. cannot be recreated by looking at MERGE_HEAD; - For a conflict that came from a merge that had renames, picking up the same path from MERGE_HEAD and HEAD wouldn't help recreating it, either; - It may have been Ok not to recreate stage #1 back when it was written, because "diff --ours/--theirs" were the only availble ways to review conflicts and they don't need stage #1 information. "diff --cc" that was invented much later is a lot more useful way but it needs stage #1. We can use resolve-undo information recorded in the index extension to solve all of these issues. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4421a82 commit 8aa3856

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

builtin-update-index.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,18 @@ static int unresolve_one(const char *path)
433433

434434
/* See if there is such entry in the index. */
435435
pos = cache_name_pos(path, namelen);
436-
if (pos < 0) {
436+
if (0 <= pos) {
437+
/* already merged */
438+
pos = unmerge_cache_entry_at(pos);
439+
if (pos < active_nr) {
440+
struct cache_entry *ce = active_cache[pos];
441+
if (ce_stage(ce) &&
442+
ce_namelen(ce) == namelen &&
443+
!memcmp(ce->name, path, namelen))
444+
return 0;
445+
}
446+
/* no resolve-undo information; fall back */
447+
} else {
437448
/* If there isn't, either it is unmerged, or
438449
* resolved as "removed" by mistake. We do not
439450
* want to do anything in the former case.

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ static inline void remove_name_hash(struct cache_entry *ce)
338338
#define cache_name_exists(name, namelen, igncase) index_name_exists(&the_index, (name), (namelen), (igncase))
339339
#define cache_name_is_other(name, namelen) index_name_is_other(&the_index, (name), (namelen))
340340
#define resolve_undo_clear() resolve_undo_clear_index(&the_index)
341+
#define unmerge_cache_entry_at(at) unmerge_index_entry_at(&the_index, at)
341342
#define unmerge_cache(pathspec) unmerge_index(&the_index, pathspec)
342343
#endif
343344

t/t2030-unresolve-info.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,11 @@ test_expect_success 'add records checkout -m undoes' '
108108
grep "^++<<<<<<<" actual
109109
'
110110

111+
test_expect_success 'unmerge with plumbing' '
112+
prime_resolve_undo &&
113+
git update-index --unresolve file &&
114+
git ls-files -u >actual &&
115+
test $(wc -l <actual) = 3
116+
'
117+
111118
test_done

0 commit comments

Comments
 (0)