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

Re: [PATCH v3 2/2] git_path(): handle `.lock` files correctly

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Oct 28, 2019, 12:32 UTC
Message-ID
<20191028123214.GT4348@szeder.dev>
In-Reply-To
<nycvar.QRO.7.76.6.1910281225260.46@tvgsbejvaqbjf.bet>
On Mon, Oct 28, 2019 at 01:01:49PM +0100, Johannes Schindelin wrote:
Show 23 quoted lines
> > > @@ -349,10 +349,16 @@ static int check_common(const char *unmatched, void *value, void *baton)
> > >  static void update_common_dir(struct strbuf *buf, int git_dir_len,
> > >  			      const char *common_dir)
> > >  {
> > > -	char *base = buf->buf + git_dir_len;
> > > +	char *base = buf->buf + git_dir_len, *base2 = NULL;
> > > +
> > > +	if (ends_with(base, ".lock"))
> > > +		base = base2 = xstrndup(base, strlen(base) - 5);
> >
> > Hm, this adds the magic number 5 and calls strlen(base) twice, because
> > ends_with() calls strip_suffix(), which calls strlen().  Calling
> > strip_suffix() directly would allow us to avoid the magic number and
> > the second strlen():
> >
> >   size_t len;
> >   if (strip_suffix(base, ".lock", &len))
> >           base = base2 = xstrndup(base, len);
> 
> Actually, we should probably avoid the extra allocation. Earlier, I was
> concerned about multi-threading issues when attempting to modify the
> `strbuf`. But then, we modify that `strbuf` a couple of lines later
> anyway,

Do we? You're right, we do indeed, because in replace_dir(buf, ...) we call strbuf_splice(buf, ...). OK, then your suggestion below is even better.

Show 13 quoted lines
> so my fears were totally unfounded. Therefore, my current
> version reads like this:
> 
> -- snip --
> 	int has_lock_suffix = strbuf_strip_suffix(buf, LOCK_SUFFIX);
> 
> 	init_common_trie();
> 	if (trie_find(&common_trie, base, check_common, NULL) > 0)
> 		replace_dir(buf, git_dir_len, common_dir);
> 
> 	if (has_lock_suffix)
> 		strbuf_addstr(buf, LOCK_SUFFIX);
> -- snap --
Previous: Johannes SchindelinNext: Junio C Hamano
Message 26 of 31 in “Handle git_path() with lock files correctly in worktrees”
  1. 0/2 Handle git_path() with lock files correctly in worktreesJohannes Schindelin via GitGitGadget, Oct 16, 2019
  2. 1/2 t1400: wrap setup code in test caseJohannes Schindelin via GitGitGadget, Oct 16, 2019
  3. 2/2 git_path(): handle `.lock` files correctlyJohannes Schindelin via GitGitGadget, Oct 16, 2019
  4. SZEDER GáborOct 16, 2019
  5. Junio C HamanoOct 17, 2019
  6. Johannes SchindelinOct 17, 2019
  7. SZEDER GáborOct 18, 2019
  8. SZEDER GáborOct 18, 2019
  9. 0/2 path.c: minor common_list fixesSZEDER Gábor, Oct 18, 2019
  10. 1/2 path.c: fix field name in 'struct common_dir'SZEDER Gábor, Oct 18, 2019
  11. 2/2 path.c: mark 'logs/HEAD' in 'common_list' as fileSZEDER Gábor, Oct 18, 2019
  12. Johannes SchindelinOct 21, 2019
  13. 0/2 Handle git_path() with lock files correctly in worktreesJohannes Schindelin via GitGitGadget, Oct 17, 2019
  14. 1/2 t1400: wrap setup code in test caseJohannes Schindelin via GitGitGadget, Oct 17, 2019
  15. 2/2 git_path(): handle `.lock` files correctlyJohannes Schindelin via GitGitGadget, Oct 17, 2019
  16. Junio C HamanoOct 18, 2019
  17. SZEDER GáborOct 18, 2019
  18. Johannes SchindelinOct 21, 2019
  19. Junio C HamanoOct 23, 2019
  20. 0/2 Handle git_path() with lock files correctly in worktreesJohannes Schindelin via GitGitGadget, Oct 21, 2019
  21. 1/2 t1400: wrap setup code in test caseJohannes Schindelin via GitGitGadget, Oct 21, 2019
  22. 2/2 git_path(): handle `.lock` files correctlyJohannes Schindelin via GitGitGadget, Oct 21, 2019
  23. SZEDER GáborOct 22, 2019
  24. Junio C HamanoOct 23, 2019
  25. Johannes SchindelinOct 28, 2019
  26. SZEDER GáborOct 28, 2019
  27. Junio C HamanoOct 28, 2019
  28. 0/2 Handle git_path() with lock files correctly in worktreesJohannes Schindelin via GitGitGadget, Oct 28, 2019
  29. 1/2 t1400: wrap setup code in test caseJohannes Schindelin via GitGitGadget, Oct 28, 2019
  30. 2/2 git_path(): handle `.lock` files correctlyJohannes Schindelin via GitGitGadget, Oct 28, 2019
  31. Junio C HamanoOct 29, 2019

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.