git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Unable to split then edit hunk in git interactive add
@ 2023-11-24  8:16 scarf
  2023-11-24 15:19 ` Phillip Wood
  0 siblings, 1 reply; 2+ messages in thread
From: scarf @ 2023-11-24  8:16 UTC (permalink / raw)
  To: git

|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.

|

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-11-24 15:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-24  8:16 Unable to split then edit hunk in git interactive add scarf
2023-11-24 15:19 ` Phillip Wood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).