提交 a2fab531 编写于 作者: J Junio C Hamano

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: NJunio C Hamano <gitster@pobox.com>
上级 03d3aada
...@@ -135,10 +135,7 @@ void create_branch(const char *head, ...@@ -135,10 +135,7 @@ void create_branch(const char *head,
struct strbuf ref = STRBUF_INIT; struct strbuf ref = STRBUF_INIT;
int forcing = 0; int forcing = 0;
strbuf_branchname(&ref, name); if (strbuf_check_branch_ref(&ref, name))
strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
if (check_ref_format(ref.buf))
die("'%s' is not a valid branch name.", name); die("'%s' is not a valid branch name.", name);
if (resolve_ref(ref.buf, sha1, 1, NULL)) { if (resolve_ref(ref.buf, sha1, 1, NULL)) {
......
...@@ -468,14 +468,10 @@ static void rename_branch(const char *oldname, const char *newname, int force) ...@@ -468,14 +468,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
if (!oldname) if (!oldname)
die("cannot rename the current branch while not on any."); die("cannot rename the current branch while not on any.");
strbuf_branchname(&oldref, oldname); if (strbuf_check_branch_ref(&oldref, oldname))
strbuf_splice(&oldref, 0, 0, "refs/heads/", 11);
if (check_ref_format(oldref.buf))
die("Invalid branch name: '%s'", oldname); die("Invalid branch name: '%s'", oldname);
strbuf_branchname(&newref, newname); if (strbuf_check_branch_ref(&newref, newname))
strbuf_splice(&newref, 0, 0, "refs/heads/", 11);
if (check_ref_format(newref.buf))
die("Invalid branch name: '%s'", newname); die("Invalid branch name: '%s'", newname);
if (resolve_ref(newref.buf, sha1, 1, NULL) && !force) if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
......
...@@ -11,9 +11,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix) ...@@ -11,9 +11,8 @@ int cmd_check_ref_format(int argc, const char **argv, const char *prefix)
{ {
if (argc == 3 && !strcmp(argv[1], "--branch")) { if (argc == 3 && !strcmp(argv[1], "--branch")) {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
strbuf_branchname(&sb, argv[2]);
strbuf_splice(&sb, 0, 0, "refs/heads/", 11); if (strbuf_check_branch_ref(&sb, argv[2]))
if (check_ref_format(sb.buf))
die("'%s' is not a valid branch name", argv[2]); die("'%s' is not a valid branch name", argv[2]);
printf("%s\n", sb.buf + 11); printf("%s\n", sb.buf + 11);
exit(0); exit(0);
......
...@@ -733,12 +733,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) ...@@ -733,12 +733,11 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
if (opts.new_branch) { if (opts.new_branch) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
strbuf_addstr(&buf, "refs/heads/"); if (strbuf_check_branch_ref(&buf, opts.new_branch))
strbuf_addstr(&buf, opts.new_branch); die("git checkout: we do not like '%s' as a branch name.",
opts.new_branch);
if (!get_sha1(buf.buf, rev)) if (!get_sha1(buf.buf, rev))
die("git checkout: branch %s already exists", opts.new_branch); die("git checkout: branch %s already exists", opts.new_branch);
if (check_ref_format(buf.buf))
die("git checkout: we do not like '%s' as a branch name.", opts.new_branch);
strbuf_release(&buf); strbuf_release(&buf);
} }
......
#include "cache.h" #include "cache.h"
#include "refs.h"
int prefixcmp(const char *str, const char *prefix) int prefixcmp(const char *str, const char *prefix)
{ {
...@@ -366,3 +367,10 @@ int strbuf_branchname(struct strbuf *sb, const char *name) ...@@ -366,3 +367,10 @@ int strbuf_branchname(struct strbuf *sb, const char *name)
strbuf_add(sb, name, len); strbuf_add(sb, name, len);
return len; return len;
} }
int strbuf_check_branch_ref(struct strbuf *sb, const char *name)
{
strbuf_branchname(sb, name);
strbuf_splice(sb, 0, 0, "refs/heads/", 11);
return check_ref_format(sb->buf);
}
...@@ -132,5 +132,6 @@ extern void stripspace(struct strbuf *buf, int skip_comments); ...@@ -132,5 +132,6 @@ extern void stripspace(struct strbuf *buf, int skip_comments);
extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env); extern int launch_editor(const char *path, struct strbuf *buffer, const char *const *env);
extern int strbuf_branchname(struct strbuf *sb, const char *name); extern int strbuf_branchname(struct strbuf *sb, const char *name);
extern int strbuf_check_branch_ref(struct strbuf *sb, const char *name);
#endif /* STRBUF_H */ #endif /* STRBUF_H */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册