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

[PATCH v3 6/9] add -p: Adjust offsets of subsequent hunks when one is skipped

From
PWPhillip Wood <phillip.wood@talktalk.net>
Date
Feb 27, 2018, 11:04 UTC
Message-ID
<20180227110404.16816-7-phillip.wood@talktalk.net>
In-Reply-To
<20180227110404.16816-1-phillip.wood@talktalk.net>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Since commit 8cbd431082 ("git-add--interactive: replace hunk recounting with apply --recount", 2008-7-2) if a hunk is skipped then we rely on the context lines to apply subsequent hunks in the right place. While this works most of the time it is possible for hunks to end up being applied in the wrong place. To fix this adjust the offset of subsequent hunks to correct for any change in the number of insertions or deletions due to the skipped hunk. The change in offset due to edited hunks that have the number of insertions or deletions changed is ignored here, it will be fixed in the next commit.

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
 git-add--interactive.perl  | 15 +++++++++++++--
 t/t3701-add-interactive.sh |  2 +-
 2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 8ababa6453..7a0a5896bb 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -926,14 +926,25 @@ sub coalesce_overlapping_hunks {
 	my @out = ();
 
 	my ($last_o_ctx, $last_was_dirty);
+	my $ofs_delta = 0;
 
-	for (grep { $_->{USE} } @in) {
+	for (@in) {
 		if ($_->{TYPE} ne 'hunk') {
 			push @out, $_;
 			next;
 		}
 		my $text = $_->{TEXT};
-		my ($o_ofs) = parse_hunk_header($text->[0]);
+		my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
+						parse_hunk_header($text->[0]);
+		unless ($_->{USE}) {
+			$ofs_delta += $o_cnt - $n_cnt;
+			next;
+		}
+		if ($ofs_delta) {
+			$n_ofs += $ofs_delta;
+			$_->{TEXT}->[0] = format_hunk_header($o_ofs, $o_cnt,
+							     $n_ofs, $n_cnt);
+		}
 		if (defined $last_o_ctx &&
 		    $o_ofs <= $last_o_ctx &&
 		    !$_->{DIRTY} &&
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index da73fbdf4d..4c30aa6b6e 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -541,7 +541,7 @@ test_expect_success 'set up pathological context' '
 	test_write_lines +b " a" >patch
 '
 
-test_expect_failure 'add -p works with pathological context lines' '
+test_expect_success 'add -p works with pathological context lines' '
 	git reset &&
 	printf "%s\n" n y |
 	git add -p &&
-- 
2.16.1
Previous: Junio C HamanoNext: Phillip Wood
Message 45 of 79 in “Correct offsets of hunks when one is skipped”
  1. 0/4 Correct offsets of hunks when one is skippedPhillip Wood, Feb 13, 2018
  2. 1/4 add -i: add function to format hunk headerPhillip Wood, Feb 13, 2018
  3. 3/4 add -p: Adjust offsets of subsequent hunks when one is skippedPhillip Wood, Feb 13, 2018
  4. 4/4 add -p: calculate offset delta for edited patchesPhillip Wood, Feb 13, 2018
  5. 2/4 t3701: add failing test for pathological context linesPhillip Wood, Feb 13, 2018
  6. Junio C HamanoFeb 13, 2018
  7. brian m. carlsonFeb 13, 2018
  8. Phillip WoodFeb 19, 2018
  9. 0/9 Correct offsets of hunks when one is skippedPhillip Wood, Feb 19, 2018
  10. 1/9 add -i: add function to format hunk headerPhillip Wood, Feb 19, 2018
  11. Junio C HamanoFeb 20, 2018
  12. 2/9 t3701: indent here documentsPhillip Wood, Feb 19, 2018
  13. Eric SunshineFeb 19, 2018
  14. 7/9 add -p: calculate offset delta for edited patchesPhillip Wood, Feb 19, 2018
  15. 9/9 add -p: don't rely on apply's '--recount' optionPhillip Wood, Feb 19, 2018
  16. Junio C HamanoFeb 20, 2018
  17. 8/9 add -p: fix counting when splitting and coalescingPhillip Wood, Feb 19, 2018
  18. 4/9 t3701: don't hard code sha1 hash valuesPhillip Wood, Feb 19, 2018
  19. Eric SunshineFeb 19, 2018
  20. Junio C HamanoFeb 20, 2018
  21. Phillip WoodFeb 21, 2018
  22. Junio C HamanoFeb 21, 2018
  23. 3/9 t3701: use test_write_lines and write_scriptPhillip Wood, Feb 19, 2018
  24. Eric SunshineFeb 19, 2018
  25. Junio C HamanoFeb 20, 2018
  26. Phillip WoodFeb 21, 2018
  27. 6/9 add -p: Adjust offsets of subsequent hunks when one is skippedPhillip Wood, Feb 19, 2018
  28. 5/9 t3701: add failing test for pathological context linesPhillip Wood, Feb 19, 2018
  29. Phillip WoodFeb 21, 2018
  30. 0/9 Correct offsets of hunks when one is skippedPhillip Wood, Feb 27, 2018
  31. 1/9 add -i: add function to format hunk headerPhillip Wood, Feb 27, 2018
  32. 4/9 t3701: don't hard code sha1 hash valuesPhillip Wood, Feb 27, 2018
  33. Junio C HamanoFeb 27, 2018
  34. Phillip WoodFeb 28, 2018
  35. Phillip WoodFeb 28, 2018
  36. 3/9 t3701: use test_write_lines and write_scriptPhillip Wood, Feb 27, 2018
  37. 5/9 t3701: add failing test for pathological context linesPhillip Wood, Feb 27, 2018
  38. 9/9 add -p: don't rely on apply's '--recount' optionPhillip Wood, Feb 27, 2018
  39. 2/9 t3701: indent here documentsPhillip Wood, Feb 27, 2018
  40. Junio C HamanoFeb 27, 2018
  41. Phillip WoodFeb 28, 2018
  42. Junio C HamanoFeb 28, 2018
  43. Phillip WoodMar 1, 2018
  44. Junio C HamanoMar 1, 2018
  45. 6/9 add -p: Adjust offsets of subsequent hunks when one is skippedPhillip Wood, Feb 27, 2018
  46. 7/9 add -p: calculate offset delta for edited patchesPhillip Wood, Feb 27, 2018
  47. 8/9 add -p: fix counting when splitting and coalescingPhillip Wood, Feb 27, 2018
  48. 0/9 Correct offsets of hunks when one is skippedPhillip Wood, Mar 1, 2018
  49. 1/9 add -i: add function to format hunk headerPhillip Wood, Mar 1, 2018
  50. 9/9 add -p: don't rely on apply's '--recount' optionPhillip Wood, Mar 1, 2018
  51. Junio C HamanoMar 1, 2018
  52. Phillip WoodMar 2, 2018
  53. 7/9 add -p: calculate offset delta for edited patchesPhillip Wood, Mar 1, 2018
  54. Junio C HamanoMar 1, 2018
  55. Phillip WoodMar 2, 2018
  56. 3/9 t3701: use test_write_lines and write_scriptPhillip Wood, Mar 1, 2018
  57. 4/9 t3701: don't hard code sha1 hash valuesPhillip Wood, Mar 1, 2018
  58. SZEDER GáborMar 2, 2018
  59. Junio C HamanoMar 2, 2018
  60. Phillip WoodMar 5, 2018
  61. SZEDER GáborMar 5, 2018
  62. 2/9 t3701: indent here documentsPhillip Wood, Mar 1, 2018
  63. 5/9 t3701: add failing test for pathological context linesPhillip Wood, Mar 1, 2018
  64. 6/9 add -p: adjust offsets of subsequent hunks when one is skippedPhillip Wood, Mar 1, 2018
  65. 8/9 add -p: fix counting when splitting and coalescingPhillip Wood, Mar 1, 2018
  66. Junio C HamanoMar 1, 2018
  67. Phillip WoodMar 2, 2018
  68. 0/9 Correct offsets of hunks when one is skippedPhillip Wood, Mar 5, 2018
  69. 1/9 add -i: add function to format hunk headerPhillip Wood, Mar 5, 2018
  70. 2/9 t3701: indent here documentsPhillip Wood, Mar 5, 2018
  71. 3/9 t3701: use test_write_lines and write_scriptPhillip Wood, Mar 5, 2018
  72. 8/9 add -p: fix counting when splitting and coalescingPhillip Wood, Mar 5, 2018
  73. 6/9 add -p: adjust offsets of subsequent hunks when one is skippedPhillip Wood, Mar 5, 2018
  74. 5/9 t3701: add failing test for pathological context linesPhillip Wood, Mar 5, 2018
  75. 4/9 t3701: don't hard code sha1 hash valuesPhillip Wood, Mar 5, 2018
  76. 7/9 add -p: calculate offset delta for edited patchesPhillip Wood, Mar 5, 2018
  77. 9/9 add -p: don't rely on apply's '--recount' optionPhillip Wood, Mar 5, 2018
  78. Junio C HamanoMar 5, 2018
  79. Phillip WoodMar 6, 2018

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.