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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johannes Schindelin committed
Dec 11, 2020 at 11:36 UTC
cc0f13c57dedaf62c9f852b6bf363aee7e3392f1
5 files changed
+14
-11
builtin/clone.c
+1
-1
@@ -1323,7 +1323,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
1323
remote_head = NULL;
1324
option_no_checkout = 1;
1325
if (!option_bare) {
1326
- const char *branch = git_default_branch_name();
1326
+ const char *branch = git_default_branch_name(0);
1327
char *ref = xstrfmt("refs/heads/%s", branch);
1328
1329
install_branch_config(0, branch, remote_name, ref);
builtin/init-db.c
+5
-3
@@ -202,7 +202,8 @@ void initialize_repository_version(int hash_algo, int reinit)
202
static int create_default_files(const char *template_path,
203
const char *original_git_dir,
204
const char *initial_branch,
205
- const struct repository_format *fmt)
205
+ const struct repository_format *fmt,
206
+ int quiet)
207
{
208
struct stat st1;
209
struct strbuf buf = STRBUF_INIT;
@@ -267,7 +268,7 @@ static int create_default_files(const char *template_path,
268
char *ref;
269
270
if (!initial_branch)
270
- initial_branch = git_default_branch_name();
271
+ initial_branch = git_default_branch_name(quiet);
272
273
ref = xstrfmt("refs/heads/%s", initial_branch);
274
if (check_refname_format(ref, 0) < 0)
@@ -438,7 +439,8 @@ int init_db(const char *git_dir, const char *real_git_dir,
439
validate_hash_algorithm(&repo_fmt, hash);
440
441
reinit = create_default_files(template_dir, original_git_dir,
441
- initial_branch, &repo_fmt);
442
+ initial_branch, &repo_fmt,
443
+ flags & INIT_DB_QUIET);
444
if (reinit && initial_branch)
445
warning(_("re-init: ignored --initial-branch=%s"),
446
initial_branch);
refs.c
+3
-3
@@ -562,7 +562,7 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
562
strvec_pushf(prefixes, *p, len, prefix);
563
}
564
565
-char *repo_default_branch_name(struct repository *r)
565
+char *repo_default_branch_name(struct repository *r, int quiet)
566
{
567
const char *config_key = "init.defaultbranch";
568
const char *config_display_key = "init.defaultBranch";
@@ -585,12 +585,12 @@ char *repo_default_branch_name(struct repository *r)
585
return ret;
586
}
587
588
-const char *git_default_branch_name(void)
588
+const char *git_default_branch_name(int quiet)
589
{
590
static char *ret;
591
592
if (!ret)
593
- ret = repo_default_branch_name(the_repository);
593
+ ret = repo_default_branch_name(the_repository, quiet);
594
595
return ret;
596
}
refs.h
+2
-2
@@ -170,8 +170,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
170
* The return value of `repo_default_branch_name()` is an allocated string. The
171
* return value of `git_default_branch_name()` is a singleton.
172
*/
173
-const char *git_default_branch_name(void);
174
-char *repo_default_branch_name(struct repository *r);
173
+const char *git_default_branch_name(int quiet);
174
+char *repo_default_branch_name(struct repository *r, int quiet);
175
176
/*
177
* A ref_transaction represents a collection of reference updates that
remote.c
+3
-2
@@ -284,7 +284,7 @@ static void read_branches_file(struct remote *remote)
284
if (frag)
285
*(frag++) = '\0';
286
else
287
- frag = (char *)git_default_branch_name();
287
+ frag = (char *)git_default_branch_name(0);
288
289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -2206,7 +2206,8 @@ struct ref *guess_remote_head(const struct ref *head,
2206
2207
/* If a remote branch exists with the default branch name, let's use it. */
2208
if (!all) {
2209
- char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
2209
+ char *ref = xstrfmt("refs/heads/%s",
2210
+ git_default_branch_name(0));
2211
2212
r = find_ref_by_name(refs, ref);
2213
free(ref);