Skip to content

Commit a2fab53

Browse files
committed
strbuf_check_branch_ref(): a helper to check a refname for a branch
This allows a common calling sequence strbuf_branchname(&ref, name); strbuf_splice(&ref, 0, 0, "refs/heads/", 11); if (check_ref_format(ref.buf)) die(...); to be refactored into if (strbuf_check_branch_ref(&ref, name)) die(...); Signed-off-by: Junio C Hamano <[email protected]>
1 parent 03d3aad commit a2fab53

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

branch.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,7 @@ void create_branch(const char *head,
135135
struct strbuf ref = STRBUF_INIT;
136136
int forcing = 0;
137137

138-
strbuf_branchname(&ref, name);
139-
strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
140-
141-
if (check_ref_format(ref.buf))
138+
if (strbuf_check_branch_ref(&ref, name))
142139
die("'%s' is not a valid branch name.", name);
143140

144141
if (resolve_ref(ref.buf, sha1, 1, NULL)) {

builtin-branch.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
468468
if (!oldname)
469469
die("cannot rename the current branch while not on any.");
470470

471-
strbuf_branchname(&oldref, oldname);
472-
strbuf_splice(&oldref, 0, 0, "refs/heads/", 11);
473-
if (check_ref_format(oldref.buf))
471+
if (strbuf_check_branch_ref(&oldref, oldname))
474472
die("Invalid branch name: '%s'", oldname);
475473

476-
strbuf_branchname(&newref, newname);
477-
strbuf_splice(&newref, 0, 0, "refs/heads/", 11);
478-
if (check_ref_format(newref.buf))
474+
if (strbuf_check_branch_ref(&newref, newname))
479475
die("Invalid branch name: '%s'", newname);
480476

481477
if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)

builtin-check-ref-format.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
1111
{
1212
if (argc == 3 && !strcmp(argv[1], "--branch")) {
1313
struct strbuf sb = STRBUF_INIT;
14-
strbuf_branchname(&sb, argv[2]);
15-
strbuf_splice(&sb, 0, 0, "refs/heads/", 11);
16-
if (check_ref_format(sb.buf))
14+
15+
if (strbuf_check_branch_ref(&sb, argv[2]))
1716
die("'%s' is not a valid branch name", argv[2]);
1817
printf("%s\n", sb.buf + 11);
1918
exit(0);

builtin-checkout.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
733733

734734
if (opts.new_branch) {
735735
struct strbuf buf = STRBUF_INIT;
736-
strbuf_addstr(&buf, "refs/heads/");
737-
strbuf_addstr(&buf, opts.new_branch);
736+
if (strbuf_check_branch_ref(&buf, opts.new_branch))
737+
die("git checkout: we do not like '%s' as a branch name.",
738+
opts.new_branch);
738739
if (!get_sha1(buf.buf, rev))
739740
die("git checkout: branch %s already exists", opts.new_branch);
740-
if (check_ref_format(buf.buf))
741-
die("git checkout: we do not like '%s' as a branch name.", opts.new_branch);
742741
strbuf_release(&buf);
743742
}
744743

strbuf.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "refs.h"
23

34
int prefixcmp(const char *str, const char *prefix)
45
{
@@ -366,3 +367,10 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
366367
strbuf_add(sb, name, len);
367368
return len;
368369
}
370+
371+
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
372+
{
373+
strbuf_branchname(sb, name);
374+
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
375+
return check_ref_format(sb->buf);
376+
}

strbuf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,6 @@ extern void stripspace(struct strbuf *buf, int skip_comments);
132132
extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
133133

134134
extern int strbuf_branchname(struct strbuf *sb, const char *name);
135+
extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
135136

136137
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)