git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Unable to split then edit hunk in git interactive add

From
Sscarf <greenscarf005@gmail.com>
Date
Nov 24, 2023, 08:16 UTC
Message-ID
<8d7c8313-0f02-4218-ba2a-1716ada7956f@gmail.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.

|
Next: Phillip Wood
Message 1 of 2 in “Unable to split then edit hunk in git interactive add”
  1. scarfNov 24, 2023
  2. Phillip WoodNov 24, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.