Skip to content

Commit a552de7

Browse files
committed
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 <[email protected]>
1 parent 431b196 commit a552de7

File tree

6 files changed

+18
-22
lines changed

6 files changed

+18
-22
lines changed

branch.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,8 @@ void create_branch(const char *head,
134134
char *real_ref, msg[PATH_MAX + 20];
135135
struct strbuf ref = STRBUF_INIT;
136136
int forcing = 0;
137-
int len;
138137

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);
144139
strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
145140

146141
if (check_ref_format(ref.buf))

builtin-branch.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds)
121121
die("Couldn't look up commit object for HEAD");
122122
}
123123
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]);
129125
if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
130126
error("Cannot delete the branch '%s' "
131127
"which you are currently on.", bname.buf);

builtin-checkout.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,11 @@ struct branch_info {
353353
static void setup_branch_path(struct branch_info *branch)
354354
{
355355
struct strbuf buf = STRBUF_INIT;
356-
int ret;
357356

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))
360359
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);
366361
branch->path = strbuf_detach(&buf, NULL);
367362
}
368363

builtin-merge.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,8 @@ static void merge_name(const char *remote, struct strbuf *msg)
360360
const char *ptr;
361361
int len, early;
362362

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;
366365

367366
memset(branch_head, 0, sizeof(branch_head));
368367
remote_head = peel_to_type(remote, 0, NULL, OBJ_COMMIT);

strbuf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,12 @@ int strbuf_read_file(struct strbuf *sb, const char *path, size_t hint)
357357

358358
return len;
359359
}
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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,6 @@ extern int strbuf_getline(struct strbuf *, FILE *, int);
131131
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

134+
extern int strbuf_branchname(struct strbuf *sb, const char *name);
135+
134136
#endif /* STRBUF_H */

0 commit comments

Comments
 (0)