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

[PATCH v3 0/9] Correct offsets of hunks when one is skipped

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

I've fixed the use of test_set_editor most of which was already in pu and reworked the sha1 comparisons to rewrite the hashes in the index lines rather than deleting the index lines.

Cover letter to v1:

While working on a patch series to stage selected lines from a hunk without having to edit it I got worried that subsequent patches would be applied in the wrong place which lead to this series to correct the offsets of hunks following those that are skipped or edited.

Interdiff:
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 0fb9c0e0f1..05540ee9ef 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -10,6 +10,16 @@ then
 	test_done
 fi
 
+diff_cmp () {
+	for x
+	do
+		sed  -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
+		     -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
+		     "$x" >"$x.filtered"
+	done
+	test_cmp "$1.filtered" "$2.filtered"
+}
+
 test_expect_success 'setup (initial)' '
 	echo content >file &&
 	git add file &&
@@ -24,6 +34,7 @@ test_expect_success 'status works (initial)' '
 test_expect_success 'setup expected' '
 	cat >expected <<-EOF
 	new file mode 100644
+	index 0000000..d95f3ad
 	--- /dev/null
 	+++ b/file
 	@@ -0,0 +1 @@
@@ -33,8 +44,8 @@ test_expect_success 'setup expected' '
 
 test_expect_success 'diff works (initial)' '
 	(echo d; echo 1) | git add -i >output &&
-	sed -ne /^index/d -e "/new file/,/content/p" <output >diff &&
-	test_cmp expected diff
+	sed -ne "/new file/,/content/p" <output >diff &&
+	diff_cmp expected diff
 '
 test_expect_success 'revert works (initial)' '
 	git add file &&
@@ -59,6 +70,7 @@ test_expect_success 'status works (commit)' '
 
 test_expect_success 'setup expected' '
 	cat >expected <<-EOF
+	index 180b47c..b6f2c08 100644
 	--- a/file
 	+++ b/file
 	@@ -1 +1,2 @@
@@ -69,8 +81,8 @@ test_expect_success 'setup expected' '
 
 test_expect_success 'diff works (commit)' '
 	(echo d; echo 1) | git add -i >output &&
-	sed -ne "/^---/,/content/p" <output >diff &&
-	test_cmp expected diff
+	sed -ne "/^index/,/content/p" <output >diff &&
+	diff_cmp expected diff
 '
 test_expect_success 'revert works (commit)' '
 	git add file &&
@@ -88,8 +100,8 @@ test_expect_success 'setup expected' '
 test_expect_success 'dummy edit works' '
 	test_set_editor : &&
 	(echo e; echo a) | git add -p &&
-	git diff | sed /^index/d >diff &&
-	test_cmp expected diff
+	git diff > diff &&
+	diff_cmp expected diff
 '
 
 test_expect_success 'setup patch' '
@@ -103,12 +115,11 @@ test_expect_success 'setup patch' '
 '
 
 test_expect_success 'setup fake editor' '
-	FAKE_EDITOR="$(pwd)/fake-editor.sh" &&
-	write_script "$FAKE_EDITOR" <<-\EOF &&
+	write_script "fake_editor.sh" <<-\EOF &&
 	mv -f "$1" oldpatch &&
 	mv -f patch "$1"
 	EOF
-	test_set_editor "$FAKE_EDITOR"
+	test_set_editor "$(pwd)/fake_editor.sh"
 '
 
 test_expect_success 'bad edit rejected' '
@@ -143,6 +154,7 @@ test_expect_success 'setup patch' '
 test_expect_success 'setup expected' '
 	cat >expected <<-EOF
 	diff --git a/file b/file
+	index b5dd6c9..f910ae9 100644
 	--- a/file
 	+++ b/file
 	@@ -1,4 +1,4 @@
@@ -156,8 +168,8 @@ test_expect_success 'setup expected' '
 
 test_expect_success 'real edit works' '
 	(echo e; echo n; echo d) | git add -p &&
-	git diff | sed /^index/d >output &&
-	test_cmp expected output
+	git diff >output &&
+	diff_cmp expected output
 '
 
 test_expect_success 'skip files similarly as commit -a' '
@@ -165,11 +177,11 @@ test_expect_success 'skip files similarly as commit -a' '
 	echo file >.gitignore &&
 	echo changed >file &&
 	echo y | git add -p file &&
-	git diff | sed /^index/d >output &&
+	git diff >output &&
 	git reset &&
 	git commit -am commit &&
-	git diff | sed /^index/d >expected &&
-	test_cmp expected output &&
+	git diff >expected &&
+	diff_cmp expected output &&
 	git reset --hard HEAD^
 '
 rm -f .gitignore
@@ -214,6 +226,7 @@ test_expect_success 'setup again' '
 # Write the patch file with a new line at the top and bottom
 test_expect_success 'setup patch' '
 	cat >patch <<-EOF
+	index 180b47c..b6f2c08 100644
 	--- a/file
 	+++ b/file
 	@@ -1,2 +1,4 @@
@@ -228,7 +241,7 @@ test_expect_success 'setup patch' '
 # Expected output, diff is similar to the patch but w/ diff at the top
 test_expect_success 'setup expected' '
 	echo diff --git a/file b/file >expected &&
-	cat patch >>expected &&
+	cat patch |sed "/^index/s/ 100644/ 100755/" >>expected &&
 	cat >expected-output <<-EOF
 	--- a/file
 	+++ b/file
@@ -258,8 +271,8 @@ test_expect_success C_LOCALE_OUTPUT 'add first line works' '
 		sed -n -e "s/^Stage this hunk[^@]*\(@@ .*\)/\1/" \
 		       -e "/^[-+@ \\\\]"/p  >output &&
 	test_must_be_empty error &&
-	git diff --cached | sed /^index/d >diff &&
-	test_cmp expected diff &&
+	git diff --cached >diff &&
+	diff_cmp expected diff &&
 	test_cmp expected-output output
 '
 
@@ -267,6 +280,7 @@ test_expect_success 'setup expected' '
 	cat >expected <<-EOF
 	diff --git a/non-empty b/non-empty
 	deleted file mode 100644
+	index d95f3ad..0000000
 	--- a/non-empty
 	+++ /dev/null
 	@@ -1 +0,0 @@
@@ -281,14 +295,15 @@ test_expect_success 'deleting a non-empty file' '
 	git commit -m non-empty &&
 	rm non-empty &&
 	echo y | git add -p non-empty &&
-	git diff --cached | sed /^index/d >diff &&
-	test_cmp expected diff
+	git diff --cached >diff &&
+	diff_cmp expected diff
 '
 
 test_expect_success 'setup expected' '
 	cat >expected <<-EOF
 	diff --git a/empty b/empty
 	deleted file mode 100644
+	index e69de29..0000000
 	EOF
 '
 
@@ -299,8 +314,8 @@ test_expect_success 'deleting an empty file' '
 	git commit -m empty &&
 	rm empty &&
 	echo y | git add -p empty &&
-	git diff --cached | sed /^index/d >diff &&
-	test_cmp expected diff
+	git diff --cached >diff &&
+	diff_cmp expected diff
 '
 
 test_expect_success 'split hunk setup' '
@@ -326,7 +341,7 @@ test_expect_success 'split hunk "add -p (edit)"' '
 	#    exits.
 	printf "%s\n" s e     q n q q |
 	EDITOR=: git add -p &&
-	git diff | sed /^index/d >actual &&
+	git diff >actual &&
 	! grep "^+15" actual
 '
 
@@ -338,7 +353,7 @@ test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
 	printf "%s\n" s n y e   q n q q |
 	EDITOR=: git add -p 2>error &&
 	test_must_be_empty error &&
-	git diff | sed /^index/d >actual &&
+	git diff >actual &&
 	! grep "^+31" actual
 '
 
@@ -357,14 +372,15 @@ test_expect_success 'patch mode ignores unmerged entries' '
 	cat >expected <<-\EOF &&
 	* Unmerged path conflict.t
 	diff --git a/non-conflict.t b/non-conflict.t
+	index f766221..5ea2ed4 100644
 	--- a/non-conflict.t
 	+++ b/non-conflict.t
 	@@ -1 +1 @@
 	-non-conflict
 	+changed
 	EOF
-	git diff --cached | sed /^index/d >diff &&
-	test_cmp expected diff
+	git diff --cached >diff &&
+	diff_cmp expected diff
 '
 
 test_expect_success TTY 'diffs can be colorized' '
@@ -393,7 +409,7 @@ test_expect_success 'patch-mode via -i prompts for files' '
 
 	echo test >expect &&
 	git diff --cached --name-only >actual &&
-	test_cmp expect actual
+	diff_cmp expect actual
 '
 
 test_expect_success 'add -p handles globs' '
@@ -550,7 +566,6 @@ test_expect_success 'add -p works with pathological context lines' '
 
 test_expect_success 'add -p patch editing works with pathological context lines' '
 	git reset &&
-	test_set_editor "$FAKE_EDITOR" &&
 	# n q q below is in case edit fails
 	printf "%s\n" e y    n q q |
 	git add -p &&

Phillip Wood (9):
  add -i: add function to format hunk header
  t3701: indent here documents
  t3701: use test_write_lines and write_script
  t3701: don't hard code sha1 hash values
  t3701: add failing test for pathological context lines
  add -p: Adjust offsets of subsequent hunks when one is skipped
  add -p: calculate offset delta for edited patches
  add -p: fix counting when splitting and coalescing
  add -p: don't rely on apply's '--recount' option

 git-add--interactive.perl  | 106 +++++++++++++----
 t/t3701-add-interactive.sh | 288 +++++++++++++++++++++++++--------------------
 2 files changed, 240 insertions(+), 154 deletions(-)
-- 
2.16.1
Previous: Phillip WoodNext: Phillip Wood
Message 30 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.