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

Re: [PATCH 5/7] grep: fix "--" rev/pathspec disambiguation

From
BWBrandon Williams <bmwill@google.com>
Date
Feb 14, 2017, 18:56 UTC
Message-ID
<20170214185621.GC44208@google.com>
In-Reply-To
<20170214060555.yzh6hhi2t7pkeqvi@sigill.intra.peff.net>
On 02/14, Jeff King wrote:
Show 11 quoted lines
> -	/* Check revs and then paths */
> +	/*
> +	 * We have to find "--" in a separate pass, because its presence
> +	 * influences how we will parse arguments that come before it.
> +	 */
> +	for (i = 0; i < argc; i++) {
> +		if (!strcmp(argv[i], "--")) {
> +			seen_dashdash = 1;
> +			break;
> +		}
> +	}

So this simply checks if "--" is an argument that was provided. This then allows grep to know ahead of time how to handle revs/paths preceding a "--" or in the absences of the "--". Seems sensible to me.

Show 36 quoted lines
> +
> +	/*
> +	 * Resolve any rev arguments. If we have a dashdash, then everything up
> +	 * to it must resolve as a rev. If not, then we stop at the first
> +	 * non-rev and assume everything else is a path.
> +	 */
>  	for (i = 0; i < argc; i++) {
>  		const char *arg = argv[i];
>  		unsigned char sha1[20];
> @@ -1158,13 +1173,14 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>  
>  		if (!strcmp(arg, "--")) {
>  			i++;
> -			seen_dashdash = 1;
>  			break;
>  		}
>  
> -		/* Stop at the first non-rev */
> -		if (get_sha1_with_context(arg, 0, sha1, &oc))
> +		if (get_sha1_with_context(arg, 0, sha1, &oc)) {
> +			if (seen_dashdash)
> +				die(_("unable to resolve revision: %s"), arg);
>  			break;
> +		}
>  
>  		object = parse_object_or_die(sha1, arg);
>  		if (!seen_dashdash)
> @@ -1172,7 +1188,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
>  		add_object_array_with_path(object, arg, &list, oc.mode, oc.path);
>  	}
>  
> -	/* The rest are paths */
> +	/*
> +	 * Anything left over is presumed to be a path. But in the non-dashdash
> +	 * "do what I mean" case, we verify and complain when that isn't true.
> +	 */
-- 
Brandon Williams
Previous: Jeff KingNext: Jeff King
Message 11 of 22 in “grep: do not unnecessarily query repo for "--"”
  1. grep: do not unnecessarily query repo for "--"Jonathan Tan, Feb 14, 2017
  2. Jeff KingFeb 14, 2017
  3. 0/7 grep rev/path parsing fixesJeff King, Feb 14, 2017
  4. 1/7 grep: move thread initialization a little lowerJeff King, Feb 14, 2017
  5. Brandon WilliamsFeb 14, 2017
  6. 2/7 grep: do not unnecessarily query repo for "--"Jeff King, Feb 14, 2017
  7. 3/7 t7810: make "--no-index --" test more robustJeff King, Feb 14, 2017
  8. 4/7 grep: re-order rev-parsing loopJeff King, Feb 14, 2017
  9. Brandon WilliamsFeb 14, 2017
  10. 5/7 grep: fix "--" rev/pathspec disambiguationJeff King, Feb 14, 2017
  11. Brandon WilliamsFeb 14, 2017
  12. Jeff KingFeb 14, 2017
  13. Junio C HamanoFeb 14, 2017
  14. 6/7 grep: avoid resolving revision names in --no-index caseJeff King, Feb 14, 2017
  15. Jonathan TanFeb 14, 2017
  16. Jeff KingFeb 14, 2017
  17. Jonathan TanFeb 14, 2017
  18. 8/7 grep: treat revs the same for --untracked as for --no-indexJeff King, Feb 14, 2017
  19. Junio C HamanoFeb 14, 2017
  20. 7/7 grep: do not diagnose misspelt revs with --no-indexJeff King, Feb 14, 2017
  21. Jeff KingFeb 14, 2017
  22. Jonathan TanFeb 14, 2017

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.