update-index: use unmerge_index_entry() to support removal

"update-index --unresolve" uses the unmerge_index_entry_at() that assumes that the path to be unresolved must be in the index, which makes it impossible to unresolve a path that was resolved as removal. Rewrite unresolve_one() to use the unmerge_index_entry() to support unresolving such a path. Existing tests for "update-index --unresolve" forgot to check one thing that tests for "checkout --merge -- paths" tested, which is to make sure that resolve-undo record that has already been used to recreate higher-stage index entries is removed. Add new invocations of "ls-files --resolve-undo" after running "update-index --unresolve" to make sure that unresolving with update-index does remove the used resolve-undo records. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 31, 2023 at 15:44 UTC 35901f1c2461640019e70bbefecdce1f59f660d2
2 files changed +55 -20
builtin/update-index.c
+22 -16
@@ -660,26 +660,31 @@ static int unresolve_one(const char *path)
660 int pos;
661 int ret = 0;
662 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
663 + struct resolve_undo_info *ru = NULL;
664 +
665 + if (the_index.resolve_undo) {
666 + struct string_list_item *item;
667 + item = string_list_lookup(the_index.resolve_undo, path);
668 + if (item) {
669 + ru = item->util;
670 + item->util = NULL;
671 + }
672 + }
673 +
674 + /* resolve-undo record exists for the path */
675 + if (ru) {
676 + ret = unmerge_index_entry(&the_index, path, ru);
677 + free(ru);
678 + return ret;
679 + }
680
681 /* See if there is such entry in the index. */
682 pos = index_name_pos(&the_index, path, namelen);
683 if (0 <= pos) {
667 - /* already merged */
668 - pos = unmerge_index_entry_at(&the_index, pos);
669 - if (pos < the_index.cache_nr) {
670 - const struct cache_entry *ce = the_index.cache[pos];
671 - if (ce_stage(ce) &&
672 - ce_namelen(ce) == namelen &&
673 - !memcmp(ce->name, path, namelen))
674 - return 0;
675 - }
676 - /* no resolve-undo information; fall back */
684 + ; /* resolve-undo record was used already -- fall back */
685 } else {
678 - /* If there isn't, either it is unmerged, or
679 - * resolved as "removed" by mistake. We do not
680 - * want to do anything in the former case.
681 - */
682 - pos = -pos-1;
686 + /* Is it unmerged? */
687 + pos = -pos - 1;
688 if (pos < the_index.cache_nr) {
689 const struct cache_entry *ce = the_index.cache[pos];
690 if (ce_namelen(ce) == namelen &&
@@ -687,9 +692,10 @@ static int unresolve_one(const char *path)
692 fprintf(stderr,
693 "%s: skipping still unmerged path.\n",
694 path);
690 - goto free_return;
695 }
696 + goto free_return;
697 }
698 + /* No, such a path does not exist -- removed */
699 }
700
701 /*
t/t2030-unresolve-info.sh
+33 -4
@@ -37,11 +37,17 @@ prime_resolve_undo () {
37 git checkout second^0 &&
38 test_tick &&
39 test_must_fail git merge third^0 &&
40 - echo merge does not leave anything &&
40 check_resolve_undo empty &&
42 - echo different >fi/le &&
43 - git add fi/le &&
44 - echo resolving records &&
41 +
42 + # how should the conflict be resolved?
43 + case "$1" in
44 + remove)
45 + rm -f file/le && git rm fi/le
46 + ;;
47 + *) # modify
48 + echo different >fi/le && git add fi/le
49 + ;;
50 + esac
51 check_resolve_undo recorded fi/le initial:fi/le second:fi/le third:fi/le
52 }
53
@@ -122,6 +128,8 @@ test_expect_success 'add records checkout -m undoes' '
128 test_expect_success 'unmerge with plumbing' '
129 prime_resolve_undo &&
130 git update-index --unresolve fi/le &&
131 + git ls-files --resolve-undo fi/le >actual &&
132 + test_must_be_empty actual &&
133 git ls-files -u >actual &&
134 test_line_count = 3 actual
135 '
@@ -130,6 +138,27 @@ test_expect_success 'unmerge can be done even after committing' '
138 prime_resolve_undo &&
139 git commit -m "record to nuke MERGE_HEAD" &&
140 git update-index --unresolve fi/le &&
141 + git ls-files --resolve-undo fi/le >actual &&
142 + test_must_be_empty actual &&
143 + git ls-files -u >actual &&
144 + test_line_count = 3 actual
145 +'
146 +
147 +test_expect_success 'unmerge removal' '
148 + prime_resolve_undo remove &&
149 + git update-index --unresolve fi/le &&
150 + git ls-files --resolve-undo fi/le >actual &&
151 + test_must_be_empty actual &&
152 + git ls-files -u >actual &&
153 + test_line_count = 3 actual
154 +'
155 +
156 +test_expect_success 'unmerge removal after committing' '
157 + prime_resolve_undo remove &&
158 + git commit -m "record to nuke MERGE_HEAD" &&
159 + git update-index --unresolve fi/le &&
160 + git ls-files --resolve-undo fi/le >actual &&
161 + test_must_be_empty actual &&
162 git ls-files -u >actual &&
163 test_line_count = 3 actual
164 '