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

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>

Junio C Hamano committed Mar 21, 2009 at 13:17 UTC a552de75eb01f78046feaf7dc88e5e4833624ad5
6 files changed +18 -22
branch.c
+1 -6
@@ -134,13 +134,8 @@ void create_branch(const char *head,
134 char *real_ref, msg[PATH_MAX + 20];
135 struct strbuf ref = STRBUF_INIT;
136 int forcing = 0;
137 - int len;
137
139 - len = strlen(name);
140 - if (interpret_branch_name(name, &ref) != len) {
141 - strbuf_reset(&ref);
142 - strbuf_add(&ref, name, len);
143 - }
138 + strbuf_branchname(&ref, name);
139 strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
140
141 if (check_ref_format(ref.buf))
builtin-branch.c
+1 -5
@@ -121,11 +121,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
121 die("Couldn't look up commit object for HEAD");
122 }
123 for (i = 0; i < argc; i++, strbuf_release(&bname)) {
124 - int len = strlen(argv[i]);
125 -
126 - if (interpret_branch_name(argv[i], &bname) != len)
127 - strbuf_add(&bname, argv[i], len);
128 -
124 + strbuf_branchname(&bname, argv[i]);
125 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
126 error("Cannot delete the branch '%s' "
127 "which you are currently on.", bname.buf);
builtin-checkout.c
+3 -8
@@ -353,16 +353,11 @@ struct branch_info {
353 static void setup_branch_path(struct branch_info *branch)
354 {
355 struct strbuf buf = STRBUF_INIT;
356 - int ret;
356
358 - if ((ret = interpret_branch_name(branch->name, &buf))
359 - && ret == strlen(branch->name)) {
357 + strbuf_branchname(&buf, branch->name);
358 + if (strcmp(buf.buf, branch->name))
359 branch->name = xstrdup(buf.buf);
361 - strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
362 - } else {
363 - strbuf_addstr(&buf, "refs/heads/");
364 - strbuf_addstr(&buf, branch->name);
365 - }
360 + strbuf_splice(&buf, 0, 0, "refs/heads/", 11);
361 branch->path = strbuf_detach(&buf, NULL);
362 }
363
builtin-merge.c
+2 -3
@@ -360,9 +360,8 @@ static void merge_name(const char *remote, struct strbuf *msg)
360 const char *ptr;
361 int len, early;
362
363 - len = strlen(remote);
364 - if (interpret_branch_name(remote, &bname) == len)
365 - remote = bname.buf;
363 + strbuf_branchname(&bname, remote);
364 + remote = bname.buf;
365
366 memset(branch_head, 0, sizeof(branch_head));
367 remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);
strbuf.c
+9
@@ -357,3 +357,12 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
357
358 return len;
359 }
360 +
361 +int strbuf_branchname(struct strbuf *sb, const char *name)
362 +{
363 + int len = strlen(name);
364 + if (interpret_branch_name(name, sb) == len)
365 + return 0;
366 + strbuf_add(sb, name, len);
367 + return len;
368 +}
strbuf.h
+2
@@ -131,4 +131,6 @@ extern int strbuf_getline(struct strbuf *, FILE *, int);
131 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 +
136 #endif /* STRBUF_H */