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

Re: What's cooking in git.git (Apr 2017, #04; Wed, 19)

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 20, 2017, 22:51 UTC
Message-ID
<xmqqo9vq8zhn.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20170420153512.kc57z7cyxdzq74sg@sigill.intra.peff.net>
Jeff King <peff@peff.net> writes:
Show 36 quoted lines
>>  	if (!ret && opts->keep_locked)
>> -		;
>> +		;	      /* --lock wants to keep "locked" file */
>>  	else
>>  		unlink_or_warn(sb.buf);
>
> I know this is just a drive-by comment, but given that the "else" is the
> only thing that does anything, would it be simpler to flip the
> conditional? The strbuf manipulation above also could go inside it.
> E.g.:
>
> diff --git a/builtin/worktree.c b/builtin/worktree.c
> index 5ebdcce79..05ade547c 100644
> --- a/builtin/worktree.c
> +++ b/builtin/worktree.c
> @@ -307,12 +307,11 @@ static int add_worktree(const char *path, const char *refname,
>  	junk_git_dir = NULL;
>  
>  done:
> -	strbuf_reset(&sb);
> -	strbuf_addf(&sb, "%s/locked", sb_repo.buf);
> -	if (!ret && opts->keep_locked)
> -		;
> -	else
> +	if (ret || !opts->keep_locked) {
> +		strbuf_reset(&sb);
> +		strbuf_addf(&sb, "%s/locked", sb_repo.buf);
>  		unlink_or_warn(sb.buf);
> +	}
>  	argv_array_clear(&child_env);
>  	strbuf_release(&sb);
>  	strbuf_release(&symref);
>
> (Since it's in its own block I'd also consider just introducing a new
> variable for the path, but I guess reusing "sb" for each path is already
> a pattern in the function).

Yeah, when I looked at Duy's version and thought about changing to lose the empty statement myself, I was afraid that the "if" condition might become less transparent to grasp, but now I see what you actually written down, I think "if we got an error, or the caller didn't ask us to, remove that file" is easy enough to understand.

Thanks.
Previous: Jeff KingNext: Junio C Hamano
Message 4 of 29 in “What's cooking in git.git (Apr 2017, #04; Wed, 19)”
  1. Junio C HamanoApr 20, 2017
  2. Duy NguyenApr 20, 2017
  3. Jeff KingApr 20, 2017
  4. Junio C HamanoApr 20, 2017
  5. Junio C HamanoApr 20, 2017
  6. Lars SchneiderApr 20, 2017
  7. Junio C HamanoApr 20, 2017
  8. Johannes SchindelinApr 20, 2017
  9. Brandon WilliamsApr 20, 2017
  10. Brandon WilliamsApr 20, 2017
  11. Junio C HamanoApr 21, 2017
  12. Junio C HamanoApr 20, 2017
  13. Johannes SchindelinApr 21, 2017
  14. Christian CouderApr 21, 2017
  15. Johannes SchindelinApr 22, 2017
  16. Christian CouderApr 22, 2017
  17. Johannes SchindelinApr 24, 2017
  18. Johannes SixtApr 22, 2017
  19. Johannes SchindelinApr 24, 2017
  20. Philip OakleyApr 24, 2017
  21. Christian CouderApr 25, 2017
  22. Christian CouderApr 25, 2017
  23. Johannes SixtApr 25, 2017
  24. Junio C HamanoApr 25, 2017
  25. Johannes SixtApr 25, 2017
  26. Junio C HamanoApr 24, 2017
  27. Johannes SchindelinApr 24, 2017
  28. Ævar Arnfjörð BjarmasonApr 24, 2017
  29. Junio C HamanoApr 25, 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.