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

[PATCH v2 13/16] line-range: teach -L:RE to search from end of previous -L range

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

For consistency with -L/RE/, teach -L:RE to search relative to the end of the previous -L range, if any.

The new behavior invalidates one test in t4211 which assumes that -L:RE begins searching at start of file. This test will be resurrected in a follow-up patch which teaches -L:RE how to override the default relative search behavior.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 Documentation/line-range-format.txt |  3 ++-
 line-range.c                        | 12 +++++++-----
 t/annotate-tests.sh                 | 16 ++++++++++++++++
 t/t4211-line-log.sh                 |  1 -
 4 files changed, 25 insertions(+), 7 deletions(-)
diff --git a/Documentation/line-range-format.txt b/Documentation/line-range-format.txt
index cf84417..469d80b 100644
--- a/Documentation/line-range-format.txt
+++ b/Documentation/line-range-format.txt
@@ -24,4 +24,5 @@ of lines before or after the line given by <start>.
 +
 If ``:<regex>'' is given in place of <start> and <end>, it denotes the range
 from the first funcname line that matches <regex>, up to the next
-funcname line.
+funcname line. ``:<regex>'' searches from the end of the previous `-L` range,
+if any, otherwise from the start of file.
diff --git a/line-range.c b/line-range.c
index 7048489..4bae4bf 100644
--- a/line-range.c
+++ b/line-range.c
@@ -161,7 +161,7 @@ static const char *find_funcname_matching_regexp(xdemitconf_t *xecfg, const char
 }
 
 static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_cb,
-					void *cb_data, long lines, long *begin, long *end,
+					void *cb_data, long lines, long anchor, long *begin, long *end,
 					const char *path)
 {
 	char *pattern;
@@ -187,7 +187,8 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
 
 	pattern = xstrndup(arg+1, term-(arg+1));
 
-	start = nth_line_cb(cb_data, 0);
+	anchor--; /* input is in human terms */
+	start = nth_line_cb(cb_data, anchor);
 
 	drv = userdiff_find_by_path(path);
 	if (drv && drv->funcname.pattern) {
@@ -205,7 +206,8 @@ static const char *parse_range_funcname(const char *arg, nth_line_fn_t nth_line_
 
 	p = find_funcname_matching_regexp(xecfg, (char*) start, &regexp);
 	if (!p)
-		die("-L parameter '%s': no match", pattern);
+		die("-L parameter '%s' starting at line %ld: no match",
+		    pattern, anchor + 1);
 	*begin = 0;
 	while (p > nth_line_cb(cb_data, *begin))
 		(*begin)++;
@@ -244,7 +246,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
 		anchor = lines + 1;
 
 	if (*arg == ':') {
-		arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, begin, end, path);
+		arg = parse_range_funcname(arg, nth_line_cb, cb_data, lines, anchor, begin, end, path);
 		if (!arg || *arg)
 			return -1;
 		return 0;
@@ -269,7 +271,7 @@ int parse_range_arg(const char *arg, nth_line_fn_t nth_line_cb,
 const char *skip_range_arg(const char *arg)
 {
 	if (*arg == ':')
-		return parse_range_funcname(arg, NULL, NULL, 0, NULL, NULL, NULL);
+		return parse_range_funcname(arg, NULL, NULL, 0, 0, NULL, NULL, NULL);
 
 	arg = parse_loc(arg, NULL, NULL, 0, -1, NULL);
 
diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh
index 5a7d7c7..4f7d6ba 100644
--- a/t/annotate-tests.sh
+++ b/t/annotate-tests.sh
@@ -382,6 +382,22 @@ test_expect_success 'blame -L :nomatch' '
 	test_must_fail $PROG -L:nomatch hello.c
 '
 
+test_expect_success 'blame -L :RE (relative)' '
+	check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
+'
+
+test_expect_success 'blame -L :RE (relative: no preceding range)' '
+	check_count -f hello.c -L:ma.. F 4 G 1
+'
+
+test_expect_success 'blame -L :RE (relative: not found)' '
+	test_must_fail $PROG -L3,3 -L:tambourine hello.c
+'
+
+test_expect_success 'blame -L :RE (relative: end-of-file)' '
+	test_must_fail $PROG -L, -L:main hello.c
+'
+
 test_expect_success 'setup incremental' '
 	(
 	GIT_AUTHOR_NAME=I &&
diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh
index b01b3dd..8ba2d51 100755
--- a/t/t4211-line-log.sh
+++ b/t/t4211-line-log.sh
@@ -48,7 +48,6 @@ canned_test "-M -L '/long f/,/^}/:b.c' move-support" move-support-f
 canned_test "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
 
 canned_test "-L 4,12:a.c -L :main:a.c simple" multiple
-canned_test "-L 4,18:a.c -L :main:a.c simple" multiple-overlapping
 canned_test "-L :main:a.c -L 4,18:a.c simple" multiple-overlapping
 canned_test "-L 4:a.c -L 8,12:a.c simple" multiple-superset
 canned_test "-L 8,12:a.c -L 4:a.c simple" multiple-superset
-- 
1.8.4.rc1.409.gbd48715
Previous: Eric SunshineNext: Eric Sunshine
Message 19 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.