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

[PATCH v2] blame: add a range option to -L

From
BPBill Pemberton <wfp5p@virginia.edu>
Date
May 3, 2010, 18:06 UTC
Message-ID
<1272909995-3198-1-git-send-email-wfp5p@virginia.edu>

In addition to <start>,<end> you can now use <center>%<radius> to specify how many lines around <center> that you want to see. For example: -L 20%5 would show lines 15 through 25

Signed-off-by: Bill Pemberton <wfp5p@virginia.edu>
---

This is like the previous patch to create a range option to -L in git-blame. However, this one uses -L<start>%<end>

I chose to use % since it's on a standard keyboard.
 Documentation/blame-options.txt |    4 ++++
 Documentation/git-blame.txt     |    8 ++++++++
 builtin/blame.c                 |   18 +++++++++++++++---
 3 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/Documentation/blame-options.txt b/Documentation/blame-options.txt
index d820569..f65e69c 100644
--- a/Documentation/blame-options.txt
+++ b/Documentation/blame-options.txt
@@ -32,6 +32,10 @@ This is only valid for <end> and will specify a number
 of lines before or after the line given by <start>.
 +
 
+-L <center>%<radius>::
+	This works like <start>,<end> with the annotated range
+	centered on <center> and showing <radius> lines around it.
+
 -l::
 	Show long rev (Default: off).
 
diff --git a/Documentation/git-blame.txt b/Documentation/git-blame.txt
index a27f439..73f6b83 100644
--- a/Documentation/git-blame.txt
+++ b/Documentation/git-blame.txt
@@ -110,6 +110,14 @@ line 40):
 	git blame -L 40,60 foo
 	git blame -L 40,+21 foo
 
+A range of lines around a particular line can be shown by using '%'
+instead of ','.  If you wanted to see line 20 along with the 5
+lines around it:
+
+       git blame -L 20%5 foo
+
+
+
 Also you can use a regular expression to specify the line range:
 
 	git blame -L '/^sub hello {/,/^}$/' foo
diff --git a/builtin/blame.c b/builtin/blame.c
index fc15863..eabc292 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1887,6 +1887,9 @@ static const char *parse_loc(const char *spec,
 	/* Allow "-L <something>,+20" to mean starting at <something>
 	 * for 20 lines, or "-L <something>,-5" for 5 lines ending at
 	 * <something>.
+	 *
+	 * In addition "-L <something>%5" means starting at
+	 * <something>-5 and ending at <something>+5
 	 */
 	if (1 < begin && (spec[0] == '+' || spec[0] == '-')) {
 		num = strtol(spec + 1, &term, 10);
@@ -1958,10 +1961,19 @@ static void prepare_blame_range(struct scoreboard *sb,
 	const char *term;
 
 	term = parse_loc(bottomtop, sb, lno, 1, bottom);
-	if (*term == ',') {
+	if (*term == ',')
+		term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
+	else if (*term == '%') {
+		long x;
+		/* ignore + or - if it's there */
+		if ((*(term+1) == '+') || (*(term+1) == '-'))
+			term++;
 		term = parse_loc(term + 1, sb, lno, *bottom + 1, top);
-		if (*term)
-			usage(blame_usage);
+		x = *top;
+		*top = *bottom - x;
+		*bottom += x;
+		if (*bottom < 1)
+			*bottom = 1;
 	}
 	if (*term)
 		usage(blame_usage);
-- 
1.7.1
Next: Michael Witten
Message 1 of 5 in “blame: add a range option to -L”
  1. blame: add a range option to -LBill Pemberton, May 3, 2010
  2. Michael WittenMay 3, 2010
  3. Junio C HamanoMay 4, 2010
  4. Jakub NarebskiMay 4, 2010
  5. Matthieu MoyMay 4, 2010

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.