Skip to content

Commit fe83269

Browse files
committed
resolve-undo: allow resurrecting conflicted state that resolved to deletion
The resolve-undo index extension records up to three (mode, object name) tuples for non-zero stages for each path that was resolved, to be used to recreate the original conflicted state later when the user requests. The unmerge_index_entry_at() function uses the resolve-undo data to do so, but it assumes that the path for which the conflicted state needs to be recreated can be specified by the position in the active_cache[] array. This obviously cannot salvage the state of conflicted paths that were resolved by removing them. For example, a delete-modify conflict, in which the change whose "modify" side made is a trivial typofix, may legitimately be resolved to remove the path, and resolve-undo extension does record the two (mode, object name) tuples for the common ancestor version and their version, lacking our version. But after recording such a removal of the path, you should be able to use resolve-undo data to recreate the conflicted state. Introduce a new unmerge_index_entry() helper function that takes the path (which does not necessarily have to exist in the active_cache[] array) and resolve-undo data, and use it to reimplement unmerge_index() public function that is used by "git rerere". The limited interface is still kept for now, as it is used by "git checkout -m" and "git update-index --unmerge", but these two codepaths will be updated to lift the assumption to allow conflicts that resolved to deletion can be recreated. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 91e0705 commit fe83269

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

resolve-undo.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,19 +182,57 @@ void unmerge_marked_index(struct index_state *istate)
182182
}
183183
}
184184

185+
int unmerge_index_entry(struct index_state *istate, const char *path,
186+
struct resolve_undo_info *ru)
187+
{
188+
int i = index_name_pos(istate, path, strlen(path));
189+
190+
if (i < 0) {
191+
/* unmerged? */
192+
i = -i - 1;
193+
if (i < istate->cache_nr &&
194+
!strcmp(istate->cache[i]->name, path))
195+
/* yes, it is already unmerged */
196+
return 0;
197+
/* fallthru: resolved to removal */
198+
} else {
199+
/* merged - remove it to replace it with unmerged entries */
200+
remove_index_entry_at(istate, i);
201+
}
202+
203+
for (i = 0; i < 3; i++) {
204+
struct cache_entry *ce;
205+
if (!ru->mode[i])
206+
continue;
207+
ce = make_cache_entry(istate, ru->mode[i], &ru->oid[i],
208+
path, i + 1, 0);
209+
if (add_index_entry(istate, ce, ADD_CACHE_OK_TO_ADD))
210+
return error("cannot unmerge '%s'", path);
211+
}
212+
return 0;
213+
}
214+
185215
void unmerge_index(struct index_state *istate, const struct pathspec *pathspec)
186216
{
187-
int i;
217+
struct string_list_item *item;
188218

189219
if (!istate->resolve_undo)
190220
return;
191221

192222
/* TODO: audit for interaction with sparse-index. */
193223
ensure_full_index(istate);
194-
for (i = 0; i < istate->cache_nr; i++) {
195-
const struct cache_entry *ce = istate->cache[i];
196-
if (!ce_path_match(istate, ce, pathspec, NULL))
224+
225+
for_each_string_list_item(item, istate->resolve_undo) {
226+
const char *path = item->string;
227+
struct resolve_undo_info *ru = item->util;
228+
if (!item->util)
229+
continue;
230+
if (!match_pathspec(istate, pathspec,
231+
item->string, strlen(item->string),
232+
0, NULL, 0))
197233
continue;
198-
i = unmerge_index_entry_at(istate, i);
234+
unmerge_index_entry(istate, path, ru);
235+
free(ru);
236+
item->util = NULL;
199237
}
200238
}

resolve-undo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ void resolve_undo_write(struct strbuf *, struct string_list *);
1818
struct string_list *resolve_undo_read(const char *, unsigned long);
1919
void resolve_undo_clear_index(struct index_state *);
2020
int unmerge_index_entry_at(struct index_state *, int);
21+
int unmerge_index_entry(struct index_state *, const char *, struct resolve_undo_info *);
2122
void unmerge_index(struct index_state *, const struct pathspec *);
2223
void unmerge_marked_index(struct index_state *);
2324

0 commit comments

Comments
 (0)