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

Re: [PATCH] clone: Make use of the strip_suffix() helper method

From
Jeff King <peff@peff.net>
Date
Jul 9, 2015, 17:00 UTC
Message-ID
<20150709170054.GA15820@peff.net>
In-Reply-To
<0000014e73738297-cce3a38b-a85d-40be-b501-354686c25eee-000000@eu-west-1.amazonses.com>
On Thu, Jul 09, 2015 at 03:33:46PM +0000, Sebastian Schuberth wrote:
Show 12 quoted lines
> @@ -174,19 +175,17 @@ static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
>  	 * Strip .{bundle,git}.
>  	 */
>  	if (is_bundle) {
> -		if (end - start > 7 && !strncmp(end - 7, ".bundle", 7))
> -			end -= 7;
> +		strip_suffix(start, ".bundle", &len);
>  	} else {
> -		if (end - start > 4 && !strncmp(end - 4, ".git", 4))
> -			end -= 4;
> +		strip_suffix(start, ".git", &len);
>  	}

Yay, always glad to see complicated string handling like this go away. As the resulting conditional blocks are one-liners, I think you can drop the curly braces, which will match our usual style:

  if (is_bundle)
	strip_suffix(start, ".bundle", &len);
  else
	strip_suffix(start, ".git", &len);

If you wanted to get really fancy, I think you could put a ternary operator in the middle of the strip_suffix call. That makes it clear that "len" is set in all code paths, but I think some people find ternary operators unreadable. :)

Show 5 quoted lines
>  	if (is_bare) {
>  		struct strbuf result = STRBUF_INIT;
> -		strbuf_addf(&result, "%.*s.git", (int)(end - start), start);
> +		strbuf_addf(&result, "%.*s.git", len, start);
>  		dir = strbuf_detach(&result, NULL);
This one can also be simplified using xstrfmt to:
  if (is_bare)
	dir = xstrfmt("%.*s.git", len, start);

Do we still need to cast "len" to an int to use it with "%.*" (it is defined by the standard as an int, not a size_t)?

-Peff
Previous: Sebastian SchuberthNext: Sebastian Schuberth
Message 2 of 24 in “clone: Make use of the strip_suffix() helper method”
  1. clone: Make use of the strip_suffix() helper methodSebastian Schuberth, Jul 9, 2015
  2. Jeff KingJul 9, 2015
  3. Sebastian SchuberthJul 9, 2015
  4. clone: Simplify string handling in guess_dir_name()Sebastian Schuberth, Jul 9, 2015
  5. Junio C HamanoJul 9, 2015
  6. Sebastian SchuberthJul 9, 2015
  7. clone: Simplify string handling in guess_dir_name()Sebastian Schuberth, Jul 9, 2015
  8. clone: simplify string handling in guess_dir_name()Sebastian Schuberth, Jul 9, 2015
  9. Junio C HamanoJul 9, 2015
  10. Sebastian SchuberthJul 9, 2015
  11. Lukas FleischerAug 4, 2015
  12. Sebastian SchuberthAug 4, 2015
  13. Jeff KingAug 4, 2015
  14. Patrick SteinhardtAug 5, 2015
  15. Jeff KingAug 5, 2015
  16. Patrick SteinhardtAug 5, 2015
  17. Jeff KingAug 5, 2015
  18. 0/2 fix clone guess_dir_name regression in v2.4.8Jeff King, Aug 5, 2015
  19. 1/2 clone: add tests for output directoryJeff King, Aug 5, 2015
  20. 2/2 clone: use computed length in guess_dir_nameJeff King, Aug 5, 2015
  21. Sebastian SchuberthAug 5, 2015
  22. Junio C HamanoAug 5, 2015
  23. Jeff KingAug 5, 2015
  24. Junio C HamanoJul 9, 2015

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.