Skip to content

Commit 255073c

Browse files
sivaraamgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a48ebe9 commit 255073c

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

builtin/branch.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
462462
{
463463
struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
464464
struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
465+
const char *interpreted_oldname = NULL;
466+
const char *interpreted_newname = NULL;
465467
int recovery = 0;
466468
int clobber_head_ok;
467469

@@ -493,6 +495,11 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
493495

494496
reject_rebase_or_bisect_branch(oldref.buf);
495497

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+
496503
if (copy)
497504
strbuf_addf(&logmsg, "Branch: copied %s to %s",
498505
oldref.buf, newref.buf);
@@ -508,10 +515,10 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
508515
if (recovery) {
509516
if (copy)
510517
warning(_("Created a copy of a misnamed branch '%s'"),
511-
oldref.buf + 11);
518+
interpreted_oldname);
512519
else
513520
warning(_("Renamed a misnamed branch '%s' away"),
514-
oldref.buf + 11);
521+
interpreted_oldname);
515522
}
516523

517524
if (!copy &&
@@ -520,9 +527,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
520527

521528
strbuf_release(&logmsg);
522529

523-
strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
530+
strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
524531
strbuf_release(&oldref);
525-
strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
532+
strbuf_addf(&newsection, "branch.%s", interpreted_newname);
526533
strbuf_release(&newref);
527534
if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
528535
die(_("Branch is renamed, but update of config-file failed"));

0 commit comments

Comments
 (0)