Skip to content

Commit e3f065e

Browse files
committed
remote: update the default remote branch
When the remote does not tell us its default, we have to guess what the default branch should be. Use our local initial branch configuration to inform the remote branch default when we clone.
1 parent cd2f74d commit e3f065e

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

src/remote.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2375,29 +2375,36 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
23752375
const git_remote_head *guess = NULL;
23762376
const git_oid *head_id;
23772377
size_t heads_len, i;
2378+
git_buf local_default = GIT_BUF_INIT;
23782379
int error;
23792380

23802381
assert(out);
23812382

23822383
if ((error = git_remote_ls(&heads, &heads_len, remote)) < 0)
2383-
return error;
2384-
2385-
if (heads_len == 0)
2386-
return GIT_ENOTFOUND;
2384+
goto done;
23872385

2388-
if (strcmp(heads[0]->name, GIT_HEAD_FILE))
2389-
return GIT_ENOTFOUND;
2386+
if (heads_len == 0 || strcmp(heads[0]->name, GIT_HEAD_FILE)) {
2387+
error = GIT_ENOTFOUND;
2388+
goto done;
2389+
}
23902390

23912391
git_buf_sanitize(out);
2392+
23922393
/* the first one must be HEAD so if that has the symref info, we're done */
2393-
if (heads[0]->symref_target)
2394-
return git_buf_puts(out, heads[0]->symref_target);
2394+
if (heads[0]->symref_target) {
2395+
error = git_buf_puts(out, heads[0]->symref_target);
2396+
goto done;
2397+
}
23952398

23962399
/*
23972400
* If there's no symref information, we have to look over them
2398-
* and guess. We return the first match unless the master
2399-
* branch is a candidate. Then we return the master branch.
2401+
* and guess. We return the first match unless the default
2402+
* branch is a candidate. Then we return the default branch.
24002403
*/
2404+
2405+
if ((error = git_repository_initialbranch(&local_default, remote->repo)) < 0)
2406+
goto done;
2407+
24012408
head_id = &heads[0]->oid;
24022409

24032410
for (i = 1; i < heads_len; i++) {
@@ -2412,16 +2419,22 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
24122419
continue;
24132420
}
24142421

2415-
if (!git__strcmp(GIT_REFS_HEADS_MASTER_FILE, heads[i]->name)) {
2422+
if (!git__strcmp(local_default.ptr, heads[i]->name)) {
24162423
guess = heads[i];
24172424
break;
24182425
}
24192426
}
24202427

2421-
if (!guess)
2422-
return GIT_ENOTFOUND;
2428+
if (!guess) {
2429+
error = GIT_ENOTFOUND;
2430+
goto done;
2431+
}
2432+
2433+
error = git_buf_puts(out, guess->name);
24232434

2424-
return git_buf_puts(out, guess->name);
2435+
done:
2436+
git_buf_dispose(&local_default);
2437+
return error;
24252438
}
24262439

24272440
int git_remote_upload(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts)

0 commit comments

Comments
 (0)