From cfaff3aac8063ec72f03c6761328c7fa44a15b34 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 11 Dec 2020 11:36:55 +0000 Subject: 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 ` worked in a freshly initialized repository without any commits. Let's make it so. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin/branch.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'builtin') diff --git a/builtin/branch.c b/builtin/branch.c index efb30b8820..200da319f1 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -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")); -- cgit v1.2.3 From cc0f13c57dedaf62c9f852b6bf363aee7e3392f1 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Fri, 11 Dec 2020 11:36:56 +0000 Subject: get_default_branch_name(): prepare for showing some advice We are about to introduce a message giving users running `git init` some advice about `init.defaultBranch`. This will necessarily be done in `repo_default_branch_name()`. Not all code paths want to show that advice, though. In particular, the `git clone` codepath _specifically_ asks for `init_db()` to be quiet, via the `INIT_DB_QUIET` flag. In preparation for showing users above-mentioned advice, let's change the function signature of `get_default_branch_name()` to accept the parameter `quiet`. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- builtin/clone.c | 2 +- builtin/init-db.c | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'builtin') diff --git a/builtin/clone.c b/builtin/clone.c index a0841923cf..64b1784011 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -1323,7 +1323,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) remote_head = NULL; option_no_checkout = 1; if (!option_bare) { - const char *branch = git_default_branch_name(); + const char *branch = git_default_branch_name(0); char *ref = xstrfmt("refs/heads/%s", branch); install_branch_config(0, branch, remote_name, ref); diff --git a/builtin/init-db.c b/builtin/init-db.c index 01bc648d41..dcc45bef51 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -202,7 +202,8 @@ void initialize_repository_version(int hash_algo, int reinit) static int create_default_files(const char *template_path, const char *original_git_dir, const char *initial_branch, - const struct repository_format *fmt) + const struct repository_format *fmt, + int quiet) { struct stat st1; struct strbuf buf = STRBUF_INIT; @@ -267,7 +268,7 @@ static int create_default_files(const char *template_path, char *ref; if (!initial_branch) - initial_branch = git_default_branch_name(); + initial_branch = git_default_branch_name(quiet); ref = xstrfmt("refs/heads/%s", initial_branch); if (check_refname_format(ref, 0) < 0) @@ -438,7 +439,8 @@ int init_db(const char *git_dir, const char *real_git_dir, validate_hash_algorithm(&repo_fmt, hash); reinit = create_default_files(template_dir, original_git_dir, - initial_branch, &repo_fmt); + initial_branch, &repo_fmt, + flags & INIT_DB_QUIET); if (reinit && initial_branch) warning(_("re-init: ignored --initial-branch=%s"), initial_branch); -- cgit v1.2.3