check-ref-format --branch: strip refs/heads/ using skip_prefix
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Oct 2017 07:10:00 +0000 (00:10 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Oct 2017 21:12:01 +0000 (06:12 +0900)
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 <gitster@pobox.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/check-ref-format.c
index eac499450f63554387fb8b32ef5780c32ded4a21..737e5ecba3158cc133dacf0dc90409070aa955fa 100644 (file)
@@ -39,12 +39,14 @@ static char *collapse_slashes(const char *refname)
 static int check_ref_format_branch(const char *arg)
 {
        struct strbuf sb = STRBUF_INIT;
+       const char *name;
        int nongit;
 
        setup_git_directory_gently(&nongit);
-       if (strbuf_check_branch_ref(&sb, arg))
+       if (strbuf_check_branch_ref(&sb, arg) ||
+           !skip_prefix(sb.buf, "refs/heads/", &name))
                die("'%s' is not a valid branch name", arg);
-       printf("%s\n", sb.buf + 11);
+       printf("%s\n", name);
        return 0;
 }