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

Re: [PATCH v4 7/9] add -p: calculate offset delta for edited patches

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 1, 2018, 20:14 UTC
Message-ID
<xmqqwoyvft92.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<20180301105103.24268-8-phillip.wood@talktalk.net>
Phillip Wood <phillip.wood@talktalk.net> writes:
Show 24 quoted lines
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>
> Recount the number of preimage and postimage lines in a hunk after it
> has been edited so any change in the number of insertions or deletions
> can be used to adjust the offsets of subsequent hunks. If an edited
> hunk is subsequently split then the offset correction will be lost. It
> would be possible to fix this if it is a problem, however the code
> here is still an improvement on the status quo for the common case
> where an edited hunk is applied without being split.
>
> This is also a necessary step to removing '--recount' and
> '--allow-overlap' from the invocation of 'git apply'. Before
> '--recount' can be removed the splitting and coalescing counting needs
> to be fixed to handle a missing newline at the end of a file. In order
> to remove '--allow-overlap' there needs to be i) some way of verifying
> the offset data in the edited hunk (probably by correlating the
> preimage (or postimage if the patch is going to be applied in reverse)
> lines of the edited and unedited versions to see if they are offset or
> if any leading/trailing context lines have been removed) and ii) a way of
> dealing with edited hunks that change context lines that are shared
> with neighbouring hunks.
>
> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
> ---
Thanks for clear description of what is going on in the series.
Show 11 quoted lines
> diff --git a/git-add--interactive.perl b/git-add--interactive.perl
> index 7a0a5896bb..0df0c2aa06 100755
> --- a/git-add--interactive.perl
> +++ b/git-add--interactive.perl
> @@ -938,13 +938,19 @@ sub coalesce_overlapping_hunks {
>  						parse_hunk_header($text->[0]);
>  		unless ($_->{USE}) {
>  			$ofs_delta += $o_cnt - $n_cnt;
> +			# If this hunk has been edited then subtract
> +			# the delta that is due to the edit.
> +			$_->{OFS_DELTA} and $ofs_delta -= $_->{OFS_DELTA};
The pattern
	<<conditional>> and <<statement with side effect>>;

is something you are newly introducing to this script. I am not sure if we want to see them. I somehow find them harder to read than the more straight-forward and naïve

	if (<<conditional>>) {
		<<statement with side effect>>;
	}
> +		# If this hunk was edited then adjust the offset delta
> +		# to reflect the edit.
> +		$_->{OFS_DELTA} and $ofs_delta += $_->{OFS_DELTA};
Likewise.
Show 23 quoted lines
> +sub recount_edited_hunk {
> +	local $_;
> +	my ($oldtext, $newtext) = @_;
> +	my ($o_cnt, $n_cnt) = (0, 0);
> +	for (@{$newtext}[1..$#{$newtext}]) {
> +		my $mode = substr($_, 0, 1);
> +		if ($mode eq '-') {
> +			$o_cnt++;
> +		} elsif ($mode eq '+') {
> +			$n_cnt++;
> +		} elsif ($mode eq ' ') {
> +			$o_cnt++;
> +			$n_cnt++;
> +		}
> +	}
> +	my ($o_ofs, undef, $n_ofs, undef) =
> +					parse_hunk_header($newtext->[0]);
> +	$newtext->[0] = format_hunk_header($o_ofs, $o_cnt, $n_ofs, $n_cnt);
> +	my (undef, $orig_o_cnt, undef, $orig_n_cnt) =
> +					parse_hunk_header($oldtext->[0]);
> +	# Return the change in the number of lines inserted by this hunk
> +	return $orig_o_cnt - $orig_n_cnt - $o_cnt + $n_cnt;
> +}
OK.
Show 16 quoted lines
> @@ -1114,25 +1144,32 @@ sub prompt_yesno {
>  }
>  
>  sub edit_hunk_loop {
> -	my ($head, $hunk, $ix) = @_;
> -	my $text = $hunk->[$ix]->{TEXT};
> +	my ($head, $hunks, $ix) = @_;
> +	my $hunk = $hunks->[$ix];
> +	my $text = $hunk->{TEXT};
> ...
> +		$newhunk->{OFS_DELTA} = recount_edited_hunk($text, $newtext);
> +		# If this hunk has already been edited then add the
> +		# offset delta of the previous edit to get the real
> +		# delta from the original unedited hunk.
> +		$hunk->{OFS_DELTA} and
> +				$newhunk->{OFS_DELTA} += $hunk->{OFS_DELTA};
Ahh, good point.
Previous: Phillip WoodNext: Phillip Wood
Message 54 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.