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

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

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Oct 21, 2019, 20:26 UTC
Message-ID
<nycvar.QRO.7.76.6.1910212157350.46@tvgsbejvaqbjf.bet>
In-Reply-To
<xmqq1rva7sp7.fsf@gitster-ct.c.googlers.com>
Hi Junio,
On Fri, 18 Oct 2019, Junio C Hamano wrote:
Show 15 quoted lines
> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > Ever since worktrees were introduced, the `git_path()` function _really_
> > needed to be called e.g. to get at the path to `logs/HEAD` (`HEAD` is
> > specific to the worktree). However, the wrong path is returned for
> > `logs/HEAD.lock`.
> >
> > This does not matter as long as the Git executable is doing the asking,
> > as the path for that `index.lock` file is constructed from
> > `git_path("index")` by appending the `.lock` suffix.
>
> Is this still git_path("index") or is it now HEAD?
Darn. It is "logs/HEAD", of course.
> > Side note: Git GUI _does_ ask for `index.lock`, but that is already
> > resolved correctly.
>
> Is that s/but/and/?
It sounds better with an "and", you're right.
Show 18 quoted lines
> > diff --git a/path.c b/path.c
> > index e3da1f3c4e..ff85692b45 100644
> > --- a/path.c
> > +++ b/path.c
> > @@ -268,7 +268,7 @@ static int trie_find(struct trie *root, const char *key, match_fn fn,
> >  	int result;
> >  	struct trie *child;
> >
> > -	if (!*key) {
> > +	if (!*key || !strcmp(key, ".lock")) {
>
> We only do strcmp for the tail part at the end of the path, so this
> should probably OK from performance point of view but semantically
> it is not very satisfying to see a special case for a single .suffix
> this deep in the callchain.  I wonder if it is nicer to have the
> higher level callers notice ".lock" or whatever other suffixes they
> care about and ask the lower layer for a key with the suffix
> stripped?

Hmm. The parameter is provided as a `const char *`, so I cannot just edit it. My first attempt at fixing this resulted in this commit:

-- snip --
    path: switch `trie_find()` to a counted string
    Before this patch, the `trie_find()` function would be fed a regular,
    NUL-terminated string.
    However, in the next commit, we want to strip any `.lock` suffix from
    the argument, and the argument is provided as a `const char *` (i.e. we
    do not want to modify it).
    Let's use a string + length pair of parameters instead of a single
    NUL-terminated string to open the door for this kind of manipulation.
    Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
diff --git a/path.c b/path.c
index e3da1f3c4e2..b18d552c890 100644
--- a/path.c
+++ b/path.c
@@ -236,7 +236,8 @@ static void *add_to_trie(struct trie *root, const char *key, void *value)
 	return old;
 }

-typedef int (*match_fn)(const char *unmatched, void *data, void *baton);
+typedef int (*match_fn)(const char *unmatched, size_t unmatched_len,
+			void *data, void *baton);

 /*
  * Search a trie for some key.  Find the longest /-or-\0-terminated
@@ -261,22 +262,22 @@ typedef int (*match_fn)(const char *unmatched, void *data, void *baton);
  * |-----------------|------------|---------------|--------------|
  *
  */
-static int trie_find(struct trie *root, const char *key, match_fn fn,
-		     void *baton)
+static int trie_find(struct trie *root, const char *key, size_t key_len,
+		     match_fn fn, void *baton)
 {
 	int i;
 	int result;
 	struct trie *child;

-	if (!*key) {
+	if (!key_len) {
 		/* we have reached the end of the key */
 		if (root->value && !root->len)
-			return fn(key, root->value, baton);
+			return fn(key, key_len, root->value, baton);
 		else
 			return -1;
 	}

-	for (i = 0; i < root->len; i++) {
+	for (i = 0; i < key_len && i < root->len; i++) {
 		/* Partial path normalization: skip consecutive slashes. */
 		if (key[i] == '/' && key[i+1] == '/') {
 			key++;
@@ -288,24 +289,26 @@ static int trie_find(struct trie *root, const char *key, match_fn fn,

 	/* Matched the entire compressed section */
 	key += i;
-	if (!*key)
+	key_len -= i;
+
+	if (!key_len)
 		/* End of key */
-		return fn(key, root->value, baton);
+		return fn(key, key_len, root->value, baton);

 	/* Partial path normalization: skip consecutive slashes */
-	while (key[0] == '/' && key[1] == '/')
-		key++;
+	while (key_len > 0 && key[0] == '/' && key[1] == '/')
+		key++, key_len--;

-	child = root->children[(unsigned char)*key];
+	child = root->children[!key_len ? '0' : (unsigned char)*key];
 	if (child)
-		result = trie_find(child, key + 1, fn, baton);
+		result = trie_find(child, key + 1, key_len - 1, fn, baton);
 	else
 		result = -1;

-	if (result >= 0 || (*key != '/' && *key != 0))
+	if (result >= 0 || (key_len && *key != '/'))
 		return result;
 	if (root->value)
-		return fn(key, root->value, baton);
+		return fn(key, key_len, root->value, baton);
 	else
 		return -1;
 }
@@ -330,17 +333,18 @@ static void init_common_trie(void)
  * Helper function for update_common_dir: returns 1 if the dir
  * prefix is common.
  */
-static int check_common(const char *unmatched, void *value, void *baton)
+static int check_common(const char *unmatched, size_t unmatched_len,
+			void *value, void *baton)
 {
 	struct common_dir *dir = value;

 	if (!dir)
 		return 0;

-	if (dir->is_dir && (unmatched[0] == 0 || unmatched[0] == '/'))
+	if (dir->is_dir && (!unmatched_len || unmatched[0] == '/'))
 		return !dir->exclude;

-	if (!dir->is_dir && unmatched[0] == 0)
+	if (!dir->is_dir && !unmatched_len)
 		return !dir->exclude;

 	return 0;
@@ -350,8 +354,10 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len,
 			      const char *common_dir)
 {
 	char *base = buf->buf + git_dir_len;
+	size_t len = strlen(base);
+
 	init_common_trie();
-	if (trie_find(&common_trie, base, check_common, NULL) > 0)
+	if (trie_find(&common_trie, base, len, check_common, NULL) > 0)
 		replace_dir(buf, git_dir_len, common_dir);
 }
-- snap --

However, I think this is _really_ ugly and intrusive. The opposite of
what my goals were.

So I think I'll just bite the bullet and use a temporary copy if the
argument ends in `.lock`.

Ciao,
Dscho
Previous: SZEDER GáborNext: Junio C Hamano
Message 18 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.