git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] refs: unify parse_worktree_ref() and ref_type()
@ 2022-09-12 17:01 Han-Wen Nienhuys via GitGitGadget
  2022-09-12 19:17 ` Junio C Hamano
  2022-09-19 16:34 ` [PATCH v2] " Han-Wen Nienhuys via GitGitGadget
  0 siblings, 2 replies; 9+ messages in thread
From: Han-Wen Nienhuys via GitGitGadget @ 2022-09-12 17:01 UTC (permalink / raw)
  To: git; +Cc: Han-Wen Nienhuys, Han-Wen Nienhuys

From: Han-Wen Nienhuys <hanwen@google.com>

The logic to handle worktree refs (worktrees/NAME/REF and
main-worktree/REF) existed in two places:

* ref_type() in refs.c

* parse_worktree_ref() in worktree.c

Collapse this logic together in one function parse_worktree_ref():
this avoids having to cross-check the result of parse_worktree_ref()
and ref_type().

Introduce enum ref_worktree_type, which is slightly different from
enum ref_type. The latter is a misleading name (one would think that
'ref_type' would have the symref option).

Instead, enum ref_worktree_type only makes explicit how a refname
relates to a worktree. From this point of view, HEAD and
refs/bisect/abc are the same: they specify the current worktree
implicitly.

The files-backend must avoid packing refs/bisect/* and friends into
packed-refs, so expose is_per_worktree_ref() separately.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
---
    refs: unify parse_worktree_ref() and ref_type()
    
    The logic to handle worktree refs (worktrees/NAME/REF and
    main-worktree/REF) existed in two places:
    
     * ref_type() in refs.c
    
     * parse_worktree_ref() in worktree.c
    
    Collapse this logic together in one function parse_worktree_ref(): this
    avoids having to cross-check the result of parse_worktree_ref() and
    ref_type().
    
    Introduce enum ref_worktree_type, which is slightly different from enum
    ref_type. The latter is a misleading name (one would think that
    'ref_type' would have the symref option).
    
    Instead, enum ref_worktree_type only makes explicit how a refname
    relates to a worktree. From this point of view, HEAD and refs/bisect/abc
    are the same: they specify the current worktree implicitly.
    
    The files-backend must avoid packing refs/bisect/* and friends into
    packed-refs, so expose is_per_worktree_ref() separately.
    
    Signed-off-by: Han-Wen Nienhuys hanwen@google.com

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1325%2Fhanwen%2Fparse-worktree-ref-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1325/hanwen/parse-worktree-ref-v1
Pull-Request: https://github.com/git/git/pull/1325

 builtin/reflog.c      |  3 +-
 reflog.c              | 13 ++-----
 refs.c                | 75 ++++++++++++++++++++++++++--------------
 refs.h                | 29 ++++++++++++----
 refs/files-backend.c  | 80 +++++++++++++++++++------------------------
 refs/packed-backend.c |  2 +-
 worktree.c            | 59 ++++---------------------------
 worktree.h            | 10 ------
 8 files changed, 120 insertions(+), 151 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 9407f835cb6..bd568d2d931 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -26,7 +26,8 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
 	 * Avoid collecting the same shared ref multiple times because
 	 * they are available via all worktrees.
 	 */
-	if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
+	if (!worktree->is_current &&
+	    parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
 		return 0;
 
 	strbuf_worktree_ref(worktree, &newref, ref);
diff --git a/reflog.c b/reflog.c
index 47ba8620c56..0b8b767f97c 100644
--- a/reflog.c
+++ b/reflog.c
@@ -310,16 +310,9 @@ static int push_tip_to_list(const char *refname, const struct object_id *oid,
 
 static int is_head(const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_MAIN_PSEUDOREF:
-		if (parse_worktree_ref(refname, NULL, NULL, &refname))
-			BUG("not a worktree ref: %s", refname);
-		break;
-	default:
-		break;
-	}
-	return !strcmp(refname, "HEAD");
+	const char *stripped_refname;
+	parse_worktree_ref(refname, NULL, NULL, &stripped_refname);
+	return !strcmp(stripped_refname, "HEAD");
 }
 
 void reflog_expiry_prepare(const char *refname,
diff --git a/refs.c b/refs.c
index 1a964505f92..45d59a7ede2 100644
--- a/refs.c
+++ b/refs.c
@@ -722,7 +722,7 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **log)
 	return repo_dwim_log(the_repository, str, len, oid, log);
 }
 
-static int is_per_worktree_ref(const char *refname)
+int is_per_worktree_ref(const char *refname)
 {
 	return starts_with(refname, "refs/worktree/") ||
 	       starts_with(refname, "refs/bisect/") ||
@@ -738,37 +738,60 @@ static int is_pseudoref_syntax(const char *refname)
 			return 0;
 	}
 
+	/* HEAD is not a pseudoref, but it certainly uses the
+	 * pseudoref syntax. */
 	return 1;
 }
 
-static int is_main_pseudoref_syntax(const char *refname)
+enum ref_worktree_type parse_worktree_ref(const char *worktree_ref,
+					  const char **name, int *name_length,
+					  const char **ref)
 {
-	return skip_prefix(refname, "main-worktree/", &refname) &&
-		*refname &&
-		is_pseudoref_syntax(refname);
-}
+	const char *name_dummy;
+	int name_length_dummy;
+	const char *ref_dummy;
+	if (!name)
+		name = &name_dummy;
+	if (!name_length)
+		name_length = &name_length_dummy;
+	if (!ref)
+		ref = &ref_dummy;
 
-static int is_other_pseudoref_syntax(const char *refname)
-{
-	if (!skip_prefix(refname, "worktrees/", &refname))
-		return 0;
-	refname = strchr(refname, '/');
-	if (!refname || !refname[1])
-		return 0;
-	return is_pseudoref_syntax(refname + 1);
-}
+	*ref = worktree_ref;
+	if (is_pseudoref_syntax(worktree_ref)) {
+		return REF_WORKTREE_CURRENT;
+	}
 
-enum ref_type ref_type(const char *refname)
-{
-	if (is_per_worktree_ref(refname))
-		return REF_TYPE_PER_WORKTREE;
-	if (is_pseudoref_syntax(refname))
-		return REF_TYPE_PSEUDOREF;
-	if (is_main_pseudoref_syntax(refname))
-		return REF_TYPE_MAIN_PSEUDOREF;
-	if (is_other_pseudoref_syntax(refname))
-		return REF_TYPE_OTHER_PSEUDOREF;
-	return REF_TYPE_NORMAL;
+	if (is_per_worktree_ref(worktree_ref)) {
+		return REF_WORKTREE_CURRENT;
+	}
+
+	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
+		if (!*worktree_ref)
+			return -1;
+		*name = NULL;
+		*name_length = 0;
+		*ref = worktree_ref;
+
+		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
+		    REF_WORKTREE_CURRENT)
+			return REF_WORKTREE_MAIN;
+	}
+	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
+		const char *slash = strchr(worktree_ref, '/');
+
+		if (!slash || slash == worktree_ref || !slash[1])
+			return -1;
+		*name = worktree_ref;
+		*name_length = slash - worktree_ref;
+		*ref = slash + 1;
+
+		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
+		    REF_WORKTREE_CURRENT)
+			return REF_WORKTREE_OTHER;
+	}
+
+	return REF_WORKTREE_SHARED;
 }
 
 long get_files_ref_lock_timeout_ms(void)
diff --git a/refs.h b/refs.h
index 23479c7ee09..9e40efc4787 100644
--- a/refs.h
+++ b/refs.h
@@ -825,15 +825,30 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);
  */
 int ref_is_hidden(const char *, const char *);
 
-enum ref_type {
-	REF_TYPE_PER_WORKTREE,	  /* refs inside refs/ but not shared       */
-	REF_TYPE_PSEUDOREF,	  /* refs outside refs/ in current worktree */
-	REF_TYPE_MAIN_PSEUDOREF,  /* pseudo refs from the main worktree     */
-	REF_TYPE_OTHER_PSEUDOREF, /* pseudo refs from other worktrees       */
-	REF_TYPE_NORMAL,	  /* normal/shared refs inside refs/        */
+/* Is this a per-worktree ref living in the refs/ namespace? */
+int is_per_worktree_ref(const char *refname);
+
+/* Describes how a refname relates to worktrees */
+enum ref_worktree_type {
+	REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
+				 refs/bisect/SOMETHING */
+	REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
+			      refs/main-worktree/HEAD */
+	REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
+			       refs/worktrees/bla/HEAD */
+	REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
 };
 
-enum ref_type ref_type(const char *refname);
+/* Parse a ref that possibly explicitly refers to a worktree ref
+ * (ie. either REFNAME, main-worktree/REFNAME or
+ * worktree/WORKTREE/REFNAME). If the name references a worktree
+ * implicitly or explicitly, return what kind it was. The
+ * worktree_name, worktree_name_length and refname argument maybe NULL.
+ */
+enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
+					  const char **worktree_name,
+					  int *worktree_name_length,
+					  const char **refname);
 
 enum expire_reflog_flags {
 	EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 95acab78eef..f230704229e 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -138,44 +138,30 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
 	return refs;
 }
 
-static void files_reflog_path_other_worktrees(struct files_ref_store *refs,
-					      struct strbuf *sb,
-					      const char *refname)
-{
-	const char *real_ref;
-	const char *worktree_name;
-	int length;
-
-	if (parse_worktree_ref(refname, &worktree_name, &length, &real_ref))
-		BUG("refname %s is not a other-worktree ref", refname);
-
-	if (worktree_name)
-		strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
-			    length, worktree_name, real_ref);
-	else
-		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir,
-			    real_ref);
-}
-
 static void files_reflog_path(struct files_ref_store *refs,
 			      struct strbuf *sb,
 			      const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PER_WORKTREE:
-	case REF_TYPE_PSEUDOREF:
+	const char *bare_refname;
+	const char *wtname;
+	int wtname_len;
+	enum ref_worktree_type wt_type = parse_worktree_ref(
+		refname, &wtname, &wtname_len, &bare_refname);
+
+	switch (wt_type) {
+	case REF_WORKTREE_CURRENT:
 		strbuf_addf(sb, "%s/logs/%s", refs->base.gitdir, refname);
 		break;
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_MAIN_PSEUDOREF:
-		files_reflog_path_other_worktrees(refs, sb, refname);
+	case REF_WORKTREE_SHARED:
+	case REF_WORKTREE_MAIN:
+		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, bare_refname);
 		break;
-	case REF_TYPE_NORMAL:
-		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
+	case REF_WORKTREE_OTHER:
+		strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
+			    wtname_len, wtname, bare_refname);
 		break;
 	default:
-		BUG("unknown ref type %d of ref %s",
-		    ref_type(refname), refname);
+		BUG("unknown ref type %d of ref %s", wt_type, refname);
 	}
 }
 
@@ -183,22 +169,25 @@ static void files_ref_path(struct files_ref_store *refs,
 			   struct strbuf *sb,
 			   const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PER_WORKTREE:
-	case REF_TYPE_PSEUDOREF:
+	const char *bare_refname;
+	const char *wtname;
+	int wtname_len;
+	enum ref_worktree_type wt_type = parse_worktree_ref(
+		refname, &wtname, &wtname_len, &bare_refname);
+	switch (wt_type) {
+	case REF_WORKTREE_CURRENT:
 		strbuf_addf(sb, "%s/%s", refs->base.gitdir, refname);
 		break;
-	case REF_TYPE_MAIN_PSEUDOREF:
-		if (!skip_prefix(refname, "main-worktree/", &refname))
-			BUG("ref %s is not a main pseudoref", refname);
-		/* fallthrough */
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_NORMAL:
-		strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
+	case REF_WORKTREE_OTHER:
+		strbuf_addf(sb, "%s/worktrees/%.*s/%s", refs->gitcommondir,
+			    wtname_len, wtname, bare_refname);
+		break;
+	case REF_WORKTREE_SHARED:
+	case REF_WORKTREE_MAIN:
+		strbuf_addf(sb, "%s/%s", refs->gitcommondir, bare_refname);
 		break;
 	default:
-		BUG("unknown ref type %d of ref %s",
-		    ref_type(refname), refname);
+		BUG("unknown ref type %d of ref %s", wt_type, refname);
 	}
 }
 
@@ -771,7 +760,8 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
 
 	while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 		if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
-		    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
+		    parse_worktree_ref(iter->iter0->refname, NULL, NULL,
+				       NULL) != REF_WORKTREE_CURRENT)
 			continue;
 
 		if ((iter->flags & DO_FOR_EACH_OMIT_DANGLING_SYMREFS) &&
@@ -1179,7 +1169,8 @@ static int should_pack_ref(const char *refname,
 			   unsigned int pack_flags)
 {
 	/* Do not pack per-worktree refs: */
-	if (ref_type(refname) != REF_TYPE_NORMAL)
+	if (parse_worktree_ref(refname, NULL, NULL, NULL) !=
+	    REF_WORKTREE_SHARED)
 		return 0;
 
 	/* Do not pack non-tags unless PACK_REFS_ALL is set: */
@@ -2277,7 +2268,8 @@ static enum iterator_selection reflog_iterator_select(
 		 */
 		return ITER_SELECT_0;
 	} else if (iter_common) {
-		if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)
+		if (parse_worktree_ref(iter_common->refname, NULL, NULL,
+				       NULL) == REF_WORKTREE_SHARED)
 			return ITER_SELECT_1;
 
 		/*
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 66c4574c99d..bf0e63ae70c 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -862,7 +862,7 @@ static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 
 	while ((ok = next_record(iter)) == ITER_OK) {
 		if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
-		    ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE)
+		    !is_per_worktree_ref(iter->base.refname))
 			continue;
 
 		if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
diff --git a/worktree.c b/worktree.c
index 90fc085f76b..bb7873c72d1 100644
--- a/worktree.c
+++ b/worktree.c
@@ -489,62 +489,17 @@ int submodule_uses_worktrees(const char *path)
 	return ret;
 }
 
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref)
-{
-	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
-		if (!*worktree_ref)
-			return -1;
-		if (name)
-			*name = NULL;
-		if (name_length)
-			*name_length = 0;
-		if (ref)
-			*ref = worktree_ref;
-		return 0;
-	}
-	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
-		const char *slash = strchr(worktree_ref, '/');
-
-		if (!slash || slash == worktree_ref || !slash[1])
-			return -1;
-		if (name)
-			*name = worktree_ref;
-		if (name_length)
-			*name_length = slash - worktree_ref;
-		if (ref)
-			*ref = slash + 1;
-		return 0;
-	}
-	return -1;
-}
-
 void strbuf_worktree_ref(const struct worktree *wt,
 			 struct strbuf *sb,
 			 const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PSEUDOREF:
-	case REF_TYPE_PER_WORKTREE:
-		if (wt && !wt->is_current) {
-			if (is_main_worktree(wt))
-				strbuf_addstr(sb, "main-worktree/");
-			else
-				strbuf_addf(sb, "worktrees/%s/", wt->id);
-		}
-		break;
-
-	case REF_TYPE_MAIN_PSEUDOREF:
-	case REF_TYPE_OTHER_PSEUDOREF:
-		break;
-
-	case REF_TYPE_NORMAL:
-		/*
-		 * For shared refs, don't prefix worktrees/ or
-		 * main-worktree/. It's not necessary and
-		 * files-backend.c can't handle it anyway.
-		 */
-		break;
+	if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
+		    REF_WORKTREE_CURRENT &&
+	    wt && !wt->is_current) {
+		if (is_main_worktree(wt))
+			strbuf_addstr(sb, "main-worktree/");
+		else
+			strbuf_addf(sb, "worktrees/%s/", wt->id);
 	}
 	strbuf_addstr(sb, refname);
 }
diff --git a/worktree.h b/worktree.h
index e9e839926b0..9dcea6fc8c1 100644
--- a/worktree.h
+++ b/worktree.h
@@ -166,16 +166,6 @@ const char *worktree_git_path(const struct worktree *wt,
 			      const char *fmt, ...)
 	__attribute__((format (printf, 2, 3)));
 
-/*
- * Parse a worktree ref (i.e. with prefix main-worktree/ or
- * worktrees/) and return the position of the worktree's name and
- * length (or NULL and zero if it's main worktree), and ref.
- *
- * All name, name_length and ref arguments could be NULL.
- */
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref);
-
 /*
  * Return a refname suitable for access from the current ref store.
  */

base-commit: 805e0a68082a217f0112db9ee86a022227a9c81b
-- 
@@ -166,16 +166,6 @@ const char *worktree_git_path(const struct worktree *wt,
 			      const char *fmt, ...)
 	__attribute__((format (printf, 2, 3)));
 
-/*
- * Parse a worktree ref (i.e. with prefix main-worktree/ or
- * worktrees/) and return the position of the worktree's name and
- * length (or NULL and zero if it's main worktree), and ref.
- *
- * All name, name_length and ref arguments could be NULL.
- */
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref);
-
 /*
  * Return a refname suitable for access from the current ref store.
  */

base-commit: 805e0a68082a217f0112db9ee86a022227a9c81b
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-12 17:01 [PATCH] refs: unify parse_worktree_ref() and ref_type() Han-Wen Nienhuys via GitGitGadget
@ 2022-09-12 19:17 ` Junio C Hamano
  2022-09-12 20:13   ` Junio C Hamano
  2022-09-19 16:34 ` [PATCH v2] " Han-Wen Nienhuys via GitGitGadget
  1 sibling, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2022-09-12 19:17 UTC (permalink / raw)
  To: Han-Wen Nienhuys via GitGitGadget; +Cc: git, Han-Wen Nienhuys, Han-Wen Nienhuys

"Han-Wen Nienhuys via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Han-Wen Nienhuys <hanwen@google.com>
>
> The logic to handle worktree refs (worktrees/NAME/REF and
> main-worktree/REF) existed in two places:
>
> * ref_type() in refs.c
>
> * parse_worktree_ref() in worktree.c
>
> Collapse this logic together in one function parse_worktree_ref():
> this avoids having to cross-check the result of parse_worktree_ref()
> and ref_type().
> ...
> The files-backend must avoid packing refs/bisect/* and friends into
> packed-refs, so expose is_per_worktree_ref() separately.

A sensible goal.

> diff --git a/builtin/reflog.c b/builtin/reflog.c
> index 9407f835cb6..bd568d2d931 100644
> --- a/builtin/reflog.c
> +++ b/builtin/reflog.c
> @@ -26,7 +26,8 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
>  	 * Avoid collecting the same shared ref multiple times because
>  	 * they are available via all worktrees.
>  	 */
> -	if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
> +	if (!worktree->is_current &&
> +	    parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
>  		return 0;

We used to say "for a ref without anything magical" but now we say
"for a ref that is not per worktree at all", and they mean the same
thing.  OK.

>  	strbuf_worktree_ref(worktree, &newref, ref);
> diff --git a/reflog.c b/reflog.c
> index 47ba8620c56..0b8b767f97c 100644
> --- a/reflog.c
> +++ b/reflog.c
> @@ -310,16 +310,9 @@ static int push_tip_to_list(const char *refname, const struct object_id *oid,
>  
>  static int is_head(const char *refname)
>  {
> -	switch (ref_type(refname)) {
> -	case REF_TYPE_OTHER_PSEUDOREF:
> -	case REF_TYPE_MAIN_PSEUDOREF:
> -		if (parse_worktree_ref(refname, NULL, NULL, &refname))
> -			BUG("not a worktree ref: %s", refname);
> -		break;
> -	default:
> -		break;
> -	}
> -	return !strcmp(refname, "HEAD");

It used to do some sort of input validation with BUG() but it no
longer does.  Intended?

The new call to parse_worktree_ref() is only to strip the possible
"main-worktree/" and/or "worktrees/$name/" names.

> +	const char *stripped_refname;
> +	parse_worktree_ref(refname, NULL, NULL, &stripped_refname);
> +	return !strcmp(stripped_refname, "HEAD");
>  }


> @@ -738,37 +738,60 @@ static int is_pseudoref_syntax(const char *refname)
>  			return 0;
>  	}
>  
> +	/* HEAD is not a pseudoref, but it certainly uses the
> +	 * pseudoref syntax. */


/*
 * Our multi-line comments have opening slash-aster
 * and closing aster-slash on their own line.
 */


Not your fault but the fault of the original file contents and diff
algorithms; the patch around here is simply unreadble because almost
nothing in the preimage contributes to the result.  So I'll remove
'-' lines first before commenting.

> +enum ref_worktree_type parse_worktree_ref(const char *worktree_ref,
> +					  const char **name, int *name_length,
> +					  const char **ref)

It is hard to see what's input and what's output.  You have comments
in <refs.h> but the prototype uses different parameter names and
they do not tell what is input and what is output, either.

>  {
> +	const char *name_dummy;
> +	int name_length_dummy;
> +	const char *ref_dummy;

Style: Have blank here between the end of decls and the beginning of statements.

> +	if (!name)
> +		name = &name_dummy;
> +	if (!name_length)
> +		name_length = &name_length_dummy;
> +	if (!ref)
> +		ref = &ref_dummy;

I can guess from these that worktree_ref is the input and all three
are to point at variables that receive optional output.  The patch
author shouldn't make the code reader guess.

> +	*ref = worktree_ref;
> +	if (is_pseudoref_syntax(worktree_ref)) {
> +		return REF_WORKTREE_CURRENT;
> +	}

Style: No braces around a single statement block.

OK, given a worktree_ref, FETCH_HEAD, MERGE_HEAD, etc. are all valid
only for the current worktree.

> +	if (is_per_worktree_ref(worktree_ref)) {
> +		return REF_WORKTREE_CURRENT;
> +	}

Also the ones that are within a certain known namespaces, like
"refs/bisect/*".

In the above two cases, name/name_length are undefined.  Presumably
the caller behaves well enough not to peek into them unless it is
told to by the returned value REF_WORKTREE_OTHER?

> +	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {

The special syntax "main-worktree/$their_ref".  We skip the prefix
and then ...

> +		if (!*worktree_ref)
> +			return -1;

Do we know if, or do we have control over, "enum ref_worktree_type"
ends up being signed or unsigned?  I am not sure if the callers in
this patch even pays attention to the error condition.

> +		*name = NULL;
> +		*name_length = 0;
> +		*ref = worktree_ref;
> +
> +		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
> +		    REF_WORKTREE_CURRENT)
> +			return REF_WORKTREE_MAIN;

In "main-worktree/$their_ref", if $their_ref is the per worktree
name, then it is a ref specific to the primary worktree.  

It is not clear what *name and *name_length are trying to return to
the caller here.

Otherwise we fall through with worktree_ref that we have stripped
main-worktree/ prefix, which means the original input

	main-worktree/worktrees/foo/blah

is now 

	worktrees/foo/blah

and the next skip_prefix() will see that it begins with "worktrees/".
Of course, if the initial input were

	worktrees/foo/blah

then we wouldn't have skipped main-worktree/ prefix from it, and go
to the next skip_prefix().  So from here on, we cannot tell which
case the original input was.

But that is OK.  Asking "give me the ref 'blah' in the worktree 'foo'"
in the current worktree should yield the same answer to the question
"give me the ref 'blah' in the worktree 'foo', as if I asked you to
do so in the main worktree".

> +	}
> +	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
> +		const char *slash = strchr(worktree_ref, '/');
> +
> +		if (!slash || slash == worktree_ref || !slash[1])
> +			return -1;
> +		*name = worktree_ref;
> +		*name_length = slash - worktree_ref;
> +		*ref = slash + 1;

OK, name and name_length are giving the name of the worktree to the
caller here.  But this is done even when we do not end up returning
REF_WORKTREE_OTHER, which sounds eh, ugly.

In any case, the part after worktrees/$worktreename/ is a per
worktree name, then we say "that's a per-worktree ref that belongs
to $worktreename" by returning _OTHER and with *name/*name_length.

> +		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
> +		    REF_WORKTREE_CURRENT)
> +			return REF_WORKTREE_OTHER;

> +	}
> +
> +	return REF_WORKTREE_SHARED;

And everything else is _SHARED.

The logic sounds sane enough, but the name/name_length being
sometimes undefined and sometimes cleared feels yucky to me. 

>  }
>  
>  long get_files_ref_lock_timeout_ms(void)
> diff --git a/refs.h b/refs.h
> index 23479c7ee09..9e40efc4787 100644
> --- a/refs.h
> +++ b/refs.h
> @@ -825,15 +825,30 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);
>   */
>  int ref_is_hidden(const char *, const char *);
>  
> +/* Is this a per-worktree ref living in the refs/ namespace? */
> +int is_per_worktree_ref(const char *refname);
> +
> +/* Describes how a refname relates to worktrees */
> +enum ref_worktree_type {
> +	REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
> +				 refs/bisect/SOMETHING */

OK.

> +	REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
> +			      refs/main-worktree/HEAD */
> +	REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
> +			       refs/worktrees/bla/HEAD */

Hmph.  Do we need refs/ prefix before main-worktree/ and
worktrees/bla?  The code we reviewed above seemed to skip
"main-worktree/" and "worktrees/" with the assumption that there
will be no "refs/" prefix before them.  Probably just two typoes,
as the comment before parse_worktree_ref() below also seems to
assume that there is no refs/ before them.

> +	REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
>  };
>  
> -enum ref_type ref_type(const char *refname);
> +/* Parse a ref that possibly explicitly refers to a worktree ref
> + * (ie. either REFNAME, main-worktree/REFNAME or
> + * worktree/WORKTREE/REFNAME). If the name references a worktree
> + * implicitly or explicitly, return what kind it was. The
> + * worktree_name, worktree_name_length and refname argument maybe NULL.
> + */

Here is my attempt to clarify what the comment tries to explain.

	/*
	 * Parse maybe_worktree_ref (input) that possibly explicitly
	 * refers to a worktree ref (i.e. REFNAME, main-worktree/REFNAME,
	 * main-worktree/worktrees/WORKTREE/REFNAME, or
	 * worktrees/WORKTREE/REFNAME).  Return what kind of ref
         * (among the four kinds listed above) it is.
         * 
	 * The location pointed at by worktree_name and worktree_name_length
	 * are modified to point to "WORKTREE" part of such an input string
         * when the returned value is REF_WORKTREE_OTHER.
	 * Otherwise they are undefined (they may still be smudged).
	 *
	 * refname is made to piont to REFNAME part of the input string in
	 * all cases.
	 *
	 * These three output parameters are optional and NULL can
	 * be given for them.
	 */

> +enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
> +					  const char **worktree_name,
> +					  int *worktree_name_length,
> +					  const char **refname);

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-12 19:17 ` Junio C Hamano
@ 2022-09-12 20:13   ` Junio C Hamano
  2022-09-13 15:43     ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2022-09-12 20:13 UTC (permalink / raw)
  To: Han-Wen Nienhuys via GitGitGadget; +Cc: git, Han-Wen Nienhuys, Han-Wen Nienhuys

Junio C Hamano <gitster@pobox.com> writes:

> Otherwise we fall through with worktree_ref that we have stripped
> main-worktree/ prefix, which means the original input
>
> 	main-worktree/worktrees/foo/blah
>
> is now 
>
> 	worktrees/foo/blah
>
> and the next skip_prefix() will see that it begins with "worktrees/".
> Of course, if the initial input were
>
> 	worktrees/foo/blah
>
> then we wouldn't have skipped main-worktree/ prefix from it, and go
> to the next skip_prefix().  So from here on, we cannot tell which
> case the original input was.
>
> But that is OK.  Asking "give me the ref 'blah' in the worktree 'foo'"
> in the current worktree should yield the same answer to the question
> "give me the ref 'blah' in the worktree 'foo', as if I asked you to
> do so in the main worktree".

This makes me wonder...

I wonder if it makes the resulting code clearer to go fully
recursive, unlike the posted code that says "if a recursive call
says it is for current, that means it is for main worktree, and
otherwise pretend as if the input did not have the prefix".

That is, something like

parse_worktree_ref(...) {

	... prepare name, name_len and ref ...

	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
		parsed = parse_worktree_ref(worktree_ref, name, name_len, ref);
        	switch (parsed) {
		case REF_WORKTREE_CURRENT:
                case REF_WORKTREE_MAIN:
			return REF_WORKTREE_MAIN;
		case REF_WORKTREE_OTHER:
		case REF_WORKTREE_SHARED:
			return parsed;
		}
	}

	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
		slash = strchr(worktree_ref, '/');

		parsed = parse_worktree_ref(slash + 1, name, name_len, ref);
        	switch (parsed) {
		case REF_WORKTREE_CURRENT:
			return WORKTREE_OTHER; /* iffy */
                case REF_WORKTREE_MAIN:
			return REF_WORKTREE_MAIN;
		case REF_WORKTREE_OTHER:
		case REF_WORKTREE_SHARED:
			return parsed;
		}
	}

	/* Otherwise the input is like HEAD, MERGE_HEAD, refs/$BLAH */
	... do whatever for these trivial cases ...

}

And while composing this follow-up, I found another thing that is
iffy.  When you ask for "worktrees/foo/refs/bisect/good", the
recursive call in "worktrees/foo" for the remainder says "current",
and the posted patch and the above pseudocode would take CURRENT to
mean "that other worktree's", and turns it into OTHER i.e. "not
ours".  But the code does so without even checking what our worktree
is called.  What happens if we are the owner of the "worktrees/foo"
namespace?  The same thing for "The main worktree says the given ref
is 'current', so we can turn that into 'main'"---if our worktree is
'main', it is also 'current' and does not have to be turned into
'main' (even though it does not hurt).

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-12 20:13   ` Junio C Hamano
@ 2022-09-13 15:43     ` Junio C Hamano
  2022-09-19 14:28       ` Han-Wen Nienhuys
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2022-09-13 15:43 UTC (permalink / raw)
  To: Han-Wen Nienhuys via GitGitGadget; +Cc: git, Han-Wen Nienhuys, Han-Wen Nienhuys

Junio C Hamano <gitster@pobox.com> writes:

> Junio C Hamano <gitster@pobox.com> writes:
>
>> Otherwise we fall through with worktree_ref that we have stripped
>> main-worktree/ prefix, which means the original input
>>
>> 	main-worktree/worktrees/foo/blah
>>
>> is now 
>>
>> 	worktrees/foo/blah
>>
>> and the next skip_prefix() will see that it begins with "worktrees/".
>> Of course, if the initial input were
>>
>> 	worktrees/foo/blah
>>
>> then we wouldn't have skipped main-worktree/ prefix from it, and go
>> to the next skip_prefix().  So from here on, we cannot tell which
>> case the original input was.
>>
>> But that is OK.  Asking "give me the ref 'blah' in the worktree 'foo'"
>> in the current worktree should yield the same answer to the question
>> "give me the ref 'blah' in the worktree 'foo', as if I asked you to
>> do so in the main worktree".
>
> This makes me wonder...
>
> I wonder if it makes the resulting code clearer to go fully
> recursive, unlike the posted code that says "if a recursive call
> says it is for current, that means it is for main worktree, and
> otherwise pretend as if the input did not have the prefix".
>
> That is, something like
> ...

The above may be a wrong suggestion, as it was solely guided by my
reading of the posted code that looked as if it wanted to support
something like "main-worktree/worktrees/foo/refs/heads/main".  If
that wasn't the intention, and we only want to support

  1. a ref spelled in the traditional way before multiple worktree feature,
  2. 1., with "main-worktree/" prefixed, or
  3. 1., with "worktrees/<worktreename>/" prefixed

then a better approach would be to have a small helper
parse_local_worktree_ref() and make the primary one into something
like

parse_worktree_ref()
{
        if (begins with "main-worktree/") {	
		strip main-worktree/ prefix;
		switch (parse_local_worktree_ref(input minus prefix)) {
		... the same switch to turn "ours" into "main's" ...
		... but we probably want to special case if we are ...
		... the main worktree ...
		}
	}
	else if (begins with "worktrees/") {
		strip worktrees/ prefix and learn worktree name;
		switch (parse_local_worktree_ref(input minus prefix)) {
		... the same switch to turn "ours" into "theirs" ...
		... but we probably want to special case if we are ...
		... the worktree in question ...
		}
	}
	return parse_local_worktree_ref(input);
}

and have parse_local_worktree_ref() handle psuedorefs,
per-worktree-refs.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-13 15:43     ` Junio C Hamano
@ 2022-09-19 14:28       ` Han-Wen Nienhuys
  2022-09-19 21:43         ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Han-Wen Nienhuys @ 2022-09-19 14:28 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Han-Wen Nienhuys via GitGitGadget, git, Han-Wen Nienhuys

On Tue, Sep 13, 2022 at 5:43 PM Junio C Hamano <gitster@pobox.com> wrote:
> then a better approach would be to have a small helper
> parse_local_worktree_ref() and make the primary one into something
> like
>...

Thanks, good idea. I'm sending you a v2.

-- 
Han-Wen Nienhuys - Google Munich
I work 80%. Don't expect answers from me on Fridays.
--
Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Liana Sebastian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2] refs: unify parse_worktree_ref() and ref_type()
  2022-09-12 17:01 [PATCH] refs: unify parse_worktree_ref() and ref_type() Han-Wen Nienhuys via GitGitGadget
  2022-09-12 19:17 ` Junio C Hamano
@ 2022-09-19 16:34 ` Han-Wen Nienhuys via GitGitGadget
  1 sibling, 0 replies; 9+ messages in thread
From: Han-Wen Nienhuys via GitGitGadget @ 2022-09-19 16:34 UTC (permalink / raw)
  To: git; +Cc: Han-Wen Nienhuys, Han-Wen Nienhuys, Han-Wen Nienhuys

From: Han-Wen Nienhuys <hanwen@google.com>

The logic to handle worktree refs (worktrees/NAME/REF and
main-worktree/REF) existed in two places:

* ref_type() in refs.c

* parse_worktree_ref() in worktree.c

Collapse this logic together in one function parse_worktree_ref():
this avoids having to cross-check the result of parse_worktree_ref()
and ref_type().

Introduce enum ref_worktree_type, which is slightly different from
enum ref_type. The latter is a misleading name (one would think that
'ref_type' would have the symref option).

Instead, enum ref_worktree_type only makes explicit how a refname
relates to a worktree. From this point of view, HEAD and
refs/bisect/abc are the same: they specify the current worktree
implicitly.

The files-backend must avoid packing refs/bisect/* and friends into
packed-refs, so expose is_per_worktree_ref() separately.

Signed-off-by: Han-Wen Nienhuys <hanwen@google.com>
---
    refs: unify parse_worktree_ref() and ref_type()
    
    The logic to handle worktree refs (worktrees/NAME/REF and
    main-worktree/REF) existed in two places:
    
     * ref_type() in refs.c
    
     * parse_worktree_ref() in worktree.c
    
    Collapse this logic together in one function parse_worktree_ref(): this
    avoids having to cross-check the result of parse_worktree_ref() and
    ref_type().
    
    Introduce enum ref_worktree_type, which is slightly different from enum
    ref_type. The latter is a misleading name (one would think that
    'ref_type' would have the symref option).
    
    Instead, enum ref_worktree_type only makes explicit how a refname
    relates to a worktree. From this point of view, HEAD and refs/bisect/abc
    are the same: they specify the current worktree implicitly.
    
    The files-backend must avoid packing refs/bisect/* and friends into
    packed-refs, so expose is_per_worktree_ref() separately.
    
    Signed-off-by: Han-Wen Nienhuys hanwen@google.com

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1325%2Fhanwen%2Fparse-worktree-ref-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1325/hanwen/parse-worktree-ref-v2
Pull-Request: https://github.com/git/git/pull/1325

Range-diff vs v1:

 1:  e2d19b80f76 ! 1:  b1e6dc0edc7 refs: unify parse_worktree_ref() and ref_type()
     @@ refs.c: static int is_pseudoref_syntax(const char *refname)
       			return 0;
       	}
       
     -+	/* HEAD is not a pseudoref, but it certainly uses the
     -+	 * pseudoref syntax. */
     ++	/*
     ++	 * HEAD is not a pseudoref, but it certainly uses the
     ++	 * pseudoref syntax.
     ++	 */
       	return 1;
       }
       
      -static int is_main_pseudoref_syntax(const char *refname)
     -+enum ref_worktree_type parse_worktree_ref(const char *worktree_ref,
     -+					  const char **name, int *name_length,
     -+					  const char **ref)
     - {
     +-{
      -	return skip_prefix(refname, "main-worktree/", &refname) &&
      -		*refname &&
      -		is_pseudoref_syntax(refname);
     --}
     -+	const char *name_dummy;
     -+	int name_length_dummy;
     -+	const char *ref_dummy;
     -+	if (!name)
     -+		name = &name_dummy;
     -+	if (!name_length)
     -+		name_length = &name_length_dummy;
     -+	if (!ref)
     -+		ref = &ref_dummy;
     ++static int is_current_worktree_ref(const char *ref) {
     ++	return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
     + }
       
      -static int is_other_pseudoref_syntax(const char *refname)
     --{
     ++enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
     ++					  const char **worktree_name, int *worktree_name_length,
     ++					  const char **bare_refname)
     + {
      -	if (!skip_prefix(refname, "worktrees/", &refname))
      -		return 0;
      -	refname = strchr(refname, '/');
     @@ refs.c: static int is_pseudoref_syntax(const char *refname)
      -		return 0;
      -	return is_pseudoref_syntax(refname + 1);
      -}
     -+	*ref = worktree_ref;
     -+	if (is_pseudoref_syntax(worktree_ref)) {
     -+		return REF_WORKTREE_CURRENT;
     -+	}
     ++	const char *name_dummy;
     ++	int name_length_dummy;
     ++	const char *ref_dummy;
       
      -enum ref_type ref_type(const char *refname)
      -{
     @@ refs.c: static int is_pseudoref_syntax(const char *refname)
      -	if (is_other_pseudoref_syntax(refname))
      -		return REF_TYPE_OTHER_PSEUDOREF;
      -	return REF_TYPE_NORMAL;
     -+	if (is_per_worktree_ref(worktree_ref)) {
     -+		return REF_WORKTREE_CURRENT;
     -+	}
     ++	if (!worktree_name)
     ++		worktree_name = &name_dummy;
     ++	if (!worktree_name_length)
     ++		worktree_name_length = &name_length_dummy;
     ++	if (!bare_refname)
     ++		bare_refname = &ref_dummy;
      +
     -+	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
     -+		if (!*worktree_ref)
     -+			return -1;
     -+		*name = NULL;
     -+		*name_length = 0;
     -+		*ref = worktree_ref;
     ++	if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
     ++		const char *slash = strchr(*bare_refname, '/');
      +
     -+		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
     -+		    REF_WORKTREE_CURRENT)
     -+			return REF_WORKTREE_MAIN;
     -+	}
     -+	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
     -+		const char *slash = strchr(worktree_ref, '/');
     ++		*worktree_name = *bare_refname;
     ++		if (!slash) {
     ++			*worktree_name_length = strlen(*worktree_name);
      +
     -+		if (!slash || slash == worktree_ref || !slash[1])
     -+			return -1;
     -+		*name = worktree_ref;
     -+		*name_length = slash - worktree_ref;
     -+		*ref = slash + 1;
     ++			/* This is an error condition, and the caller tell because the bare_refname is "" */
     ++			*bare_refname = *worktree_name + *worktree_name_length;
     ++			return REF_WORKTREE_OTHER;
     ++		}
     ++
     ++		*worktree_name_length = slash - *bare_refname;
     ++		*bare_refname = slash + 1;
      +
     -+		if (parse_worktree_ref(*ref, NULL, NULL, NULL) ==
     -+		    REF_WORKTREE_CURRENT)
     ++		if (is_current_worktree_ref(*bare_refname))
      +			return REF_WORKTREE_OTHER;
      +	}
      +
     ++	*worktree_name = NULL;
     ++	*worktree_name_length = 0;
     ++
     ++	if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
     ++	    && is_current_worktree_ref(*bare_refname))
     ++		return REF_WORKTREE_MAIN;
     ++
     ++	*bare_refname = maybe_worktree_ref;
     ++	if (is_current_worktree_ref(maybe_worktree_ref))
     ++		return REF_WORKTREE_CURRENT;
     ++
      +	return REF_WORKTREE_SHARED;
       }
       
     @@ refs.h: int parse_hide_refs_config(const char *var, const char *value, const cha
      +	REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
      +				 refs/bisect/SOMETHING */
      +	REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
     -+			      refs/main-worktree/HEAD */
     ++			      main-worktree/HEAD */
      +	REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
     -+			       refs/worktrees/bla/HEAD */
     ++			       worktrees/bla/HEAD */
      +	REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
       };
       
      -enum ref_type ref_type(const char *refname);
     -+/* Parse a ref that possibly explicitly refers to a worktree ref
     -+ * (ie. either REFNAME, main-worktree/REFNAME or
     -+ * worktree/WORKTREE/REFNAME). If the name references a worktree
     -+ * implicitly or explicitly, return what kind it was. The
     -+ * worktree_name, worktree_name_length and refname argument maybe NULL.
     ++/*
     ++ * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
     ++ * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
     ++ * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
     ++ * worktree name is returned in `worktree_name` (pointing into
     ++ * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
     ++ * refname stripped of prefixes) is returned in `bare_refname`. The
     ++ * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
     ++ * NULL.
      + */
      +enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
      +					  const char **worktree_name,
      +					  int *worktree_name_length,
     -+					  const char **refname);
     ++					  const char **bare_refname);
       
       enum expire_reflog_flags {
       	EXPIRE_REFLOGS_DRY_RUN = 1 << 0,


 builtin/reflog.c      |  3 +-
 reflog.c              | 13 ++-----
 refs.c                | 76 ++++++++++++++++++++++++++--------------
 refs.h                | 33 ++++++++++++++----
 refs/files-backend.c  | 80 +++++++++++++++++++------------------------
 refs/packed-backend.c |  2 +-
 worktree.c            | 59 ++++---------------------------
 worktree.h            | 10 ------
 8 files changed, 126 insertions(+), 150 deletions(-)

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 9407f835cb6..bd568d2d931 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -26,7 +26,8 @@ static int collect_reflog(const char *ref, const struct object_id *oid, int unus
 	 * Avoid collecting the same shared ref multiple times because
 	 * they are available via all worktrees.
 	 */
-	if (!worktree->is_current && ref_type(ref) == REF_TYPE_NORMAL)
+	if (!worktree->is_current &&
+	    parse_worktree_ref(ref, NULL, NULL, NULL) == REF_WORKTREE_SHARED)
 		return 0;
 
 	strbuf_worktree_ref(worktree, &newref, ref);
diff --git a/reflog.c b/reflog.c
index 47ba8620c56..0b8b767f97c 100644
--- a/reflog.c
+++ b/reflog.c
@@ -310,16 +310,9 @@ static int push_tip_to_list(const char *refname, const struct object_id *oid,
 
 static int is_head(const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_MAIN_PSEUDOREF:
-		if (parse_worktree_ref(refname, NULL, NULL, &refname))
-			BUG("not a worktree ref: %s", refname);
-		break;
-	default:
-		break;
-	}
-	return !strcmp(refname, "HEAD");
+	const char *stripped_refname;
+	parse_worktree_ref(refname, NULL, NULL, &stripped_refname);
+	return !strcmp(stripped_refname, "HEAD");
 }
 
 void reflog_expiry_prepare(const char *refname,
diff --git a/refs.c b/refs.c
index 1a964505f92..6e86607f3bb 100644
--- a/refs.c
+++ b/refs.c
@@ -722,7 +722,7 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **log)
 	return repo_dwim_log(the_repository, str, len, oid, log);
 }
 
-static int is_per_worktree_ref(const char *refname)
+int is_per_worktree_ref(const char *refname)
 {
 	return starts_with(refname, "refs/worktree/") ||
 	       starts_with(refname, "refs/bisect/") ||
@@ -738,37 +738,63 @@ static int is_pseudoref_syntax(const char *refname)
 			return 0;
 	}
 
+	/*
+	 * HEAD is not a pseudoref, but it certainly uses the
+	 * pseudoref syntax.
+	 */
 	return 1;
 }
 
-static int is_main_pseudoref_syntax(const char *refname)
-{
-	return skip_prefix(refname, "main-worktree/", &refname) &&
-		*refname &&
-		is_pseudoref_syntax(refname);
+static int is_current_worktree_ref(const char *ref) {
+	return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
 }
 
-static int is_other_pseudoref_syntax(const char *refname)
+enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
+					  const char **worktree_name, int *worktree_name_length,
+					  const char **bare_refname)
 {
-	if (!skip_prefix(refname, "worktrees/", &refname))
-		return 0;
-	refname = strchr(refname, '/');
-	if (!refname || !refname[1])
-		return 0;
-	return is_pseudoref_syntax(refname + 1);
-}
+	const char *name_dummy;
+	int name_length_dummy;
+	const char *ref_dummy;
 
-enum ref_type ref_type(const char *refname)
-{
-	if (is_per_worktree_ref(refname))
-		return REF_TYPE_PER_WORKTREE;
-	if (is_pseudoref_syntax(refname))
-		return REF_TYPE_PSEUDOREF;
-	if (is_main_pseudoref_syntax(refname))
-		return REF_TYPE_MAIN_PSEUDOREF;
-	if (is_other_pseudoref_syntax(refname))
-		return REF_TYPE_OTHER_PSEUDOREF;
-	return REF_TYPE_NORMAL;
+	if (!worktree_name)
+		worktree_name = &name_dummy;
+	if (!worktree_name_length)
+		worktree_name_length = &name_length_dummy;
+	if (!bare_refname)
+		bare_refname = &ref_dummy;
+
+	if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
+		const char *slash = strchr(*bare_refname, '/');
+
+		*worktree_name = *bare_refname;
+		if (!slash) {
+			*worktree_name_length = strlen(*worktree_name);
+
+			/* This is an error condition, and the caller tell because the bare_refname is "" */
+			*bare_refname = *worktree_name + *worktree_name_length;
+			return REF_WORKTREE_OTHER;
+		}
+
+		*worktree_name_length = slash - *bare_refname;
+		*bare_refname = slash + 1;
+
+		if (is_current_worktree_ref(*bare_refname))
+			return REF_WORKTREE_OTHER;
+	}
+
+	*worktree_name = NULL;
+	*worktree_name_length = 0;
+
+	if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
+	    && is_current_worktree_ref(*bare_refname))
+		return REF_WORKTREE_MAIN;
+
+	*bare_refname = maybe_worktree_ref;
+	if (is_current_worktree_ref(maybe_worktree_ref))
+		return REF_WORKTREE_CURRENT;
+
+	return REF_WORKTREE_SHARED;
 }
 
 long get_files_ref_lock_timeout_ms(void)
diff --git a/refs.h b/refs.h
index 23479c7ee09..a42957a7917 100644
--- a/refs.h
+++ b/refs.h
@@ -825,15 +825,34 @@ int parse_hide_refs_config(const char *var, const char *value, const char *);
  */
 int ref_is_hidden(const char *, const char *);
 
-enum ref_type {
-	REF_TYPE_PER_WORKTREE,	  /* refs inside refs/ but not shared       */
-	REF_TYPE_PSEUDOREF,	  /* refs outside refs/ in current worktree */
-	REF_TYPE_MAIN_PSEUDOREF,  /* pseudo refs from the main worktree     */
-	REF_TYPE_OTHER_PSEUDOREF, /* pseudo refs from other worktrees       */
-	REF_TYPE_NORMAL,	  /* normal/shared refs inside refs/        */
+/* Is this a per-worktree ref living in the refs/ namespace? */
+int is_per_worktree_ref(const char *refname);
+
+/* Describes how a refname relates to worktrees */
+enum ref_worktree_type {
+	REF_WORKTREE_CURRENT, /* implicitly per worktree, eg. HEAD or
+				 refs/bisect/SOMETHING */
+	REF_WORKTREE_MAIN, /* explicitly in main worktree, eg.
+			      main-worktree/HEAD */
+	REF_WORKTREE_OTHER, /* explicitly in named worktree, eg.
+			       worktrees/bla/HEAD */
+	REF_WORKTREE_SHARED, /* the default, eg. refs/heads/main */
 };
 
-enum ref_type ref_type(const char *refname);
+/*
+ * Parse a `maybe_worktree_ref` as a ref that possibly refers to a worktree ref
+ * (ie. either REFNAME, main-worktree/REFNAME or worktree/WORKTREE/REFNAME). It
+ * returns what kind of ref was found, and in case of REF_WORKTREE_OTHER, the
+ * worktree name is returned in `worktree_name` (pointing into
+ * `maybe_worktree_ref`) and `worktree_name_length`. The bare refname (the
+ * refname stripped of prefixes) is returned in `bare_refname`. The
+ * `worktree_name`, `worktree_name_length` and `bare_refname` arguments may be
+ * NULL.
+ */
+enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
+					  const char **worktree_name,
+					  int *worktree_name_length,
+					  const char **bare_refname);
 
 enum expire_reflog_flags {
 	EXPIRE_REFLOGS_DRY_RUN = 1 << 0,
diff --git a/refs/files-backend.c b/refs/files-backend.c
index 95acab78eef..f230704229e 100644
--- a/refs/files-backend.c
+++ b/refs/files-backend.c
@@ -138,44 +138,30 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store,
 	return refs;
 }
 
-static void files_reflog_path_other_worktrees(struct files_ref_store *refs,
-					      struct strbuf *sb,
-					      const char *refname)
-{
-	const char *real_ref;
-	const char *worktree_name;
-	int length;
-
-	if (parse_worktree_ref(refname, &worktree_name, &length, &real_ref))
-		BUG("refname %s is not a other-worktree ref", refname);
-
-	if (worktree_name)
-		strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
-			    length, worktree_name, real_ref);
-	else
-		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir,
-			    real_ref);
-}
-
 static void files_reflog_path(struct files_ref_store *refs,
 			      struct strbuf *sb,
 			      const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PER_WORKTREE:
-	case REF_TYPE_PSEUDOREF:
+	const char *bare_refname;
+	const char *wtname;
+	int wtname_len;
+	enum ref_worktree_type wt_type = parse_worktree_ref(
+		refname, &wtname, &wtname_len, &bare_refname);
+
+	switch (wt_type) {
+	case REF_WORKTREE_CURRENT:
 		strbuf_addf(sb, "%s/logs/%s", refs->base.gitdir, refname);
 		break;
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_MAIN_PSEUDOREF:
-		files_reflog_path_other_worktrees(refs, sb, refname);
+	case REF_WORKTREE_SHARED:
+	case REF_WORKTREE_MAIN:
+		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, bare_refname);
 		break;
-	case REF_TYPE_NORMAL:
-		strbuf_addf(sb, "%s/logs/%s", refs->gitcommondir, refname);
+	case REF_WORKTREE_OTHER:
+		strbuf_addf(sb, "%s/worktrees/%.*s/logs/%s", refs->gitcommondir,
+			    wtname_len, wtname, bare_refname);
 		break;
 	default:
-		BUG("unknown ref type %d of ref %s",
-		    ref_type(refname), refname);
+		BUG("unknown ref type %d of ref %s", wt_type, refname);
 	}
 }
 
@@ -183,22 +169,25 @@ static void files_ref_path(struct files_ref_store *refs,
 			   struct strbuf *sb,
 			   const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PER_WORKTREE:
-	case REF_TYPE_PSEUDOREF:
+	const char *bare_refname;
+	const char *wtname;
+	int wtname_len;
+	enum ref_worktree_type wt_type = parse_worktree_ref(
+		refname, &wtname, &wtname_len, &bare_refname);
+	switch (wt_type) {
+	case REF_WORKTREE_CURRENT:
 		strbuf_addf(sb, "%s/%s", refs->base.gitdir, refname);
 		break;
-	case REF_TYPE_MAIN_PSEUDOREF:
-		if (!skip_prefix(refname, "main-worktree/", &refname))
-			BUG("ref %s is not a main pseudoref", refname);
-		/* fallthrough */
-	case REF_TYPE_OTHER_PSEUDOREF:
-	case REF_TYPE_NORMAL:
-		strbuf_addf(sb, "%s/%s", refs->gitcommondir, refname);
+	case REF_WORKTREE_OTHER:
+		strbuf_addf(sb, "%s/worktrees/%.*s/%s", refs->gitcommondir,
+			    wtname_len, wtname, bare_refname);
+		break;
+	case REF_WORKTREE_SHARED:
+	case REF_WORKTREE_MAIN:
+		strbuf_addf(sb, "%s/%s", refs->gitcommondir, bare_refname);
 		break;
 	default:
-		BUG("unknown ref type %d of ref %s",
-		    ref_type(refname), refname);
+		BUG("unknown ref type %d of ref %s", wt_type, refname);
 	}
 }
 
@@ -771,7 +760,8 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
 
 	while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
 		if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
-		    ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
+		    parse_worktree_ref(iter->iter0->refname, NULL, NULL,
+				       NULL) != REF_WORKTREE_CURRENT)
 			continue;
 
 		if ((iter->flags & DO_FOR_EACH_OMIT_DANGLING_SYMREFS) &&
@@ -1179,7 +1169,8 @@ static int should_pack_ref(const char *refname,
 			   unsigned int pack_flags)
 {
 	/* Do not pack per-worktree refs: */
-	if (ref_type(refname) != REF_TYPE_NORMAL)
+	if (parse_worktree_ref(refname, NULL, NULL, NULL) !=
+	    REF_WORKTREE_SHARED)
 		return 0;
 
 	/* Do not pack non-tags unless PACK_REFS_ALL is set: */
@@ -2277,7 +2268,8 @@ static enum iterator_selection reflog_iterator_select(
 		 */
 		return ITER_SELECT_0;
 	} else if (iter_common) {
-		if (ref_type(iter_common->refname) == REF_TYPE_NORMAL)
+		if (parse_worktree_ref(iter_common->refname, NULL, NULL,
+				       NULL) == REF_WORKTREE_SHARED)
 			return ITER_SELECT_1;
 
 		/*
diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index 66c4574c99d..bf0e63ae70c 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -862,7 +862,7 @@ static int packed_ref_iterator_advance(struct ref_iterator *ref_iterator)
 
 	while ((ok = next_record(iter)) == ITER_OK) {
 		if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
-		    ref_type(iter->base.refname) != REF_TYPE_PER_WORKTREE)
+		    !is_per_worktree_ref(iter->base.refname))
 			continue;
 
 		if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
diff --git a/worktree.c b/worktree.c
index 90fc085f76b..bb7873c72d1 100644
--- a/worktree.c
+++ b/worktree.c
@@ -489,62 +489,17 @@ int submodule_uses_worktrees(const char *path)
 	return ret;
 }
 
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref)
-{
-	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
-		if (!*worktree_ref)
-			return -1;
-		if (name)
-			*name = NULL;
-		if (name_length)
-			*name_length = 0;
-		if (ref)
-			*ref = worktree_ref;
-		return 0;
-	}
-	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
-		const char *slash = strchr(worktree_ref, '/');
-
-		if (!slash || slash == worktree_ref || !slash[1])
-			return -1;
-		if (name)
-			*name = worktree_ref;
-		if (name_length)
-			*name_length = slash - worktree_ref;
-		if (ref)
-			*ref = slash + 1;
-		return 0;
-	}
-	return -1;
-}
-
 void strbuf_worktree_ref(const struct worktree *wt,
 			 struct strbuf *sb,
 			 const char *refname)
 {
-	switch (ref_type(refname)) {
-	case REF_TYPE_PSEUDOREF:
-	case REF_TYPE_PER_WORKTREE:
-		if (wt && !wt->is_current) {
-			if (is_main_worktree(wt))
-				strbuf_addstr(sb, "main-worktree/");
-			else
-				strbuf_addf(sb, "worktrees/%s/", wt->id);
-		}
-		break;
-
-	case REF_TYPE_MAIN_PSEUDOREF:
-	case REF_TYPE_OTHER_PSEUDOREF:
-		break;
-
-	case REF_TYPE_NORMAL:
-		/*
-		 * For shared refs, don't prefix worktrees/ or
-		 * main-worktree/. It's not necessary and
-		 * files-backend.c can't handle it anyway.
-		 */
-		break;
+	if (parse_worktree_ref(refname, NULL, NULL, NULL) ==
+		    REF_WORKTREE_CURRENT &&
+	    wt && !wt->is_current) {
+		if (is_main_worktree(wt))
+			strbuf_addstr(sb, "main-worktree/");
+		else
+			strbuf_addf(sb, "worktrees/%s/", wt->id);
 	}
 	strbuf_addstr(sb, refname);
 }
diff --git a/worktree.h b/worktree.h
index e9e839926b0..9dcea6fc8c1 100644
--- a/worktree.h
+++ b/worktree.h
@@ -166,16 +166,6 @@ const char *worktree_git_path(const struct worktree *wt,
 			      const char *fmt, ...)
 	__attribute__((format (printf, 2, 3)));
 
-/*
- * Parse a worktree ref (i.e. with prefix main-worktree/ or
- * worktrees/) and return the position of the worktree's name and
- * length (or NULL and zero if it's main worktree), and ref.
- *
- * All name, name_length and ref arguments could be NULL.
- */
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref);
-
 /*
  * Return a refname suitable for access from the current ref store.
  */

base-commit: 805e0a68082a217f0112db9ee86a022227a9c81b
-- 
@@ -166,16 +166,6 @@ const char *worktree_git_path(const struct worktree *wt,
 			      const char *fmt, ...)
 	__attribute__((format (printf, 2, 3)));
 
-/*
- * Parse a worktree ref (i.e. with prefix main-worktree/ or
- * worktrees/) and return the position of the worktree's name and
- * length (or NULL and zero if it's main worktree), and ref.
- *
- * All name, name_length and ref arguments could be NULL.
- */
-int parse_worktree_ref(const char *worktree_ref, const char **name,
-		       int *name_length, const char **ref);
-
 /*
  * Return a refname suitable for access from the current ref store.
  */

base-commit: 805e0a68082a217f0112db9ee86a022227a9c81b
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-19 14:28       ` Han-Wen Nienhuys
@ 2022-09-19 21:43         ` Junio C Hamano
  2022-09-20  8:53           ` Han-Wen Nienhuys
  0 siblings, 1 reply; 9+ messages in thread
From: Junio C Hamano @ 2022-09-19 21:43 UTC (permalink / raw)
  To: Han-Wen Nienhuys; +Cc: Han-Wen Nienhuys via GitGitGadget, git, Han-Wen Nienhuys

Han-Wen Nienhuys <hanwen@google.com> writes:

> On Tue, Sep 13, 2022 at 5:43 PM Junio C Hamano <gitster@pobox.com> wrote:
>> then a better approach would be to have a small helper
>> parse_local_worktree_ref() and make the primary one into something
>> like
>>...
>
> Thanks, good idea. I'm sending you a v2.

Hmph, is that "v2" <pull.1325.v2.git.git.1663605291172.gitgitgadget@gmail.com>
or is there another version of it?

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-19 21:43         ` Junio C Hamano
@ 2022-09-20  8:53           ` Han-Wen Nienhuys
  2022-09-21 16:45             ` Junio C Hamano
  0 siblings, 1 reply; 9+ messages in thread
From: Han-Wen Nienhuys @ 2022-09-20  8:53 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Han-Wen Nienhuys via GitGitGadget, git, Han-Wen Nienhuys

On Mon, Sep 19, 2022 at 11:43 PM Junio C Hamano <gitster@pobox.com> wrote:
> > On Tue, Sep 13, 2022 at 5:43 PM Junio C Hamano <gitster@pobox.com> wrote:
> >> then a better approach would be to have a small helper
> >> parse_local_worktree_ref() and make the primary one into something
> >> like
> >>...
> >
> > Thanks, good idea. I'm sending you a v2.
>
> is that "v2" <pull.1325.v2.git.git.1663605291172.gitgitgadget@gmail.com>
> or is there another version of it?

I think so.

> Hmph,

Did I do something wrong?

-- 
Han-Wen Nienhuys - Google Munich
I work 80%. Don't expect answers from me on Fridays.
--
Google Germany GmbH, Erika-Mann-Strasse 33, 80636 Munich
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Liana Sebastian

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()
  2022-09-20  8:53           ` Han-Wen Nienhuys
@ 2022-09-21 16:45             ` Junio C Hamano
  0 siblings, 0 replies; 9+ messages in thread
From: Junio C Hamano @ 2022-09-21 16:45 UTC (permalink / raw)
  To: Han-Wen Nienhuys; +Cc: Han-Wen Nienhuys via GitGitGadget, git, Han-Wen Nienhuys

Han-Wen Nienhuys <hanwen@google.com> writes:

> On Mon, Sep 19, 2022 at 11:43 PM Junio C Hamano <gitster@pobox.com> wrote:
>> > On Tue, Sep 13, 2022 at 5:43 PM Junio C Hamano <gitster@pobox.com> wrote:
>> >> then a better approach would be to have a small helper
>> >> parse_local_worktree_ref() and make the primary one into something
>> >> like
>> >>...
>> >
>> > Thanks, good idea. I'm sending you a v2.
>>
>> is that "v2" <pull.1325.v2.git.git.1663605291172.gitgitgadget@gmail.com>
>> or is there another version of it?
>
> I think so.
>
>> Hmph,
>
> Did I do something wrong?

Not "wrong" per-se, but I was surprised by the patch that looked
quite different from what I expected from your response to the
"small helper" suggestion.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-09-21 16:50 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-12 17:01 [PATCH] refs: unify parse_worktree_ref() and ref_type() Han-Wen Nienhuys via GitGitGadget
2022-09-12 19:17 ` Junio C Hamano
2022-09-12 20:13   ` Junio C Hamano
2022-09-13 15:43     ` Junio C Hamano
2022-09-19 14:28       ` Han-Wen Nienhuys
2022-09-19 21:43         ` Junio C Hamano
2022-09-20  8:53           ` Han-Wen Nienhuys
2022-09-21 16:45             ` Junio C Hamano
2022-09-19 16:34 ` [PATCH v2] " Han-Wen Nienhuys via GitGitGadget

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).