# Unable to split then edit hunk in git interactive add

2 messages from 2023-11-24 to 2023-11-24. Participants: scarf, Phillip Wood.
Thread: https://gitlist.dev/t/60552

## scarf, 2023-11-24 08:16

Subject: Unable to split then edit hunk in git interactive add
Message-ID: <8d7c8313-0f02-4218-ba2a-1716ada7956f@gmail.com>
URL: https://gitlist.dev/e/8d7c8313-0f02-4218-ba2a-1716ada7956f%40gmail.com

```
|Thank you for filling out a Git bug report!
Please answer the following questions to help us understand your issue.

What did you do before the bug happened? (Steps to reproduce your issue)

1. Initialized a new repo with `git init`.
1-1. Created first commit with a file `main.txt` with the following content:
ˋˋˋrs
pub fn format_text(
   input_text: &str,
   format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
) -> Result<Option<String>> {
   let parse_result = jsonc_parser::parse_to_ast(
     input_text,
     &CollectOptions {
       comments: false,
       tokens: false,
     },
     &ParseOptions {
       allow_comments: true,
       allow_loose_object_property_names: true,
       allow_trailing_commas: true,
     },
   )?;
   let Some(root_value) = parse_result.value else {
     return Ok(None);
   };

   Ok(format_root(input_text, &root_value, format_with_host))
}
ˋˋˋ

1-2. Edited the content of the file to:
ˋˋˋrs
pub fn format_text(
   input_text: &str,
   format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
) -> Result<Option<String>> {
   let parse_result = jsonc_parser::parse_to_ast(input_text, 
&COLLECT_OPTIONS, &PARSE_OPTIONS)?;

   let format_result = parse_result
     .value
     .and_then(|root_value| format_root(input_text, &root_value, 
format_with_host));

   Ok(format_result)
}
ˋˋˋ

1-3. Running `git status` will show
ˋˋˋrs
On branch main
Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git restore <file>..." to discard changes in working directory)
         modified:   main.txt
ˋˋˋ

2. To reliably reproduce the error:
2-1. run `git add --patch`
2-2. (s)plit once
2-3. (e)dit first hunk. the content of `addp-hunk-edit.diff` is:
ˋˋˋdiff
@@ -2,19 +2,5 @@ pub fn format_text(
    input_text: &str,
    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
  ) -> Result<Option<String>> {
-  let parse_result = jsonc_parser::parse_to_ast(
-    input_text,
-    &CollectOptions {
-      comments: false,
-      tokens: false,
-    },
-    &ParseOptions {
-      allow_comments: true,
-      allow_loose_object_property_names: true,
-      allow_trailing_commas: true,
-    },
-  )?;
-  let Some(root_value) = parse_result.value else {
-    return Ok(None);
-  };
+  let parse_result = jsonc_parser::parse_to_ast(input_text, 
&COLLECT_OPTIONS, &PARSE_OPTIONS)?;
ˋˋˋ

2-4. undelete(remove '-') L18-L20, move L21 up so it goes right before 
L18. the edited content of `addp-hunk-edit.diff` is:
ˋˋˋdiff
@@ -2,19 +2,5 @@ pub fn format_text(
    input_text: &str,
    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
  ) -> Result<Option<String>> {
-  let parse_result = jsonc_parser::parse_to_ast(
-    input_text,
-    &CollectOptions {
-      comments: false,
-      tokens: false,
-    },
-    &ParseOptions {
-      allow_comments: true,
-      allow_loose_object_property_names: true,
-      allow_trailing_commas: true,
-    },
-  )?;
+  let parse_result = jsonc_parser::parse_to_ast(input_text, 
&COLLECT_OPTIONS, &PARSE_OPTIONS)?;
    let Some(root_value) = parse_result.value else {
      return Ok(None);
    };
ˋˋˋ

2-5. it fails with `hunks do not overlap` error. full error message is:
ˋˋˋ
error: hunks do not overlap:
    input_text: &str,
    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,
  ) -> Result<Option<String>> {
-  let parse_result = jsonc_parser::parse_to_ast(
-    input_text,
-    &CollectOptions {
-      comments: false,
-      tokens: false,
-    },
-    &ParseOptions {
-      allow_comments: true,
-      allow_loose_object_property_names: true,
-      allow_trailing_commas: true,
-    },
-  )?;
+  let parse_result = jsonc_parser::parse_to_ast(input_text, 
&COLLECT_OPTIONS, &PARSE_OPTIONS)?;
    let Some(root_value) = parse_result.value else {
      return Ok(None);
    };


         does not end with:


error: patch failed: main.txt:20
error: main.txt: patch does not apply
error: 'git apply --cached' failed
ˋˋˋ

3. to side-step the error,
3-1. run `git add --patch`
3-2. follow 2-1 to 2-4 from above
3-3. it works without error

What did you expect to happen? (Expected behavior)

I expected editing splitted hunk to work without errors.

What happened instead? (Actual behavior)

Editing hunks will only work if the hunk is not splitted.

What's different between what you expected and what actually happened?

This behavior is inconsistent and undocumented in 
https://git-scm.com/docs/git-add,
which lead me to believe this is a bug.

Anything else you want to add:

I apologize for the long wall of demo snippets.
I wasn't sure whether it's allowed to send repository as tarball or link 
the formatted content.

This stackoverflow post shows the exact same error: 
https://stackoverflow.com/q/62896307/13503626
its comment in https://stackoverflow.com/a/62897311/13503626 mentions 
sending a bug report on mailing list,
however after searching through mailing list archive at 
https://lore.kernel.org/git/?q=b%3A"split"+b%3A"edit"+b%3A"hunk"
I couldn't find any bug report related to this issue.

[System Info]
git version:
git version 2.40.1
cpu: x86_64
no commit associated with this build
sizeof-long: 8
sizeof-size_t: 8
shell-path: /bin/sh
uname: Linux 6.5.0-10-generic #10-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 13 
13:49:38 UTC 2023 x86_64
compiler info: gnuc: 12.3
libc info: glibc: 2.38
$SHELL (typically, interactive shell): /usr/bin/fish


[Enabled Hooks]
None.

|

```

## Phillip Wood, 2023-11-24 15:19

Subject: Re: Unable to split then edit hunk in git interactive add
Message-ID: <b6e24bb6-a497-4d37-a938-cfbcdbe77a9d@gmail.com>
URL: https://gitlist.dev/e/b6e24bb6-a497-4d37-a938-cfbcdbe77a9d%40gmail.com
In-Reply-To: <8d7c8313-0f02-4218-ba2a-1716ada7956f@gmail.com>

```
On 24/11/2023 08:16, scarf wrote:
> |Thank you for filling out a Git bug report!
> Please answer the following questions to help us understand your issue.
> 
> What did you do before the bug happened? (Steps to reproduce your issue)

Thanks for taking the time to report this. While it is a known 
limitation of "git add -p" that editing a hunk after splitting it can 
cause problems (see below) I am unable to reproduce this particular bug.

When a hunk is split the resulting hunks share the context lines around 
the split. The trailing context lines of the first hunk are the same 
lines as the leading context of the second hunk. If the shared context 
is edited then the lines no-longer match between the hunks which means 
they cannot be applied. In general it is better to avoid splitting a 
hunk if you plan to edit it.

Best Wishes

Phillip

```
