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

Re: summaries in git add --patch

From
William Pursell <bill.pursell@gmail.com>
Date
Nov 28, 2008, 06:42 UTC
Message-ID
<492F92C9.7030301@gmail.com>
In-Reply-To
<7viqq8adsf.fsf@gitster.siamese.dyndns.org>

Here's a new patch. Instead of displaying the summary and then the current hunk, it implements a 'goto' command. It prints the summary and then prompts for the index of the hunk to jump to. By not printing the current hunk, the list should typically stay on screen. Also, the summary is optional, so:

g -- bring up summary and prompt for index g3 -- jump to hunk 3

commit 510edf7c28fcc571f29106e32f2570d5f2e04fc3
Author: William Pursell <bill.pursell@gmail.com>
Date:   Fri Nov 28 06:22:36 2008 +0000
     Implement 'g' command (goto) in add --patch
     This command prints a summary of the hunks in the current
     file and prompts the user for an index of the hunk to make
     current.
     Signed-off-by: William Pursell <bill.pursell@gmail.com>
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index b0223c3..e6d73a0 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -553,7 +553,7 @@ sub parse_diff {

  	for (my $i = 0; $i < @diff; $i++) {
  		if ($diff[$i] =~ /^@@ /) {
-			push @hunk, { TEXT => [], DISPLAY => [] };
+			push @hunk, { TEXT => [], DISPLAY => [], SUMMARY => $diff[$i] };
  		}
  		push @{$hunk[-1]{TEXT}}, $diff[$i];
  		push @{$hunk[-1]{DISPLAY}},
@@ -685,6 +685,7 @@ sub split_hunk {
  			    (($n_cnt != 1) ? ",$n_cnt" : '') .
  			    " @@\n");
  		my $display_head = $head;
+		$hunk->{SUMMARY} = $head;
  		unshift @{$hunk->{TEXT}}, $head;
  		if ($diff_use_color) {
  			$display_head = colored($fraginfo_color, $head);
@@ -783,6 +784,7 @@ sub edit_hunk_loop {
  				 $newhunk,
  				 @{$hunk}[$ix+1..$#{$hunk}])) {
  			$newhunk->{DISPLAY} = [color_diff(@{$text})];
+			$newhunk->{SUMMARY} = $$text[0];
  			return $newhunk;
  		}
  		else {
@@ -799,6 +801,7 @@ sub help_patch_cmd {
  y - stage this hunk
  n - do not stage this hunk
  a - stage this and all the remaining hunks in the file
+g - select a hunk to jump to
  d - do not stage this hunk nor any of the remaining hunks in the file
  j - leave this hunk undecided, see next undecided hunk
  J - leave this hunk undecided, see next hunk
@@ -836,6 +839,27 @@ sub patch_update_cmd {
  	}
  }

+sub select_new_hunk {
+	my $ri = shift;
+	my @hunk = @_;
+	my ($i, $response);
+	print "   '+' stage, '-' don't stage\n";
+	for ( $i = 0; $i < @hunk; $i++ ) {
+		my $status = " ";
+		if( defined $hunk[$i]{USE} ) {
+			$status = $hunk[$i]{USE} ? "+" : "-";
+		}
+		printf "%s%3d: %s",
+			$status,
+			$i + 1,
+			$hunk[$i]{SUMMARY};
+	}
+	printf "goto which hunk? ";
+	$response = <STDIN>;
+	chomp $response;
+	$$ri = $response - 1;
+}
+
  sub patch_update_file {
  	my ($ix, $num);
  	my $path = shift;
@@ -919,7 +943,7 @@ sub patch_update_file {
  		for (@{$hunk[$ix]{DISPLAY}}) {
  			print;
  		}
-		print colored $prompt_color, "Stage this hunk [y/n/a/d$other/?]? ";
+		print colored $prompt_color, "Stage this hunk [y/n/a/d/g$other/?]? ";
  		my $line = <STDIN>;
  		if ($line) {
  			if ($line =~ /^y/i) {
@@ -937,6 +961,16 @@ sub patch_update_file {
  				}
  				next;
  			}
+			elsif ($line =~ /^g/) {
+				chomp ($line);
+				if ($line =~ /^g$/) {
+					select_new_hunk (\$ix, @hunk);
+				}
+				else {
+					$ix = (substr $line, 1) - 1;
+				}
+				next;
+			}
  			elsif ($line =~ /^d/i) {
  				while ($ix < $num) {
  					if (!defined $hunk[$ix]{USE}) {
-- 
William Pursell
Previous: William PursellNext: Junio C Hamano
Message 5 of 19 in “summaries in git add --patch”
  1. William PursellNov 27, 2008
  2. Jakub NarebskiNov 27, 2008
  3. Junio C HamanoNov 28, 2008
  4. William PursellNov 28, 2008
  5. William PursellNov 28, 2008
  6. Junio C HamanoNov 28, 2008
  7. William PursellNov 29, 2008
  8. Junio C HamanoDec 3, 2008
  9. William PursellDec 3, 2008
  10. Junio C HamanoDec 3, 2008
  11. William PursellDec 4, 2008
  12. Junio C HamanoDec 4, 2008
  13. William PursellDec 4, 2008
  14. Junio C HamanoDec 5, 2008
  15. William PursellDec 3, 2008
  16. Junio C HamanoDec 3, 2008
  17. William PursellDec 4, 2008
  18. Junio C HamanoDec 4, 2008
  19. William PursellDec 4, 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.