Merge branch 'bf/set-head-symref'

When "git fetch $remote" notices that refs/remotes/$remote/HEAD is
missing and discovers what branch the other side points with its
HEAD, refs/remotes/$remote/HEAD is updated to point to it.

* bf/set-head-symref:
  fetch set_head: handle mirrored bare repositories
  fetch: set remote/HEAD if it does not exist
  refs: add create_only option to refs_update_symref_extended
  refs: add TRANSACTION_CREATE_EXISTS error
  remote set-head: better output for --auto
  remote set-head: refactor for readability
  refs: atomically record overwritten ref in update_symref
  refs: standardize output of refs_read_symbolic_ref
  t/t5505-remote: test failure of set-head
  t/t5505-remote: set default branch to main
This commit is contained in:
Junio C Hamano
2024-12-19 10:58:27 -08:00
18 changed files with 439 additions and 57 deletions

View File

@@ -1574,6 +1574,72 @@ static int backfill_tags(struct display_state *display_state,
return retcode;
}
static const char *strip_refshead(const char *name){
skip_prefix(name, "refs/heads/", &name);
return name;
}
static int set_head(const struct ref *remote_refs)
{
int result = 0, is_bare;
struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT;
const char *remote = gtransport->remote->name;
char *head_name = NULL;
struct ref *ref, *matches;
struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
struct refspec_item refspec = {
.force = 0,
.pattern = 1,
.src = (char *) "refs/heads/*",
.dst = (char *) "refs/heads/*",
};
struct string_list heads = STRING_LIST_INIT_DUP;
struct ref_store *refs = get_main_ref_store(the_repository);
get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
fetch_map, 1);
for (ref = matches; ref; ref = ref->next) {
string_list_append(&heads, strip_refshead(ref->name));
}
if (!heads.nr)
result = 1;
else if (heads.nr > 1)
result = 1;
else
head_name = xstrdup(heads.items[0].string);
if (!head_name)
goto cleanup;
is_bare = is_bare_repository();
if (is_bare) {
strbuf_addstr(&b_head, "HEAD");
strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
} else {
strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote);
strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote, head_name);
}
/* make sure it's valid */
if (!is_bare && !refs_ref_exists(refs, b_remote_head.buf)) {
result = 1;
goto cleanup;
}
if (refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
"fetch", NULL, !is_bare))
result = 1;
cleanup:
free(head_name);
free_refs(fetch_map);
free_refs(matches);
string_list_clear(&heads, 0);
strbuf_release(&b_head);
strbuf_release(&b_remote_head);
return result;
}
static int do_fetch(struct transport *transport,
struct refspec *rs,
const struct fetch_config *config)
@@ -1643,6 +1709,8 @@ static int do_fetch(struct transport *transport,
"refs/tags/");
}
strvec_push(&transport_ls_refs_options.ref_prefixes, "HEAD");
if (must_list_refs) {
trace2_region_enter("fetch", "remote_refs", the_repository);
remote_refs = transport_get_remote_refs(transport,
@@ -1787,6 +1855,12 @@ static int do_fetch(struct transport *transport,
"you need to specify exactly one branch with the --set-upstream option"));
}
}
if (set_head(remote_refs))
;
/*
* Way too many cases where this can go wrong
* so let's just fail silently for now.
*/
cleanup:
if (retcode) {