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

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

From
Thomas Rast <trast@student.ethz.ch>
Date
May 23, 2008, 20:21 UTC
Message-ID
<200805232221.45406.trast@student.ethz.ch>

Adds a new option 'e' to the 'add -p' command loop that lets you discard or keep one hunk line at a time. This is useful if there are no unchanged lines in the middle of the hunk, so 's' will not work, but you would still like to split it.

Signed-off-by: Thomas Rast <trast@student.ethz.ch>
---

This is my first patch, and I had to dust off my Perl knowledge a bit, so I hope it is up to your standards.

I could have used this a few times because I frequently hack around a bit, then after some time notice that this should really go in two separate commits. Usually 'add -p' can separate the changes, but if their diff lines are immediately adjacent, one has to go back and undo some editing before the first commit, then redo later.

I'm not quite happy with the scheme I use to handle colored diffs. 'git diff' apparently does not always reset the color before newlines (is this a bug?), so I insert extra resets. However, I did not want to implement "full" diff coloring directly in git-add--interactive.

Also, I would be glad to hear your comments on the "user interface" of the edit command loop. I haven't found a good way of saying "an optional number followed by y or n" that is consistent with the "[y/n]" format used in the rest of the patch loop. Similiarly, an option to undo an edit might be nice, but would complicate the code a fair bit.

Best regards, Thomas

 Documentation/git-add.txt |    1 +
 git-add--interactive.perl |  110 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 111 insertions(+), 0 deletions(-)
diff --git a/Documentation/git-add.txt b/Documentation/git-add.txt
index bb4abe2..8de4d4a 100644
--- a/Documentation/git-add.txt
+++ b/Documentation/git-add.txt
@@ -229,6 +229,7 @@ patch::
        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
 +
 After deciding the fate for all hunks, if there is any hunk
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 903953e..8af841a 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -18,6 +18,10 @@ my ($fraginfo_color) =
 	$diff_use_color ? (
 		$repo->get_color('color.diff.frag', 'cyan'),
 	) : ();
+my ($diff_plain_color) =
+	$diff_use_color ? (
+		$repo->get_color('color.diff.plain', ''),
+	) : ();
 
 my $normal_color = $repo->get_color("", "reset");
 
@@ -682,6 +686,107 @@ sub split_hunk {
 	return @split;
 }
 
+sub edit_hunk_manually {
+	my ($in_text, $in_display) = @_;
+
+	my @text = @$in_text;
+	my @display = @$in_display;
+
+	my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text[0]);
+	my $num = scalar @text;
+
+	my $ix = 0;
+
+      OUTER:
+	while (1) {
+		$text[0] = ("@@ -$o_ofs" .
+			    (($o_cnt != 1) ? ",$o_cnt" : '') .
+			    " +$n_ofs" .
+			    (($n_cnt != 1) ? ",$n_cnt" : '') .
+			    " @@\n");
+		if ($diff_use_color) {
+			$display[0] = colored $fraginfo_color, $text[0];
+		}
+		else {
+			$display[0] = $text[0];
+		}
+
+		while ($text[$ix] !~ /^[+-]/) {
+			$ix++;
+			last OUTER if $ix >= $num;
+		}
+		my $lineno = 0;
+		for (my $i = 0; $i < $num; $i++) {
+			if ($i >= $ix && $text[$i] =~ /^[+-]/) {
+				$lineno++;
+				if ($lineno == 1) {
+					print $normal_color . colored($prompt_color, ">1 ");
+				}
+				elsif ($lineno <  100) {
+					print $normal_color . colored($prompt_color, sprintf("%2d ", $lineno));
+				}
+				else {
+					print "   ";
+				}
+			}
+			else {
+				print "   ";
+			}
+			print $display[$i];
+		}
+		print colored $prompt_color, "Use line(s) [<num>y/n]? ";
+		my $line = <STDIN>;
+		if ($line) {
+			if ($line =~ /^(\d*)y/) {
+				my $repeat = $1 || 1;
+				while (1) {
+					last if ($repeat <= 0 || $ix >= $num);
+					$repeat-- if ($text[$ix] =~ /^[+-]/);
+					$ix++;
+				}
+			}
+			elsif ($line =~ /^(\d*)n/) {
+				# This is the interesting case.
+				# - lines become context, + lines are dropped
+				my $repeat = $1 || 1;
+				while (1) {
+					last if ($repeat <= 0 || $ix >= $num);
+					if ($text[$ix] =~ /^\+/) {
+						$repeat--;
+						splice(@text, $ix, 1);
+						splice(@display, $ix, 1);
+						$n_cnt--;
+						$num--;
+						$ix--;
+					}
+					elsif ($text[$ix] =~ /^-/) {
+						$repeat--;
+						$n_cnt++;
+						$text[$ix] =~ s/^-/ /;
+						# need a better way to do this
+						$display[$ix] = $normal_color . colored $diff_plain_color, $text[$ix];
+					}
+					$ix++;
+				}
+			}
+		}
+	}
+
+	while (1) {
+		for (@display) {
+			print;
+		}
+		print colored $prompt_color, "Accept and replace old hunk [y/n]? ";
+		my $line = <STDIN>;
+		if ($line =~ /^y/) {
+			return (\@text, \@display);
+		}
+		elsif ($line =~ /^n/) {
+			return ($in_text, $in_display);
+		}
+	}
+}
+
 sub find_last_o_ctx {
 	my ($it) = @_;
 	my $text = $it->{TEXT};
@@ -781,6 +886,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 +991,7 @@ sub patch_update_file {
 		if (hunk_splittable($hunk[$ix]{TEXT})) {
 			$other .= '/s';
 		}
+		$other .= '/e';
 		for (@{$hunk[$ix]{DISPLAY}}) {
 			print;
 		}
@@ -949,6 +1056,9 @@ sub patch_update_file {
 				$num = scalar @hunk;
 				next;
 			}
+			elsif ($line =~ /^e/) {
+				($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY}) = edit_hunk_manually($hunk[$ix]{TEXT}, $hunk[$ix]{DISPLAY});
+			}
 			else {
 				help_patch_cmd($other);
 				next;
-- 
1.5.4.5
Next: Ping Yin
Message 1 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.