From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by dcvr.yhbt.net (Postfix) with ESMTP id E44321F5AE for ; Tue, 23 Jun 2020 22:48:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388254AbgFWWrW (ORCPT ); Tue, 23 Jun 2020 18:47:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46728 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387634AbgFWWrT (ORCPT ); Tue, 23 Jun 2020 18:47:19 -0400 Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E65C0C061288 for ; Tue, 23 Jun 2020 15:33:38 -0700 (PDT) Received: by mail-wm1-x342.google.com with SMTP id 22so345898wmg.1 for ; Tue, 23 Jun 2020 15:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=xIGZgGWN6swPfoO12zG76ek2gRMFdIiQnieS7km2TVY=; b=DkiHOXXzk+7Cnpvn1o212li5daxLoeVk3NSs1GpPv//L8AXG2X/ulM8Nqn2X+cgZti F1vCbLhpzOuAMiT/sujMg9xpQ//3R8z1znVL7FkGjOi2Ls14K1kc+hkfdXBtu+K++T+0 rhqraMu2Bar1KzqYHoxH3fRRa35zxuSswTkI5FciJ3vvg/6qGym8ABa18w7Wf6H6zxK5 Siok1PjpQ0qg/utthsXy0JgOK8GRg8Xmx04dWGVuf893RfZeI3QOzqX5H17U80XdlgFN +Yr3CP1XQFep/ploi6uq2hjDXWk/p2GkPJn2EE2n9CmJK1spJtCl9Uct3qVfQ3PmlQx4 QIMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=xIGZgGWN6swPfoO12zG76ek2gRMFdIiQnieS7km2TVY=; b=F//38ApGI1QEtsGJMp/vgtaM8W2aBeGLU9wByP2lCgLKXV29MaJIqAUhUfl22z4u7R H6sqFDqoU65NgXYHZ8OdnsQnrkfJUW9NQQlmvaq7u3RMGLe183sYACKGcd1aHq9JMPUe VYyw+guq9xebHVyEeR4fVLaAmS3JURw5wzi68Q2bKr3Iwb2SZfPvi/u6wtQiJWxhqGV3 3vHpKrtKTTEHWTxbX4jByCiBkK/qAWrcRkTdzSMQp/Qa4hTb+gg62ICzwfkE17j+C37r IoWR2mSpv316fzt3uxJ5bEK9Jc9bHtAN9AIz8YModFFeVnL7qM3D0kJBPitFN3YNoCvp bm4A== X-Gm-Message-State: AOAM531JzQIRHRTgbqo1wBmKXoD/7/E0mpkCw6f3PxVaOMMNIHbKOVxk zc8YIwlu582jJmjQjJg38QenyUK8 X-Google-Smtp-Source: ABdhPJyBdunIwf0saQOH2xYte4LMOVfbA9iNGhkVOdtTpaP2LLZhEhMUd8Otb7FRMPSPfOJwlOqKgw== X-Received: by 2002:a05:600c:228e:: with SMTP id 14mr14194497wmf.44.1592951617476; Tue, 23 Jun 2020 15:33:37 -0700 (PDT) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z9sm5340154wmi.7.2020.06.23.15.33.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jun 2020 15:33:36 -0700 (PDT) Message-Id: In-Reply-To: References: From: "Don Goodman-Wilson via GitGitGadget" Date: Tue, 23 Jun 2020 22:33:27 +0000 Subject: [PATCH v3 5/8] init: allow setting the default for the initial branch name via the config Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MIME-Version: 1.0 To: git@vger.kernel.org Cc: don@goodman-wilson.com, stolee@gmail.com, peff@peff.net, sandals@crustytoothpaste.net, Matt Rogers , Eric Sunshine , Taylor Blau , Phillip Wood , Alban Gruin , Johannes Sixt , Denton Liu , =?UTF-8?Q?=C3=86var_Arnfj=C3=B6r=C3=B0?= Bjarmason , Johannes Schindelin , Don Goodman-Wilson Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Don Goodman-Wilson We just introduced the command-line option `--initial-branch=` to allow initializing a new repository with a different initial branch than the hard-coded one. To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each and every `git init` invocation), let's introduce the `init.defaultBranch` config setting. Helped-by: Johannes Schindelin Helped-by: Derrick Stolee Signed-off-by: Don Goodman-Wilson --- Documentation/config/init.txt | 4 ++++ builtin/init-db.c | 2 +- refs.c | 30 ++++++++++++++++++++++++++++++ refs.h | 9 +++++++++ t/t0001-init.sh | 13 +++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) diff --git a/Documentation/config/init.txt b/Documentation/config/init.txt index 46fa8c6a08..6ae4a38416 100644 --- a/Documentation/config/init.txt +++ b/Documentation/config/init.txt @@ -1,3 +1,7 @@ init.templateDir:: Specify the directory from which templates will be copied. (See the "TEMPLATE DIRECTORY" section of linkgit:git-init[1].) + +init.defaultBranch:: + Allows overriding the default branch name when initializing + a new repository. diff --git a/builtin/init-db.c b/builtin/init-db.c index a898153901..b8634b5f35 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -269,7 +269,7 @@ static int create_default_files(const char *template_path, char *ref; if (!initial_branch) - initial_branch = "master"; + initial_branch = git_default_branch_name(); ref = xstrfmt("refs/heads/%s", initial_branch); if (check_refname_format(ref, 0) < 0) diff --git a/refs.c b/refs.c index 224ff66c7b..b98dea5217 100644 --- a/refs.c +++ b/refs.c @@ -560,6 +560,36 @@ void expand_ref_prefix(struct argv_array *prefixes, const char *prefix) argv_array_pushf(prefixes, *p, len, prefix); } +char *repo_default_branch_name(struct repository *r) +{ + const char *config_key = "init.defaultbranch"; + const char *config_display_key = "init.defaultBranch"; + char *ret = NULL, *full_ref; + + if (repo_config_get_string(r, config_key, &ret) < 0) + die(_("could not retrieve `%s`"), config_display_key); + + if (!ret) + ret = xstrdup("master"); + + full_ref = xstrfmt("refs/heads/%s", ret); + if (check_refname_format(full_ref, 0)) + die(_("invalid branch name: %s = %s"), config_display_key, ret); + free(full_ref); + + return ret; +} + +const char *git_default_branch_name(void) +{ + static char *ret; + + if (!ret) + ret = repo_default_branch_name(the_repository); + + return ret; +} + /* * *string and *len will only be substituted, and *string returned (for * later free()ing) if the string passed in is a magic short-hand form diff --git a/refs.h b/refs.h index e010f8aec2..f212f8945e 100644 --- a/refs.h +++ b/refs.h @@ -154,6 +154,15 @@ int repo_dwim_log(struct repository *r, const char *str, int len, struct object_ int dwim_ref(const char *str, int len, struct object_id *oid, char **ref); int dwim_log(const char *str, int len, struct object_id *oid, char **ref); +/* + * Retrieves the default branch name for newly-initialized repositories. + * + * The return value of `repo_default_branch_name()` is an allocated string. The + * return value of `git_default_branch_name()` is a singleton. + */ +const char *git_default_branch_name(void); +char *repo_default_branch_name(struct repository *r); + /* * A ref_transaction represents a collection of reference updates that * should succeed or fail together. diff --git a/t/t0001-init.sh b/t/t0001-init.sh index 61837ca25f..047197d08f 100755 --- a/t/t0001-init.sh +++ b/t/t0001-init.sh @@ -477,4 +477,17 @@ test_expect_success '--initial-branch' ' grep hello actual ' +test_expect_success 'overridden default initial branch name (config)' ' + test_config_global init.defaultBranch nmb && + git init initial-branch-config && + git -C initial-branch-config symbolic-ref HEAD >actual && + grep nmb actual +' + +test_expect_success 'invalid default branch name' ' + test_config_global init.defaultBranch "with space" && + test_must_fail git init initial-branch-invalid 2>err && + test_i18ngrep "invalid branch name" err +' + test_done -- gitgitgadget