Skip to content

Commit 5bcbde9

Browse files
committed
refs: move ref name helpers around
strbuf_branchname(), strbuf_check_{branch,tag}_ref() are helper functions to deal with branch and tag names, and the fact that they happen to use strbuf to hold the name of a branch or a tag is not essential. These functions fit better in the refs API than strbuf API, the latter of which is about string manipulations. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 92999a4 commit 5bcbde9

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

builtin/tag.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -447,17 +447,6 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
447447
return 0;
448448
}
449449

450-
static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
451-
{
452-
if (name[0] == '-')
453-
return -1;
454-
455-
strbuf_reset(sb);
456-
strbuf_addf(sb, "refs/tags/%s", name);
457-
458-
return check_refname_format(sb->buf, 0);
459-
}
460-
461450
int cmd_tag(int argc,
462451
const char **argv,
463452
const char *prefix,

object-name.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,42 +1734,6 @@ int repo_interpret_branch_name(struct repository *r,
17341734
return -1;
17351735
}
17361736

1737-
void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
1738-
{
1739-
int len = strlen(name);
1740-
struct interpret_branch_name_options options = {
1741-
.allowed = allowed
1742-
};
1743-
int used = repo_interpret_branch_name(the_repository, name, len, sb,
1744-
&options);
1745-
1746-
if (used < 0)
1747-
used = 0;
1748-
strbuf_add(sb, name + used, len - used);
1749-
}
1750-
1751-
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
1752-
{
1753-
if (startup_info->have_repository)
1754-
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
1755-
else
1756-
strbuf_addstr(sb, name);
1757-
1758-
/*
1759-
* This splice must be done even if we end up rejecting the
1760-
* name; builtin/branch.c::copy_or_rename_branch() still wants
1761-
* to see what the name expanded to so that "branch -m" can be
1762-
* used as a tool to correct earlier mistakes.
1763-
*/
1764-
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
1765-
1766-
if (*name == '-' ||
1767-
!strcmp(sb->buf, "refs/heads/HEAD"))
1768-
return -1;
1769-
1770-
return check_refname_format(sb->buf, 0);
1771-
}
1772-
17731737
void object_context_release(struct object_context *ctx)
17741738
{
17751739
free(ctx->path);

refs.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,53 @@ static char *substitute_branch_name(struct repository *r,
697697
return NULL;
698698
}
699699

700+
void strbuf_branchname(struct strbuf *sb, const char *name, unsigned allowed)
701+
{
702+
int len = strlen(name);
703+
struct interpret_branch_name_options options = {
704+
.allowed = allowed
705+
};
706+
int used = repo_interpret_branch_name(the_repository, name, len, sb,
707+
&options);
708+
709+
if (used < 0)
710+
used = 0;
711+
strbuf_add(sb, name + used, len - used);
712+
}
713+
714+
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
715+
{
716+
if (startup_info->have_repository)
717+
strbuf_branchname(sb, name, INTERPRET_BRANCH_LOCAL);
718+
else
719+
strbuf_addstr(sb, name);
720+
721+
/*
722+
* This splice must be done even if we end up rejecting the
723+
* name; builtin/branch.c::copy_or_rename_branch() still wants
724+
* to see what the name expanded to so that "branch -m" can be
725+
* used as a tool to correct earlier mistakes.
726+
*/
727+
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
728+
729+
if (*name == '-' ||
730+
!strcmp(sb->buf, "refs/heads/HEAD"))
731+
return -1;
732+
733+
return check_refname_format(sb->buf, 0);
734+
}
735+
736+
int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
737+
{
738+
if (name[0] == '-')
739+
return -1;
740+
741+
strbuf_reset(sb);
742+
strbuf_addf(sb, "refs/tags/%s", name);
743+
744+
return check_refname_format(sb->buf, 0);
745+
}
746+
700747
int repo_dwim_ref(struct repository *r, const char *str, int len,
701748
struct object_id *oid, char **ref, int nonfatal_dangling_mark)
702749
{

refs.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,35 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_
180180
*/
181181
char *repo_default_branch_name(struct repository *r, int quiet);
182182

183+
/*
184+
* Copy "name" to "sb", expanding any special @-marks as handled by
185+
* repo_interpret_branch_name(). The result is a non-qualified branch name
186+
* (so "foo" or "origin/master" instead of "refs/heads/foo" or
187+
* "refs/remotes/origin/master").
188+
*
189+
* Note that the resulting name may not be a syntactically valid refname.
190+
*
191+
* If "allowed" is non-zero, restrict the set of allowed expansions. See
192+
* repo_interpret_branch_name() for details.
193+
*/
194+
void strbuf_branchname(struct strbuf *sb, const char *name,
195+
unsigned allowed);
196+
197+
/*
198+
* Like strbuf_branchname() above, but confirm that the result is
199+
* syntactically valid to be used as a local branch name in refs/heads/.
200+
*
201+
* The return value is "0" if the result is valid, and "-1" otherwise.
202+
*/
203+
int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
204+
205+
/*
206+
* Similar for a tag name in refs/tags/.
207+
*
208+
* The return value is "0" if the result is valid, and "-1" otherwise.
209+
*/
210+
int strbuf_check_tag_ref(struct strbuf *sb, const char *name);
211+
183212
/*
184213
* A ref_transaction represents a collection of reference updates that
185214
* should succeed or fail together.

strbuf.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -637,28 +637,6 @@ static inline void strbuf_complete_line(struct strbuf *sb)
637637
strbuf_complete(sb, '\n');
638638
}
639639

640-
/*
641-
* Copy "name" to "sb", expanding any special @-marks as handled by
642-
* repo_interpret_branch_name(). The result is a non-qualified branch name
643-
* (so "foo" or "origin/master" instead of "refs/heads/foo" or
644-
* "refs/remotes/origin/master").
645-
*
646-
* Note that the resulting name may not be a syntactically valid refname.
647-
*
648-
* If "allowed" is non-zero, restrict the set of allowed expansions. See
649-
* repo_interpret_branch_name() for details.
650-
*/
651-
void strbuf_branchname(struct strbuf *sb, const char *name,
652-
unsigned allowed);
653-
654-
/*
655-
* Like strbuf_branchname() above, but confirm that the result is
656-
* syntactically valid to be used as a local branch name in refs/heads/.
657-
*
658-
* The return value is "0" if the result is valid, and "-1" otherwise.
659-
*/
660-
int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
661-
662640
typedef int (*char_predicate)(char ch);
663641

664642
void strbuf_addstr_urlencode(struct strbuf *sb, const char *name,

0 commit comments

Comments
 (0)