{"thread":{"id":"60552","subject":"Unable to split then edit hunk in git interactive add","startedAt":"2023-11-24T08:16:33Z","lastAt":"2023-11-24T15:20:04Z","messageCount":2,"participants":["scarf","Phillip Wood"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"485101","messageId":"8d7c8313-0f02-4218-ba2a-1716ada7956f@gmail.com","threadId":"60552","inReplyTo":null,"subject":"Unable to split then edit hunk in git interactive add","fromName":"scarf","fromEmail":"greenscarf005@gmail.com","sentAt":"2023-11-24T08:16:28Z","receivedAt":"2023-11-24T08:16:33Z","isPatch":false,"sender":{"key":"greenscarf005@gmail.com","avatar":null},"body":"|Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\n\n1. Initialized a new repo with `git init`.\n1-1. Created first commit with a file `main.txt` with the following content:\n```rs\npub fn format_text(\n   input_text: &str,\n   format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,\n) -> Result<Option<String>> {\n   let parse_result = jsonc_parser::parse_to_ast(\n     input_text,\n     &CollectOptions {\n       comments: false,\n       tokens: false,\n     },\n     &ParseOptions {\n       allow_comments: true,\n       allow_loose_object_property_names: true,\n       allow_trailing_commas: true,\n     },\n   )?;\n   let Some(root_value) = parse_result.value else {\n     return Ok(None);\n   };\n\n   Ok(format_root(input_text, &root_value, format_with_host))\n}\n```\n\n1-2. Edited the content of the file to:\n```rs\npub fn format_text(\n   input_text: &str,\n   format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,\n) -> Result<Option<String>> {\n   let parse_result = jsonc_parser::parse_to_ast(input_text, \n&COLLECT_OPTIONS, &PARSE_OPTIONS)?;\n\n   let format_result = parse_result\n     .value\n     .and_then(|root_value| format_root(input_text, &root_value, \nformat_with_host));\n\n   Ok(format_result)\n}\n```\n\n1-3. Running `git status` will show\n```rs\nOn branch main\nChanges not staged for commit:\n   (use \"git add <file>...\" to update what will be committed)\n   (use \"git restore <file>...\" to discard changes in working directory)\n         modified:   main.txt\n```\n\n2. To reliably reproduce the error:\n2-1. run `git add --patch`\n2-2. (s)plit once\n2-3. (e)dit first hunk. the content of `addp-hunk-edit.diff` is:\n```diff\n@@ -2,19 +2,5 @@ pub fn format_text(\n    input_text: &str,\n    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,\n  ) -> Result<Option<String>> {\n-  let parse_result = jsonc_parser::parse_to_ast(\n-    input_text,\n-    &CollectOptions {\n-      comments: false,\n-      tokens: false,\n-    },\n-    &ParseOptions {\n-      allow_comments: true,\n-      allow_loose_object_property_names: true,\n-      allow_trailing_commas: true,\n-    },\n-  )?;\n-  let Some(root_value) = parse_result.value else {\n-    return Ok(None);\n-  };\n+  let parse_result = jsonc_parser::parse_to_ast(input_text, \n&COLLECT_OPTIONS, &PARSE_OPTIONS)?;\n```\n\n2-4. undelete(remove '-') L18-L20, move L21 up so it goes right before \nL18. the edited content of `addp-hunk-edit.diff` is:\n```diff\n@@ -2,19 +2,5 @@ pub fn format_text(\n    input_text: &str,\n    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,\n  ) -> Result<Option<String>> {\n-  let parse_result = jsonc_parser::parse_to_ast(\n-    input_text,\n-    &CollectOptions {\n-      comments: false,\n-      tokens: false,\n-    },\n-    &ParseOptions {\n-      allow_comments: true,\n-      allow_loose_object_property_names: true,\n-      allow_trailing_commas: true,\n-    },\n-  )?;\n+  let parse_result = jsonc_parser::parse_to_ast(input_text, \n&COLLECT_OPTIONS, &PARSE_OPTIONS)?;\n    let Some(root_value) = parse_result.value else {\n      return Ok(None);\n    };\n```\n\n2-5. it fails with `hunks do not overlap` error. full error message is:\n```\nerror: hunks do not overlap:\n    input_text: &str,\n    format_with_host: impl FnMut(&Path, String) -> Result<Option<String>>,\n  ) -> Result<Option<String>> {\n-  let parse_result = jsonc_parser::parse_to_ast(\n-    input_text,\n-    &CollectOptions {\n-      comments: false,\n-      tokens: false,\n-    },\n-    &ParseOptions {\n-      allow_comments: true,\n-      allow_loose_object_property_names: true,\n-      allow_trailing_commas: true,\n-    },\n-  )?;\n+  let parse_result = jsonc_parser::parse_to_ast(input_text, \n&COLLECT_OPTIONS, &PARSE_OPTIONS)?;\n    let Some(root_value) = parse_result.value else {\n      return Ok(None);\n    };\n\n\n         does not end with:\n\n\nerror: patch failed: main.txt:20\nerror: main.txt: patch does not apply\nerror: 'git apply --cached' failed\n```\n\n3. to side-step the error,\n3-1. run `git add --patch`\n3-2. follow 2-1 to 2-4 from above\n3-3. it works without error\n\nWhat did you expect to happen? (Expected behavior)\n\nI expected editing splitted hunk to work without errors.\n\nWhat happened instead? (Actual behavior)\n\nEditing hunks will only work if the hunk is not splitted.\n\nWhat's different between what you expected and what actually happened?\n\nThis behavior is inconsistent and undocumented in \nhttps://git-scm.com/docs/git-add,\nwhich lead me to believe this is a bug.\n\nAnything else you want to add:\n\nI apologize for the long wall of demo snippets.\nI wasn't sure whether it's allowed to send repository as tarball or link \nthe formatted content.\n\nThis stackoverflow post shows the exact same error: \nhttps://stackoverflow.com/q/62896307/13503626\nits comment in https://stackoverflow.com/a/62897311/13503626 mentions \nsending a bug report on mailing list,\nhowever after searching through mailing list archive at \nhttps://lore.kernel.org/git/?q=b%3A\"split\"+b%3A\"edit\"+b%3A\"hunk\"\nI couldn't find any bug report related to this issue.\n\n[System Info]\ngit version:\ngit version 2.40.1\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 6.5.0-10-generic #10-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 13 \n13:49:38 UTC 2023 x86_64\ncompiler info: gnuc: 12.3\nlibc info: glibc: 2.38\n$SHELL (typically, interactive shell): /usr/bin/fish\n\n\n[Enabled Hooks]\nNone.\n\n|\n"},{"id":"485124","messageId":"b6e24bb6-a497-4d37-a938-cfbcdbe77a9d@gmail.com","threadId":"60552","inReplyTo":"8d7c8313-0f02-4218-ba2a-1716ada7956f@gmail.com","subject":"Re: Unable to split then edit hunk in git interactive add","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2023-11-24T15:19:59Z","receivedAt":"2023-11-24T15:20:04Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 24/11/2023 08:16, scarf wrote:\n> |Thank you for filling out a Git bug report!\n> Please answer the following questions to help us understand your issue.\n> \n> What did you do before the bug happened? (Steps to reproduce your issue)\n\nThanks for taking the time to report this. While it is a known \nlimitation of \"git add -p\" that editing a hunk after splitting it can \ncause problems (see below) I am unable to reproduce this particular bug.\n\nWhen a hunk is split the resulting hunks share the context lines around \nthe split. The trailing context lines of the first hunk are the same \nlines as the leading context of the second hunk. If the shared context \nis edited then the lines no-longer match between the hunks which means \nthey cannot be applied. In general it is better to avoid splitting a \nhunk if you plan to edit it.\n\nBest Wishes\n\nPhillip\n"}]}