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

Re: [PATCH v3] git-add--interactive: manual hunk editing mode

From
Jeff King <peff@peff.net>
Date
Jun 10, 2008, 11:05 UTC
Message-ID
<20080610110553.GA13655@sigill.intra.peff.net>
In-Reply-To
<200806092319.23906.johan@herland.net>
On Mon, Jun 09, 2008 at 11:19:23PM +0200, Johan Herland wrote:
> On Monday 09 June 2008, Jeff King wrote:
> > But honestly, I don't really see a use case that isn't covered by
> > "manually edit the diff and apply the hunk". And the rationale in your
> > "side note" indicates that you think the same way.

Below is a simple patch to build interactive edit support on top of Dscho's "git apply --recount" patch. Rather than create new hunks, the action is just "edit and apply": if the apply is successful, the hunk is removed from further consideration. This is just for playing with, and not for commit:

  - it's not very well tested :)
  - I think the recount stuff may have flaws; I didn't follow the thread
    too closely, but Junio seemed to have a lot of comments (I am
    working under the assumption that Dscho will get that part right,
    and we can just build on top of it)
  - I tried one or two simple tests where I edited and applied an early
    hunk, and then resumed the loop for later hunks. Everything seemed
    to work, but I am not convinced that it isn't possible to make the
    rest of the hunk selection loop useless by applying early and
    invalidating all of the other hunks. That might be a flaw with this
    approach. A more sane interface might be to simply jump from the
    hunk selection loop into manually editing _all_ hunks and then
    applying, ending the hunk selection loop.

This should be applied on top of "allow git-apply to ignore the hunk headers" (v3) which can be found on the list. Obviously significant portions of the patch below are based on Thomas' version.

-- >8 -- git-add--interactive: manual hunk editing mode

Adds a new option 'e' to the 'add -p' command loop that lets you
edit the current hunk in your favourite editor, applying the
result.
---
 git-add--interactive.perl |   81 +++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 81 insertions(+), 0 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 903953e..62b5aed 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -2,6 +2,7 @@
 
 use strict;
 use Git;
+use File::Temp;
 
 my $repo = Git->repository();
 
@@ -770,6 +771,76 @@ sub coalesce_overlapping_hunks {
 	return @out;
 }
 
+sub edit_hunk_manually {
+	my ($oldtext) = @_;
+
+	my $t = File::Temp->new(
+		TEMPLATE => $repo->repo_path . "/git-hunk-edit.XXXXXX",
+		SUFFIX => '.diff'
+	);
+	print $t "# Manual hunk edit mode -- see bottom for a quick guide\n";
+	print $t @$oldtext;
+	print $t <<EOF;
+# ---
+# To remove '-' lines, make them ' ' lines (context).
+# To remove '+' lines, delete them.
+# Empty lines and lines starting with # will be removed.
+#
+# Lines starting with @ start a new hunk. Line counts will be adjusted
+# according to contents. If the line numbers are missing altogether,
+# they will be inferred from the previous hunk.
+#
+# You can change the hunk to your heart's content, but it will be
+# refused if the end result (the entire patch including your edited
+# hunk) does not apply cleanly.
+EOF
+	close $t;
+
+	my $editor = $ENV{GIT_EDITOR} || $repo->config("core.editor")
+		|| $ENV{VISUAL} || $ENV{EDITOR} || "vi";
+	system('sh', '-c', $editor.' "$@"', $editor, $t);
+
+	open my $fh, '<', $t
+		or die "failed to open hunk edit file for reading: " . $!;
+	my @newtext = grep { !/^#/ } <$fh>;
+	close $fh;
+
+	# Reinsert the first hunk header if the user accidentally deleted it
+	if ($newtext[0] !~ /^@/) {
+		unshift @newtext, $oldtext->[0];
+	}
+	return \@newtext;
+}
+
+sub apply_diff {
+	open(my $fh, '|-', qw(git apply --cached --recount));
+	print $fh map { @$_ } @_;
+	return close $fh;
+}
+
+sub prompt_yesno {
+	my ($prompt) = @_;
+	while (1) {
+		print colored $prompt_color, $prompt;
+		my $line = <STDIN>;
+		return 0 if $line =~ /^n/i;
+		return 1 if $line =~ /^y/i;
+	}
+}
+
+sub edit_hunk_loop {
+	my ($head, $text) = @_;
+
+	while (1) {
+		$text = edit_hunk_manually($text);
+		apply_diff($head, $text) and return 1;
+		prompt_yesno(
+			'Your edited hunk does not apply. Edit again '
+			. '(saying "no" discards!) [y/n]? '
+		) or return 0;
+	}
+}
+
 sub help_patch_cmd {
 	print colored $help_color, <<\EOF ;
 y - stage this hunk
@@ -781,6 +852,7 @@ J - leave this hunk undecided, see next hunk
 k - leave this hunk undecided, see previous undecided hunk
 K - leave this hunk undecided, see previous hunk
 s - split the current hunk into smaller hunks
+e - manually edit the current hunk
 ? - print help
 EOF
 }
@@ -885,6 +957,7 @@ sub patch_update_file {
 		if (hunk_splittable($hunk[$ix]{TEXT})) {
 			$other .= '/s';
 		}
+		$other .= '/e';
 		for (@{$hunk[$ix]{DISPLAY}}) {
 			print;
 		}
@@ -949,6 +1022,14 @@ sub patch_update_file {
 				$num = scalar @hunk;
 				next;
 			}
+			elsif ($line =~ /^e/) {
+				if (edit_hunk_loop($head->{TEXT},
+						   $hunk[$ix]{TEXT})) {
+					splice @hunk, $ix, 1;
+					$num = @hunk;
+				}
+				next;
+			}
 			else {
 				help_patch_cmd($other);
 				next;
-- 
1.5.6.rc2.26.gda075.dirty
Previous: Johan HerlandNext: Thomas Rast
Message 42 of 62 in “git-add--interactive: manual hunk editing mode”
  1. git-add--interactive: manual hunk editing modeThomas Rast, May 23, 2008
  2. Ping YinMay 24, 2008
  3. Thomas RastMay 29, 2008
  4. Johannes SchindelinMay 29, 2008
  5. Thomas RastMay 29, 2008
  6. Thomas RastMay 29, 2008
  7. Jeff KingMay 29, 2008
  8. Johannes SchindelinMay 30, 2008
  9. Jakub NarebskiMay 30, 2008
  10. Thomas RastMay 30, 2008
  11. Junio C HamanoMay 30, 2008
  12. git-add--interactive: manual hunk editing mode v2Thomas Rast, Jun 1, 2008
  13. Thomas RastJun 1, 2008
  14. Jeff KingJun 1, 2008
  15. Jeff KingJun 5, 2008
  16. Thomas RastJun 5, 2008
  17. Jeff KingJun 5, 2008
  18. Thomas RastJun 5, 2008
  19. Jeff KingJun 5, 2008
  20. Thomas RastJun 5, 2008
  21. Jeff KingJun 5, 2008
  22. Junio C HamanoJun 5, 2008
  23. Jeff KingJun 5, 2008
  24. Johannes SchindelinJun 5, 2008
  25. Jeff KingJun 6, 2008
  26. Jeff KingJun 6, 2008
  27. Thomas RastJun 8, 2008
  28. Johannes SchindelinJun 8, 2008
  29. Johannes SchindelinJun 6, 2008
  30. Thomas RastJun 8, 2008
  31. Johannes SchindelinJun 8, 2008
  32. git-add--interactive: manual hunk editing modeThomas Rast, Jun 5, 2008
  33. git-add--interactive: manual hunk editing modeThomas Rast, Jun 8, 2008
  34. Johannes SchindelinJun 8, 2008
  35. Johan HerlandJun 9, 2008
  36. Jeff KingJun 9, 2008
  37. Johannes SchindelinJun 9, 2008
  38. Junio C HamanoJun 9, 2008
  39. Johan HerlandJun 9, 2008
  40. Jeff KingJun 9, 2008
  41. Johan HerlandJun 9, 2008
  42. Jeff KingJun 10, 2008
  43. Thomas RastJun 11, 2008
  44. Jeff KingJun 12, 2008
  45. Thomas RastJun 12, 2008
  46. Jeff KingJun 12, 2008
  47. git-add--interactive: manual hunk editing modeThomas Rast, Jun 13, 2008
  48. Jeff KingJun 23, 2008
  49. Johannes SchindelinJun 23, 2008
  50. Jeff KingJun 23, 2008
  51. apply --recount, was Re: [PATCH v4] git-add--interactive: manual hunk editing modeJohannes Schindelin, Jun 23, 2008
  52. Jeff KingJun 24, 2008
  53. 0/3 Manual editing for 'add' and 'add -p'Thomas Rast, Jun 24, 2008
  54. Miklos VajnaJun 24, 2008
  55. 1/3 Allow git-apply to ignore the hunk headers (AKA recountdiff)Thomas Rast, Jun 24, 2008
  56. Junio C HamanoJun 24, 2008
  57. Jeff KingJun 25, 2008
  58. Johannes SchindelinJun 27, 2008
  59. 2/3 git-add: introduce --edit (to edit the diff vs. the index)Thomas Rast, Jun 24, 2008
  60. 3/3 git-add--interactive: manual hunk editing modeThomas Rast, Jun 24, 2008
  61. Andreas EricssonJun 10, 2008
  62. Thomas RastJun 5, 2008

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.