Skip to content

Commit 7ccc94f

Browse files
committed
check-ref-format --branch: strip refs/heads/ using skip_prefix
The expansion returned from strbuf_check_branch_ref always starts with "refs/heads/" by construction, but there is nothing about its name or advertised API making that obvious. This command is used to process human-supplied input from the command line and is usually not the inner loop, so we can spare some cycles to be more defensive. Instead of hard-coding the offset strlen("refs/heads/") to skip, verify that the expansion actually starts with refs/heads/. [jn: split out from a larger patch, added explanation] Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c3f847 commit 7ccc94f

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

builtin/check-ref-format.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ static char *collapse_slashes(const char *refname)
3939
static int check_ref_format_branch(const char *arg)
4040
{
4141
struct strbuf sb = STRBUF_INIT;
42+
const char *name;
4243
int nongit;
4344

4445
setup_git_directory_gently(&nongit);
45-
if (strbuf_check_branch_ref(&sb, arg))
46+
if (strbuf_check_branch_ref(&sb, arg) ||
47+
!skip_prefix(sb.buf, "refs/heads/", &name))
4648
die("'%s' is not a valid branch name", arg);
47-
printf("%s\n", sb.buf + 11);
49+
printf("%s\n", name);
4850
return 0;
4951
}
5052

0 commit comments

Comments
 (0)