strbuf_branchname(): a wrapper for branch name shorthands
The function takes a user-supplied string that is supposed to be a branch name, and puts it in a strbuf after expanding possible shorthand notation. A handful of open coded sequence to do this in the existing code have been changed to use this helper function. Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
431b1969fc
commit
a552de75eb
7
branch.c
7
branch.c
@ -134,13 +134,8 @@ void create_branch(const char *head,
|
|||||||
char *real_ref, msg[PATH_MAX + 20];
|
char *real_ref, msg[PATH_MAX + 20];
|
||||||
struct strbuf ref = STRBUF_INIT;
|
struct strbuf ref = STRBUF_INIT;
|
||||||
int forcing = 0;
|
int forcing = 0;
|
||||||
int len;
|
|
||||||
|
|
||||||
len = strlen(name);
|
strbuf_branchname(&ref, name);
|
||||||
if (interpret_branch_name(name, &ref) != len) {
|
|
||||||
strbuf_reset(&ref);
|
|
||||||
strbuf_add(&ref, name, len);
|
|
||||||
}
|
|
||||||
strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
|
strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
|
||||||
|
|
||||||
if (check_ref_format(ref.buf))
|
if (check_ref_format(ref.buf))
|
||||||
|
@ -121,11 +121,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
|
|||||||
die("Couldn't look up commit object for HEAD");
|
die("Couldn't look up commit object for HEAD");
|
||||||
}
|
}
|
||||||
for (i = 0; i < argc; i++, strbuf_release(&bname)) {
|
for (i = 0; i < argc; i++, strbuf_release(&bname)) {
|
||||||
int len = strlen(argv[i]);
|
strbuf_branchname(&bname, argv[i]);
|
||||||
|
|
||||||
if (interpret_branch_name(argv[i], &bname) != len)
|
|
||||||
strbuf_add(&bname, argv[i], len);
|
|
||||||
|
|
||||||
if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
|
if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
|
||||||
error("Cannot delete the branch '%s' "
|
error("Cannot delete the branch '%s' "
|
||||||
"which you are currently on.", bname.buf);
|
"which you are currently on.", bname.buf);
|
||||||
|
@ -353,16 +353,11 @@ struct branch_info {
|
|||||||
static void setup_branch_path(struct branch_info *branch)
|
static void setup_branch_path(struct branch_info *branch)
|
||||||
{
|
{
|
||||||
struct strbuf buf = STRBUF_INIT;
|
struct strbuf buf = STRBUF_INIT;
|
||||||
int ret;
|
|
||||||
|
|
||||||
if ((ret = interpret_branch_name(branch->name, &buf))
|
strbuf_branchname(&buf, branch->name);
|
||||||
&& ret == strlen(branch->name)) {
|
if (strcmp(buf.buf, branch->name))
|
||||||
branch->name = xstrdup(buf.buf);
|
branch->name = xstrdup(buf.buf);
|
||||||
strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
|
strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
|
||||||
} else {
|
|
||||||
strbuf_addstr(&buf, "refs/heads/");
|
|
||||||
strbuf_addstr(&buf, branch->name);
|
|
||||||
}
|
|
||||||
branch->path = strbuf_detach(&buf, NULL);
|
branch->path = strbuf_detach(&buf, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -360,9 +360,8 @@ static void merge_name(const char *remote, struct strbuf *msg)
|
|||||||
const char *ptr;
|
const char *ptr;
|
||||||
int len, early;
|
int len, early;
|
||||||
|
|
||||||
len = strlen(remote);
|
strbuf_branchname(&bname, remote);
|
||||||
if (interpret_branch_name(remote, &bname) == len)
|
remote = bname.buf;
|
||||||
remote = bname.buf;
|
|
||||||
|
|
||||||
memset(branch_head, 0, sizeof(branch_head));
|
memset(branch_head, 0, sizeof(branch_head));
|
||||||
remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
|
remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
|
||||||
|
9
strbuf.c
9
strbuf.c
@ -357,3 +357,12 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
|
|||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int strbuf_branchname(struct strbuf *sb, const char *name)
|
||||||
|
{
|
||||||
|
int len = strlen(name);
|
||||||
|
if (interpret_branch_name(name, sb) == len)
|
||||||
|
return 0;
|
||||||
|
strbuf_add(sb, name, len);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
2
strbuf.h
2
strbuf.h
@ -131,4 +131,6 @@ extern int strbuf_getline(struct strbuf *, FILE *, int);
|
|||||||
extern void stripspace(struct strbuf *buf, int skip_comments);
|
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);
|
||||||
|
|
||||||
#endif /* STRBUF_H */
|
#endif /* STRBUF_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user