Skip to content

Commit

Permalink
fetch-pack: put shallow info in output parameter
Browse files Browse the repository at this point in the history
Expand the transport fetch method signature, by adding an output
parameter, to allow transports to return information about the refs they
have fetched.  Then communicate shallow status information through this
mechanism instead of by modifying the input list of refs.

This does require clients to sometimes generate the ref map twice: once
from the list of refs provided by the remote (as is currently done) and
potentially once from the new list of refs that the fetch mechanism
provides.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
bmwill authored and gitster committed Jun 28, 2018
1 parent 6d1700d commit 989b8c4
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 24 deletions.
4 changes: 2 additions & 2 deletions builtin/clone.c
Expand Up @@ -1155,7 +1155,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
}

if (!is_local && !complete_refs_before_fetch)
transport_fetch_refs(transport, mapped_refs);
transport_fetch_refs(transport, mapped_refs, NULL);

remote_head = find_ref_by_name(refs, "HEAD");
remote_head_points_at =
Expand Down Expand Up @@ -1197,7 +1197,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
if (is_local)
clone_local(path, git_dir);
else if (refs && complete_refs_before_fetch)
transport_fetch_refs(transport, mapped_refs);
transport_fetch_refs(transport, mapped_refs, NULL);

update_remote_refs(refs, mapped_refs, remote_head_points_at,
branch_top.buf, reflog_msg.buf, transport,
Expand Down
28 changes: 24 additions & 4 deletions builtin/fetch.c
Expand Up @@ -946,11 +946,13 @@ static int quickfetch(struct ref *ref_map)
return check_connected(iterate_ref_map, &rm, &opt);
}

static int fetch_refs(struct transport *transport, struct ref *ref_map)
static int fetch_refs(struct transport *transport, struct ref *ref_map,
struct ref **updated_remote_refs)
{
int ret = quickfetch(ref_map);
if (ret)
ret = transport_fetch_refs(transport, ref_map);
ret = transport_fetch_refs(transport, ref_map,
updated_remote_refs);
if (!ret)
/*
* Keep the new pack's ".keep" file around to allow the caller
Expand Down Expand Up @@ -1112,7 +1114,7 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
transport_set_option(transport, TRANS_OPT_DEPTH, "0");
transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
if (!fetch_refs(transport, ref_map))
if (!fetch_refs(transport, ref_map, NULL))
consume_refs(transport, ref_map);

if (gsecondary) {
Expand All @@ -1128,6 +1130,7 @@ static int do_fetch(struct transport *transport,
int autotags = (transport->remote->fetch_tags == 1);
int retcode = 0;
const struct ref *remote_refs;
struct ref *updated_remote_refs = NULL;
struct argv_array ref_prefixes = ARGV_ARRAY_INIT;

if (tags == TAGS_DEFAULT) {
Expand Down Expand Up @@ -1178,7 +1181,24 @@ static int do_fetch(struct transport *transport,
transport->url);
}
}
if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {

if (fetch_refs(transport, ref_map, &updated_remote_refs)) {
free_refs(ref_map);
retcode = 1;
goto cleanup;
}
if (updated_remote_refs) {
/*
* Regenerate ref_map using the updated remote refs. This is
* to account for additional information which may be provided
* by the transport (e.g. shallow info).
*/
free_refs(ref_map);
ref_map = get_ref_map(transport->remote, updated_remote_refs, rs,
tags, &autotags);
free_refs(updated_remote_refs);
}
if (consume_refs(transport, ref_map)) {
free_refs(ref_map);
retcode = 1;
goto cleanup;
Expand Down
2 changes: 1 addition & 1 deletion fetch-object.c
Expand Up @@ -19,7 +19,7 @@ static void fetch_refs(const char *remote_name, struct ref *ref)

transport_set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
transport_set_option(transport, TRANS_OPT_NO_DEPENDENTS, "1");
transport_fetch_refs(transport, ref);
transport_fetch_refs(transport, ref, NULL);
fetch_if_missing = original_fetch_if_missing;
}

Expand Down
15 changes: 8 additions & 7 deletions fetch-pack.c
Expand Up @@ -1470,12 +1470,13 @@ static int remove_duplicates_in_refs(struct ref **ref, int nr)
}

static void update_shallow(struct fetch_pack_args *args,
struct ref **sought, int nr_sought,
struct ref *refs,
struct shallow_info *si)
{
struct oid_array ref = OID_ARRAY_INIT;
int *status;
int i;
struct ref *r;

if (args->deepen && alternate_shallow_file) {
if (*alternate_shallow_file == '\0') { /* --unshallow */
Expand Down Expand Up @@ -1517,8 +1518,8 @@ static void update_shallow(struct fetch_pack_args *args,
remove_nonexistent_theirs_shallow(si);
if (!si->nr_ours && !si->nr_theirs)
return;
for (i = 0; i < nr_sought; i++)
oid_array_append(&ref, &sought[i]->old_oid);
for (r = refs; r; r = r->next)
oid_array_append(&ref, &r->old_oid);
si->ref = &ref;

if (args->update_shallow) {
Expand Down Expand Up @@ -1552,12 +1553,12 @@ static void update_shallow(struct fetch_pack_args *args,
* remote is also shallow, check what ref is safe to update
* without updating .git/shallow
*/
status = xcalloc(nr_sought, sizeof(*status));
status = xcalloc(ref.nr, sizeof(*status));
assign_shallow_commits_to_refs(si, NULL, status);
if (si->nr_ours || si->nr_theirs) {
for (i = 0; i < nr_sought; i++)
for (r = refs, i = 0; r; r = r->next, i++)
if (status[i])
sought[i]->status = REF_STATUS_REJECT_SHALLOW;
r->status = REF_STATUS_REJECT_SHALLOW;
}
free(status);
oid_array_clear(&ref);
Expand Down Expand Up @@ -1591,7 +1592,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
ref_cpy = do_fetch_pack(args, fd, ref, sought, nr_sought,
&si, pack_lockfile);
reprepare_packed_git(the_repository);
update_shallow(args, sought, nr_sought, &si);
update_shallow(args, ref_cpy, &si);
clear_shallow_info(&si);
return ref_cpy;
}
Expand Down
6 changes: 4 additions & 2 deletions transport-helper.c
Expand Up @@ -651,14 +651,16 @@ static int connect_helper(struct transport *transport, const char *name,
}

static int fetch(struct transport *transport,
int nr_heads, struct ref **to_fetch)
int nr_heads, struct ref **to_fetch,
struct ref **fetched_refs)
{
struct helper_data *data = transport->data;
int i, count;

if (process_connect(transport, 0)) {
do_take_over(transport);
return transport->vtable->fetch(transport, nr_heads, to_fetch);
return transport->vtable->fetch(transport, nr_heads, to_fetch,
fetched_refs);
}

count = 0;
Expand Down
9 changes: 8 additions & 1 deletion transport-internal.h
Expand Up @@ -36,11 +36,18 @@ struct transport_vtable {
* Fetch the objects for the given refs. Note that this gets
* an array, and should ignore the list structure.
*
* The transport *may* provide, in fetched_refs, the list of refs that
* it fetched. If the transport knows anything about the fetched refs
* that the caller does not know (for example, shallow status), it
* should provide that list of refs and include that information in the
* list.
*
* If the transport did not get hashes for refs in
* get_refs_list(), it should set the old_sha1 fields in the
* provided refs now.
**/
int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs);
int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs,
struct ref **fetched_refs);

/**
* Push the objects and refs. Send the necessary objects, and
Expand Down
34 changes: 28 additions & 6 deletions transport.c
Expand Up @@ -151,7 +151,8 @@ static struct ref *get_refs_from_bundle(struct transport *transport,
}

static int fetch_refs_from_bundle(struct transport *transport,
int nr_heads, struct ref **to_fetch)
int nr_heads, struct ref **to_fetch,
struct ref **fetched_refs)
{
struct bundle_transport_data *data = transport->data;
return unbundle(&data->header, data->fd,
Expand Down Expand Up @@ -287,7 +288,8 @@ static struct ref *get_refs_via_connect(struct transport *transport, int for_pus
}

static int fetch_refs_via_pack(struct transport *transport,
int nr_heads, struct ref **to_fetch)
int nr_heads, struct ref **to_fetch,
struct ref **fetched_refs)
{
int ret = 0;
struct git_transport_data *data = transport->data;
Expand Down Expand Up @@ -354,8 +356,12 @@ static int fetch_refs_via_pack(struct transport *transport,
if (report_unmatched_refs(to_fetch, nr_heads))
ret = -1;

if (fetched_refs)
*fetched_refs = refs;
else
free_refs(refs);

free_refs(refs_tmp);
free_refs(refs);
free(dest);
return ret;
}
Expand Down Expand Up @@ -1215,19 +1221,31 @@ const struct ref *transport_get_remote_refs(struct transport *transport,
return transport->remote_refs;
}

int transport_fetch_refs(struct transport *transport, struct ref *refs)
int transport_fetch_refs(struct transport *transport, struct ref *refs,
struct ref **fetched_refs)
{
int rc;
int nr_heads = 0, nr_alloc = 0, nr_refs = 0;
struct ref **heads = NULL;
struct ref *nop_head = NULL, **nop_tail = &nop_head;
struct ref *rm;

for (rm = refs; rm; rm = rm->next) {
nr_refs++;
if (rm->peer_ref &&
!is_null_oid(&rm->old_oid) &&
!oidcmp(&rm->peer_ref->old_oid, &rm->old_oid))
!oidcmp(&rm->peer_ref->old_oid, &rm->old_oid)) {
/*
* These need to be reported as fetched, but we don't
* actually need to fetch them.
*/
if (fetched_refs) {
struct ref *nop_ref = copy_ref(rm);
*nop_tail = nop_ref;
nop_tail = &nop_ref->next;
}
continue;
}
ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
heads[nr_heads++] = rm;
}
Expand All @@ -1245,7 +1263,11 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
heads[nr_heads++] = rm;
}

rc = transport->vtable->fetch(transport, nr_heads, heads);
rc = transport->vtable->fetch(transport, nr_heads, heads, fetched_refs);
if (fetched_refs && nop_head) {
*nop_tail = *fetched_refs;
*fetched_refs = nop_head;
}

free(heads);
return rc;
Expand Down
3 changes: 2 additions & 1 deletion transport.h
Expand Up @@ -218,7 +218,8 @@ int transport_push(struct transport *connection,
const struct ref *transport_get_remote_refs(struct transport *transport,
const struct argv_array *ref_prefixes);

int transport_fetch_refs(struct transport *transport, struct ref *refs);
int transport_fetch_refs(struct transport *transport, struct ref *refs,
struct ref **fetched_refs);
void transport_unlock_pack(struct transport *transport);
int transport_disconnect(struct transport *transport);
char *transport_anonymize_url(const char *url);
Expand Down

0 comments on commit 989b8c4

Please sign in to comment.