Skip to content

Commit cc0f13c

Browse files
dschogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cfaff3a commit cc0f13c

File tree

5 files changed

+14
-11
lines changed

5 files changed

+14
-11
lines changed

builtin/clone.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
13231323
remote_head = NULL;
13241324
option_no_checkout = 1;
13251325
if (!option_bare) {
1326-
const char *branch = git_default_branch_name();
1326+
const char *branch = git_default_branch_name(0);
13271327
char *ref = xstrfmt("refs/heads/%s", branch);
13281328

13291329
install_branch_config(0, branch, remote_name, ref);

builtin/init-db.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ void initialize_repository_version(int hash_algo, int reinit)
202202
static int create_default_files(const char *template_path,
203203
const char *original_git_dir,
204204
const char *initial_branch,
205-
const struct repository_format *fmt)
205+
const struct repository_format *fmt,
206+
int quiet)
206207
{
207208
struct stat st1;
208209
struct strbuf buf = STRBUF_INIT;
@@ -267,7 +268,7 @@ static int create_default_files(const char *template_path,
267268
char *ref;
268269

269270
if (!initial_branch)
270-
initial_branch = git_default_branch_name();
271+
initial_branch = git_default_branch_name(quiet);
271272

272273
ref = xstrfmt("refs/heads/%s", initial_branch);
273274
if (check_refname_format(ref, 0) < 0)
@@ -438,7 +439,8 @@ int init_db(const char *git_dir, const char *real_git_dir,
438439
validate_hash_algorithm(&repo_fmt, hash);
439440

440441
reinit = create_default_files(template_dir, original_git_dir,
441-
initial_branch, &repo_fmt);
442+
initial_branch, &repo_fmt,
443+
flags & INIT_DB_QUIET);
442444
if (reinit && initial_branch)
443445
warning(_("re-init: ignored --initial-branch=%s"),
444446
initial_branch);

refs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ void expand_ref_prefix(struct strvec *prefixes, const char *prefix)
562562
strvec_pushf(prefixes, *p, len, prefix);
563563
}
564564

565-
char *repo_default_branch_name(struct repository *r)
565+
char *repo_default_branch_name(struct repository *r, int quiet)
566566
{
567567
const char *config_key = "init.defaultbranch";
568568
const char *config_display_key = "init.defaultBranch";
@@ -585,12 +585,12 @@ char *repo_default_branch_name(struct repository *r)
585585
return ret;
586586
}
587587

588-
const char *git_default_branch_name(void)
588+
const char *git_default_branch_name(int quiet)
589589
{
590590
static char *ret;
591591

592592
if (!ret)
593-
ret = repo_default_branch_name(the_repository);
593+
ret = repo_default_branch_name(the_repository, quiet);
594594

595595
return ret;
596596
}

refs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **ref);
170170
* The return value of `repo_default_branch_name()` is an allocated string. The
171171
* return value of `git_default_branch_name()` is a singleton.
172172
*/
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);
175175

176176
/*
177177
* A ref_transaction represents a collection of reference updates that

remote.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ static void read_branches_file(struct remote *remote)
284284
if (frag)
285285
*(frag++) = '\0';
286286
else
287-
frag = (char *)git_default_branch_name();
287+
frag = (char *)git_default_branch_name(0);
288288

289289
add_url_alias(remote, strbuf_detach(&buf, NULL));
290290
refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s",
@@ -2206,7 +2206,8 @@ struct ref *guess_remote_head(const struct ref *head,
22062206

22072207
/* If a remote branch exists with the default branch name, let's use it. */
22082208
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));
22102211

22112212
r = find_ref_by_name(refs, ref);
22122213
free(ref);

0 commit comments

Comments
 (0)