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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
d3fa443f97
commit
71e5473493
59
worktree.c
59
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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user