# git restore --staged -p on merge conflict causes crash

3 messages from 2025-01-07 to 2025-01-07. Participants: Keith Mendoza, Kristoffer Haugsbakk.
Thread: https://gitlist.dev/t/62758

## Keith Mendoza, 2025-01-07 14:41

Subject: git restore --staged -p on merge conflict causes crash
Message-ID: <DM8PR02MB7942969BE7FEAFB48D9AEDCEC7112@DM8PR02MB7942.namprd02.prod.outlook.com>
URL: https://gitlist.dev/e/DM8PR02MB7942969BE7FEAFB48D9AEDCEC7112%40DM8PR02MB7942.namprd02.prod.outlook.com

```
What did you do before the bug happened? (Steps to reproduce your issue)
1. Start with a main branch
1. Add a new file (test.yaml)
1. git checkout --no-track -b branch-1
1. Commit a change to test.yaml
1. git checkout main
1. Commit a change to the same lines in test.yaml that was updated in branch-1
1. git checkout branch-1
1. git merge main
1. git restore --staged -p test.yaml

What did you expect to happen? (Expected behavior)
Be able to select which updates to restore

What happened instead? (Actual behavior)
The following is displayed on the console
ˋˋˋ
BUG: add-patch.c:497: diff starts with unexpected line:
* Unmerged path test.yaml

Aborted (core dumped)
ˋˋˋ

What's different between what you expected and what actually happened?
git crashed and generated a core dump.

Anything else you want to add:
Here's the output of git diff after the `git merge main`

ˋˋˋ
diff --cc test.yaml
index 26bf81a,46282a7..0000000
--- a/test.yaml
+++ b/test.yaml
@@@ -1,7 -1,7 +1,13 @@@
  job:
++<<<<<<< HEAD
 +  newfield:
 +    field1: blah
 +    field2: blah-blah
++=======
+   conflict-field:
+     blah1: blah-blah
+     blah2: blah-blah-blah
++>>>>>>> main
    something:
      required: true
      other: 1
ˋˋˋ

The generated core dump is attached.

Please review the rest of the bug report below.
You can delete any lines you don't wish to share.


[System Info]
git version:
git version 2.39.5
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64
compiler info: gnuc: 12.2
libc info: glibc: 2.36
$SHELL (typically, interactive shell): /bin/bash


[Enabled Hooks]

```

## Kristoffer Haugsbakk, 2025-01-07 15:34

Subject: Re: git restore --staged -p on merge conflict causes crash
Message-ID: <27ec479b-b116-4fa7-b87c-416a5f7f3b25@app.fastmail.com>
URL: https://gitlist.dev/e/27ec479b-b116-4fa7-b87c-416a5f7f3b25%40app.fastmail.com
In-Reply-To: <DM8PR02MB7942969BE7FEAFB48D9AEDCEC7112@DM8PR02MB7942.namprd02.prod.outlook.com>

```
On Tue, Jan 7, 2025, at 15:41, Keith Mendoza wrote:
> What did you do before the bug happened? (Steps to reproduce your issue)
> 1. Start with a main branch
> 1. Add a new file (test.yaml)
> 1. git checkout --no-track -b branch-1
> 1. Commit a change to test.yaml
> 1. git checkout main
> 1. Commit a change to the same lines in test.yaml that was updated in branch-1
> 1. git checkout branch-1
> 1. git merge main
> 1. git restore --staged -p test.yaml
>
> What did you expect to happen? (Expected behavior)
> Be able to select which updates to restore
>
> What happened instead? (Actual behavior)
> The following is displayed on the console
> ˋˋˋ
> BUG: add-patch.c:497: diff starts with unexpected line:
> * Unmerged path test.yaml
>
> Aborted (core dumped)
> ˋˋˋ

It looks[1] like this was fixed in 28d1122f9ca (add-patch: handle "*
Unmerged path" lines, 2023-03-09).[2] That fix was included in v2.41.0
and later.  So not on your maintenance release (v2.39.5).

The bug was introduced in 2.37.0:

Jeff King wrote:
> Thanks, a fix is below. While the escape-hatch of the perl version is
> going away in v2.40, people have been exposed to the bug since we
> flipped the default in v2.37.0.

At least I wasn’t able to reproduce on 2.48.0.rc2 with the below.

ˋˋˋ
git init &&
touch test.yaml &&
git add test.yaml &&
git commit -minit &&
git checkout --no-track -b branch-1 &&
echo something >test.yaml &&
git add test.yaml &&
git commit -mchange &&
git checkout main &&
echo 'something else (conflict)' >test.yaml &&
git add test.yaml &&
git commit -m'change here too' &&
git checkout branch-1 &&
# Merge conflict
git merge main
git restore --staged -p test.yaml
ˋˋˋ

† 1: Based on:

      BUG: add-patch.c:498: diff starts with unexpected line:

🔗 2: https://lore.kernel.org/git/ZAmfqC9WMl3XeyEr@coredump.intra.peff.net/

```

## Keith Mendoza, 2025-01-07 16:07

Subject: RE: git restore --staged -p on merge conflict causes crash
Message-ID: <DM8PR02MB79428E0A2C6C32EA67D791D1C7112@DM8PR02MB7942.namprd02.prod.outlook.com>
URL: https://gitlist.dev/e/DM8PR02MB79428E0A2C6C32EA67D791D1C7112%40DM8PR02MB7942.namprd02.prod.outlook.com
In-Reply-To: <27ec479b-b116-4fa7-b87c-416a5f7f3b25@app.fastmail.com>

```
Thanks for the information.


-----Original Message-----
From: Kristoffer Haugsbakk <kristofferhaugsbakk@fastmail.com> 
Sent: Tuesday, January 7, 2025 7:34 AM
To: Keith Mendoza <kmendoza@beyondtrust.com>; git@vger.kernel.org
Subject: Re: git restore --staged -p on merge conflict causes crash

This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.


On Tue, Jan 7, 2025, at 15:41, Keith Mendoza wrote:
> What did you do before the bug happened? (Steps to reproduce your issue)
> 1. Start with a main branch
> 1. Add a new file (test.yaml)
> 1. git checkout --no-track -b branch-1
> 1. Commit a change to test.yaml
> 1. git checkout main
> 1. Commit a change to the same lines in test.yaml that was updated in branch-1
> 1. git checkout branch-1
> 1. git merge main
> 1. git restore --staged -p test.yaml
>
> What did you expect to happen? (Expected behavior)
> Be able to select which updates to restore
>
> What happened instead? (Actual behavior)
> The following is displayed on the console
> ˋˋˋ
> BUG: add-patch.c:497: diff starts with unexpected line:
> * Unmerged path test.yaml
>
> Aborted (core dumped)
> ˋˋˋ

It looks[1] like this was fixed in 28d1122f9ca (add-patch: handle "*
Unmerged path" lines, 2023-03-09).[2] That fix was included in v2.41.0
and later.  So not on your maintenance release (v2.39.5).

The bug was introduced in 2.37.0:

Jeff King wrote:
> Thanks, a fix is below. While the escape-hatch of the perl version is
> going away in v2.40, people have been exposed to the bug since we
> flipped the default in v2.37.0.

At least I wasn’t able to reproduce on 2.48.0.rc2 with the below.

ˋˋˋ
git init &&
touch test.yaml &&
git add test.yaml &&
git commit -minit &&
git checkout --no-track -b branch-1 &&
echo something >test.yaml &&
git add test.yaml &&
git commit -mchange &&
git checkout main &&
echo 'something else (conflict)' >test.yaml &&
git add test.yaml &&
git commit -m'change here too' &&
git checkout branch-1 &&
# Merge conflict
git merge main
git restore --staged -p test.yaml
ˋˋˋ

† 1: Based on:

      BUG: add-patch.c:498: diff starts with unexpected line:

🔗 2: https://urldefense.com/v3/__https://lore.kernel.org/git/ZAmfqC9WMl3XeyEr@coredump.intra.peff.net/__;!!GAuxx38R6fLb!Jh_X9EDKuOi07wcMoWg66W79Lx_4t8FDTQeu-coao1J1o1a51DUz8ce6MOvCVMIUHqsP1IaqVB02dIITAty5bP4W5X6vVR0m$

```
