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

[PATCH v2 08/16] line-range: teach -L/RE/ to search relative to anchor point

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 6, 2013, 13:59 UTC
Message-ID
<1375797589-65308-9-git-send-email-sunshine@sunshineco.com>
In-Reply-To
<1375797589-65308-1-git-send-email-sunshine@sunshineco.com>

Range specification -L/RE/ for blame/log unconditionally begins searching at line one. Mailing list discussion [1] suggests that, in the presence of multiple -L options, -L/RE/ should search relative to the endpoint of the previous -L range, if any.

Teach the parsing machinery underlying blame's and log's -L options to accept a start point for -L/RE/ searches. Follow-up patches will upgrade blame and log to take advantage of this ability.

[1]: http://thread.gmane.org/gmane.comp.version-control.git/229755/focus=229966
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 builtin/blame.c |  2 +-
 line-log.c      |  2 +-
 line-range.c    | 30 ++++++++++++++++++++++++++----
 line-range.h    |  5 ++++-
 4 files changed, 32 insertions(+), 7 deletions(-)
diff --git a/builtin/blame.c b/builtin/blame.c
index 2f4d9e2..7b084d8 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2479,7 +2479,7 @@ parse_done:
 	for (range_i = 0; range_i < range_list.nr; ++range_i) {
 		long bottom, top;
 		if (parse_range_arg(range_list.items[range_i].string,
-				    nth_line_cb, &sb, lno,
+				    nth_line_cb, &sb, lno, 1,
 				    &bottom, &top, sb.path))
 			usage(blame_usage);
 		if (lno < top || ((lno || bottom) && lno < bottom))
diff --git a/line-log.c b/line-log.c
index bdadf35..38f827b 100644
--- a/line-log.c
+++ b/line-log.c
@@ -591,7 +591,7 @@ parse_lines(struct commit *commit, const char *prefix, struct string_list *args)
 		cb_data.line_ends = ends;
 
 		if (parse_range_arg(range_part, nth_line, &cb_data,
-				    lines, &begin, &end,
+				    lines, 1, &begin, &end,
 				    full_name))
 			die("malformed -L argument '%s'", range_part);
 		if (lines < end || ((lines || begin) && lines < begin))
diff --git a/line-range.c b/line-range.c
index 69e8d6b..bbf3c0f 100644
--- a/line-range.c
+++ b/line-range.c
@@ -6,6 +6,18 @@
 
 /*
  * Parse one item in the -L option
+ *
+ * 'begin' is applicable only to relative range anchors. Absolute anchors
+ * ignore this value.
+ *
+ * When parsing "-L A,B", parse_loc() is called once for A and once for B.
+ *
+ * When parsing A, 'begin' must be a negative number, the absolute value of
+ * which is the line at which relative start-of-range anchors should be
+ * based. Beginning of file is represented by -1.
+ *
+ * When parsing B, 'begin' must be the positive line number immediately
+ * following the line computed for 'A'.
  */
 static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
 			     void *data, long lines, long begin, long *ret)
@@ -46,6 +58,10 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
 			*ret = num;
 		return term;
 	}
+
+	if (begin < 0)
+		begin = -begin;
+
 	if (spec[0] != '/')
 		return spec;
 
@@ -85,7 +101,8 @@ static const char *parse_loc(const char *spec, nth_line_fn_t nth_line,
 	else {
 		char errbuf[1024];
 		regerror(reg_error, &regexp, errbuf, 1024);
-		die("-L parameter '%s': %s", spec + 1, errbuf);
+		die("-L parameter '%s' starting at line %ld: %s",
+		    spec + 1, begin + 1, errbuf);
 	}
 }
 
@@ -210,11 +227,16 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
 }
 
 int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
-		    void *cb_data, long lines, long *begin, long *end,
-		    const char *path)
+		    void *cb_data, long lines, long anchor,
+		    long *begin, long *end, const char *path)
 {
 	*begin = *end = 0;
 
+	if (anchor < 1)
+		anchor = 1;
+	if (anchor > lines)
+		anchor = lines + 1;
+
 	if (*arg == ':') {
 		arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
 		if (!arg || *arg)
@@ -222,7 +244,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
 		return 0;
 	}
 
-	arg = parse_loc(arg, nth_line_cb, cb_data, lines, 1, begin);
+	arg = parse_loc(arg, nth_line_cb, cb_data, lines, -anchor, begin);
 
 	if (*arg == ',')
 		arg = parse_loc(arg + 1, nth_line_cb, cb_data, lines, *begin + 1, end);
diff --git a/line-range.h b/line-range.h
index ae3d012..83ba3c2 100644
--- a/line-range.h
+++ b/line-range.h
@@ -9,6 +9,9 @@
  * line 'lno' inside the 'cb_data'.  The caller is expected to already
  * have a suitable map at hand to make this a constant-time lookup.
  *
+ * 'anchor' is the 1-based line at which relative range specifications
+ * should be anchored. Absolute ranges are unaffected by this value.
+ *
  * Returns 0 in case of success and -1 if there was an error.  The
  * actual range is stored in *begin and *end.  The counting starts
  * at 1!  In case of error, the caller should show usage message.
@@ -18,7 +21,7 @@ typedef const char *(*nth_line_fn_t)(void *data, long lno);
 
 extern int parse_range_arg(const char *arg,
 			   nth_line_fn_t nth_line_cb,
-			   void *cb_data, long lines,
+			   void *cb_data, long lines, long anchor,
 			   long *begin, long *end,
 			   const char *path);
 
-- 
1.8.4.rc1.409.gbd48715
Previous: Eric SunshineNext: Junio C Hamano
Message 12 of 22 in “blame: accept multiple -L ranges”
  1. 00/16 blame: accept multiple -L rangesEric Sunshine, Aug 6, 2013
  2. 01/16 git-log.txt: place each -L option variation on its own lineEric Sunshine, Aug 6, 2013
  3. 02/16 line-range-format.txt: clarify -L:regex usage formEric Sunshine, Aug 6, 2013
  4. 03/16 range-set: publish API for re-use by git-blame -LEric Sunshine, Aug 6, 2013
  5. 04/16 blame: inline one-line function into its lone callerEric Sunshine, Aug 6, 2013
  6. 05/16 blame: accept multiple -L rangesEric Sunshine, Aug 6, 2013
  7. Junio C HamanoAug 6, 2013
  8. Eric SunshineAug 6, 2013
  9. Junio C HamanoAug 6, 2013
  10. 06/16 t8001/t8002: blame: add tests of multiple -L optionsEric Sunshine, Aug 6, 2013
  11. 07/16 blame: document multiple -L supportEric Sunshine, Aug 6, 2013
  12. 08/16 line-range: teach -L/RE/ to search relative to anchor pointEric Sunshine, Aug 6, 2013
  13. Junio C HamanoAug 6, 2013
  14. 09/16 blame: teach -L/RE/ to search from end of previous -L rangeEric Sunshine, Aug 6, 2013
  15. Junio C HamanoAug 6, 2013
  16. 10/16 log: teach -L/RE/ to search from end of previous -L rangeEric Sunshine, Aug 6, 2013
  17. 11/16 line-range-format.txt: document -L/RE/ relative searchEric Sunshine, Aug 6, 2013
  18. 12/16 line-range: teach -L^/RE/ to search from start of fileEric Sunshine, Aug 6, 2013
  19. 13/16 line-range: teach -L:RE to search from end of previous -L rangeEric Sunshine, Aug 6, 2013
  20. 14/16 line-range: teach -L^:RE to search from start of fileEric Sunshine, Aug 6, 2013
  21. 15/16 t8001/t8002: blame: add tests of -L line numbers less than 1Eric Sunshine, Aug 6, 2013
  22. 16/16 line-range: reject -L line numbers less than 1Eric Sunshine, Aug 6, 2013

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.