branch -m: allow renaming a yet-unborn branch
In one of the next commits, we would like to give users some advice regarding the initial branch name, and how to modify it. To that end, it would be good if `git branch -m <name>` worked in a freshly initialized repository without any commits. Let's make it so. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Dec 11, 2020 at 11:36 UTC
cfaff3aac8063ec72f03c6761328c7fa44a15b34
2 files changed
+11
-1
builtin/branch.c
+3
-1
@@ -538,7 +538,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
538
strbuf_addf(&logmsg, "Branch: renamed %s to %s",
539
oldref.buf, newref.buf);
540
541
- if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
541
+ if (!copy &&
542
+ (!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
543
+ rename_ref(oldref.buf, newref.buf, logmsg.buf))
544
die(_("Branch rename failed"));
545
if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
546
die(_("Branch copy failed"));
t/t0001-init.sh
+8
@@ -571,4 +571,12 @@ test_expect_success 'invalid default branch name' '
571
test_i18ngrep "invalid branch name" err
572
'
573
574
+test_expect_success 'branch -m with the initial branch' '
575
+ git init rename-initial &&
576
+ git -C rename-initial branch -m renamed &&
577
+ test renamed = $(git -C rename-initial symbolic-ref --short HEAD) &&
578
+ git -C rename-initial branch -m renamed again &&
579
+ test again = $(git -C rename-initial symbolic-ref --short HEAD)
580
+'
581
+
582
test_done