Skip to content

Commit 28d1122

Browse files
peffgitster
authored andcommitted
add-patch: handle "* Unmerged path" lines
When we generate a diff with --cached, unmerged entries have no oid for their index entry: $ git diff-index --abbrev --cached HEAD :100644 000000 f719efd 0000000 U my-conflict So when we are asked to produce a patch, since we only have one side, we just emit a special message: $ git diff-index --cached -p HEAD * Unmerged path my-conflict This confuses interactive-patch modes that look at cached diffs. For example: $ git reset -p BUG: add-patch.c:498: diff starts with unexpected line: * Unmerged path my-conflict Making things even more confusing, you'll get that error only if the unmerged entry is alphabetically the first changed file. Otherwise, we simply stick the unrecognized line to the end of the previous hunk. There it's mostly harmless, as it eventually gets fed back to "git apply", which happily ignores it. But it's still shown to the user attached to the hunk, which is wrong. So let's handle these lines as a noop. There's not really anything useful to do with a conflicted merge in this case, and that's what we do for other cases like "add -p". There we get a "diff --cc" line, which we accept as starting a new file, but we refuse to use any of its hunks (their headers start with "@@@" and not "@@ ", so we silently ignore them). It seems like simply recognizing the line and continuing in our parsing loop would work. But we actually need to run the rest of the loop body to handle matching up our colored/filtered output. But that code assumes that we have some active file_diff we're working on. So instead, we'll just insert a dummy entry into our array. This ends up the same as if we saw a "diff --cc" line (a file with no hunks). Reported-by: Philippe Blain <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 768bb23 commit 28d1122

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

add-patch.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
483483
if (!eol)
484484
eol = pend;
485485

486-
if (starts_with(p, "diff ")) {
486+
if (starts_with(p, "diff ") ||
487+
starts_with(p, "* Unmerged path ")) {
487488
complete_file(marker, hunk);
488489
ALLOC_GROW_BY(s->file_diff, s->file_diff_nr, 1,
489490
file_diff_alloc);

t/t3701-add-interactive.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,4 +1068,25 @@ test_expect_success 'show help from add--helper' '
10681068
test_cmp expect actual
10691069
'
10701070

1071+
test_expect_success 'reset -p with unmerged files' '
1072+
test_when_finished "git checkout --force main" &&
1073+
test_commit one conflict &&
1074+
git checkout -B side HEAD^ &&
1075+
test_commit two conflict &&
1076+
test_must_fail git merge one &&
1077+
1078+
# this is a noop with only an unmerged entry
1079+
git reset -p &&
1080+
1081+
# add files that sort before and after unmerged entry
1082+
echo a >a &&
1083+
echo z >z &&
1084+
git add a z &&
1085+
1086+
# confirm that we can reset those files
1087+
printf "%s\n" y y | git reset -p &&
1088+
git diff-index --cached --diff-filter=u HEAD >staged &&
1089+
test_must_be_empty staged
1090+
'
1091+
10711092
test_done

0 commit comments

Comments
 (0)