@matheusbsbs / matheus-bagliano / commits / a2fab531bb
Fork 0

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 <gitster@pobox.com>

Junio C Hamano committed Mar 21, 2009 at 14:35 UTC a2fab531bbb00ff64335906e22854365be2eb5c7
6 files changed +17 -17
branch.c
+1 -4
@@ -135,10 +135,7 @@ void create_branch(const char *head,
135 struct strbuf ref = STRBUF_INIT;
136 int forcing = 0;
137
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))
139 die("'%s' is not a valid branch name.", name);
140
141 if (resolve_ref(ref.buf, sha1, 1, NULL)) {
builtin-branch.c
+2 -6
@@ -468,14 +468,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
468 if (!oldname)
469 die("cannot rename the current branch while not on any.");
470
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))
472 die("Invalid branch name: '%s'", oldname);
473
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))
475 die("Invalid branch name: '%s'", newname);
476
477 if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
builtin-check-ref-format.c
+2 -3
@@ -11,9 +11,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
11 {
12 if (argc == 3 && !strcmp(argv[1], "--branch")) {
13 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]))
16 die("'%s' is not a valid branch name", argv[2]);
17 printf("%s\n", sb.buf + 11);
18 exit(0);
builtin-checkout.c
+3 -4
@@ -733,12 +733,11 @@ no_reference:
733
734 if (opts.new_branch) {
735 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);
739 if (!get_sha1(buf.buf, rev))
740 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);
741 strbuf_release(&buf);
742 }
743
strbuf.c
+8
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "refs.h"
3
4 int prefixcmp(const char *str, const char *prefix)
5 {
@@ -366,3 +367,10 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
367 strbuf_add(sb, name, len);
368 return len;
369 }
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
+1
@@ -132,5 +132,6 @@ extern void stripspace(struct strbuf *buf, int skip_comments);
132 extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
133
134 extern int strbuf_branchname(struct strbuf *sb, const char *name);
135 +extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
136
137 #endif /* STRBUF_H */