summaryrefslogtreecommitdiff
path: root/remote.c
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2020-06-24 14:46:35 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-06-24 16:14:21 (GMT)
commita471214bd610bb415a37d5b2c3506b83549acd57 (patch)
treeda4811bd343763fb77d4166a9802e6fdb39bf4ba /remote.c
parent0cc1b475bbcc530c5429c741fbcfdc2730d27223 (diff)
downloadgit-a471214bd610bb415a37d5b2c3506b83549acd57.zip
git-a471214bd610bb415a37d5b2c3506b83549acd57.tar.gz
git-a471214bd610bb415a37d5b2c3506b83549acd57.tar.bz2
remote: use the configured default branch name when appropriate
When guessing the default branch name of a remote, and there are no refs to guess from, we want to go with the preference specified by the user for the fall-back, i.e. the default name to be used for the initial branch of new repositories (because as far as the user is concerned, a remote that has no branches yet is a new repository). At the same time, when talking to an older Git server that does not report a symref for `HEAD` (but instead reports a commit hash), let's try to guess the configured default branch name first. If it does not match the reported commit hash, let's fall back to `master` as before. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'remote.c')
-rw-r--r--remote.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/remote.c b/remote.c
index 534c642..bc46413 100644
--- a/remote.c
+++ b/remote.c
@@ -276,7 +276,7 @@ static void read_branches_file(struct remote *remote)
/*
* The branches file would have URL and optionally
- * #branch specified. The "master" (or specified) branch is
+ * #branch specified. The default (or specified) branch is
* fetched and stored in the local branch matching the
* remote name.
*/
@@ -284,7 +284,7 @@ static void read_branches_file(struct remote *remote)
if (frag)
*(frag++) = '\0';
else
- frag = "master";
+ frag = (char *)git_default_branch_name();
add_url_alias(remote, strbuf_detach(&buf, NULL));
strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
@@ -2097,8 +2097,16 @@ struct ref *guess_remote_head(const struct ref *head,
if (head->symref)
return copy_ref(find_ref_by_name(refs, head->symref));
- /* If refs/heads/master could be right, it is. */
+ /* If a remote branch exists with the default branch name, let's use it. */
if (!all) {
+ char *ref = xstrfmt("refs/heads/%s", git_default_branch_name());
+
+ r = find_ref_by_name(refs, ref);
+ free(ref);
+ if (r && oideq(&r->old_oid, &head->old_oid))
+ return copy_ref(r);
+
+ /* Fall back to the hard-coded historical default */
r = find_ref_by_name(refs, "refs/heads/master");
if (r && oideq(&r->old_oid, &head->old_oid))
return copy_ref(r);