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

Re: summaries in git add --patch[PATCH 1/2]

From
William Pursell <bill.pursell@gmail.com>
Date
Dec 4, 2008, 06:55 UTC
Message-ID
<49377ED8.4050905@gmail.com>
In-Reply-To
<7vd4g8alp7.fsf@gitster.siamese.dyndns.org>
Junio C Hamano wrote:
> Do you really want the surrounding @@ in the result, by the way?

Oddly, I liked it before. But now that you mention it, it does seem ugly.

> How well does substr() work with utf-8 and other multi-byte encodings
> these days, I have to wonder...
Hopefully, it works well.
Here's another go, with your suggestions applied.
 From 92ab9b7c694ba98b43984bbbdfcd5eeb9cbb7d56 Mon Sep 17 00:00:00 2001
From: William Pursell <bill.pursell@gmail.com>
Date: Thu, 4 Dec 2008 06:09:50 +0000
Subject: [PATCH 1/2] Add subroutine to display one-line summary of hunks.

This commit implements a rather simple-minded mechanism to display a one-line summary of the hunks in an array ref. The display consists of the line numbers and the first changed line, truncated to 80 characters. 20 lines are displayed at a time, and the index of the first undisplayed line is returned, allowing the caller to display more if desired. (The 20 and 80 should be made configurable.)

Signed-off-by: William Pursell <bill.pursell@gmail.com>
---
  git-add--interactive.perl |   42 ++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index b0223c3..b25a841 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -836,6 +836,48 @@ sub patch_update_cmd {
  	}
  }

+# Generate a one line summary of a hunk.
+sub summarize_hunk {
+	my $rhunk = shift;
+	my $summary = $rhunk->{TEXT}[0];
+
+	# Keep the line numbers, discard extra context.
+	$summary =~ s/@@(.*?)@@.*/$1 /s;
+	$summary .= " " x (20 - length $summary);
+
+	# Add some user context, the first changed line that contains
+	# some non-white character other than a bracket.
+	for my $line (@{$rhunk->{TEXT}}) {
+		if ($line =~ m/^([+-][][{}()\s]*[^][{}()\s])/) {
+			$summary .= $line;
+			last;
+		}
+	}
+
+	chomp $summary;
+	return substr($summary, 0, 80) . "\n";
+}
+
+
+# Print a one-line summary of each hunk in the array ref in
+# the first argument, starting wih the index in the 2nd.
+sub display_hunks {
+	my ($hunks, $i) = @_;
+	my $ctr = 0;
+	$i = 0 if not $i;
+	for (; $i < @$hunks && $ctr < 20; $i++, $ctr++) {
+		my $status = " ";
+		if (defined $hunks->[$i]{USE}) {
+			$status = $hunks->[$i]{USE} ? "+" : "-";
+		}
+		printf "%s%2d: %s",
+			$status,
+			$i + 1,
+			summarize_hunk($hunks->[$i]);
+	}
+	return $i;
+}
+
  sub patch_update_file {
  	my ($ix, $num);
  	my $path = shift;
-- 
1.6.1.rc1.37.g83daf.dirty



-- 
William Pursell
Previous: Junio C HamanoNext: Junio C Hamano
Message 11 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.