]> Kevux Git Server - rit/commitdiff
branch -m: allow renaming a yet-unborn branch
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Fri, 11 Dec 2020 11:36:55 +0000 (11:36 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 13 Dec 2020 23:53:50 +0000 (15:53 -0800)
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>
builtin/branch.c
t/t0001-init.sh

index efb30b8..200da31 100644 (file)
@@ -538,7 +538,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
                strbuf_addf(&logmsg, "Branch: renamed %s to %s",
                            oldref.buf, newref.buf);
 
-       if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
+       if (!copy &&
+           (!head || strcmp(oldname, head) || !is_null_oid(&head_oid)) &&
+           rename_ref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch rename failed"));
        if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
                die(_("Branch copy failed"));
index 69a3204..bb23e56 100755 (executable)
@@ -571,4 +571,12 @@ test_expect_success 'invalid default branch name' '
        test_i18ngrep "invalid branch name" err
 '
 
+test_expect_success 'branch -m with the initial branch' '
+       git init rename-initial &&
+       git -C rename-initial branch -m renamed &&
+       test renamed = $(git -C rename-initial symbolic-ref --short HEAD) &&
+       git -C rename-initial branch -m renamed again &&
+       test again = $(git -C rename-initial symbolic-ref --short HEAD)
+'
+
 test_done