builtin/branch: strip refs/heads/ using skip_prefix

Instead of hard-coding the offset strlen("refs/heads/") to skip the prefix "refs/heads/" use the skip_prefix() function which is more communicative and verifies that the string actually starts with that prefix. Signed-off-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kaartic Sivaraam committed Dec 1, 2017 at 11:29 UTC 255073ca59de7cd293e4a9d6c12831b6e505c37e
1 file changed +11 -4
builtin/branch.c
+11 -4
@@ -462,6 +462,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
462 {
463 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
464 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
465 + const char *interpreted_oldname = NULL;
466 + const char *interpreted_newname = NULL;
467 int recovery = 0;
468 int clobber_head_ok;
469
@@ -493,6 +495,11 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
495
496 reject_rebase_or_bisect_branch(oldref.buf);
497
498 + if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
499 + !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
500 + die("BUG: expected prefix missing for refs");
501 + }
502 +
503 if (copy)
504 strbuf_addf(&logmsg, "Branch: copied %s to %s",
505 oldref.buf, newref.buf);
@@ -508,10 +515,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
515 if (recovery) {
516 if (copy)
517 warning(_("Created a copy of a misnamed branch '%s'"),
511 - oldref.buf + 11);
518 + interpreted_oldname);
519 else
520 warning(_("Renamed a misnamed branch '%s' away"),
514 - oldref.buf + 11);
521 + interpreted_oldname);
522 }
523
524 if (!copy &&
@@ -520,9 +527,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
527
528 strbuf_release(&logmsg);
529
523 - strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
530 + strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
531 strbuf_release(&oldref);
525 - strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
532 + strbuf_addf(&newsection, "branch.%s", interpreted_newname);
533 strbuf_release(&newref);
534 if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
535 die(_("Branch is renamed, but update of config-file failed"));