aboutsummaryrefslogtreecommitdiffstats
path: root/fetch-pack.c
diff options
context:
space:
mode:
authorJonathan Tan <jonathantanmy@google.com>2021-05-04 14:16:01 -0700
committerJunio C Hamano <gitster@pobox.com>2021-05-05 10:41:29 +0900
commit9c1e657a8fd26fa3ed8d13fb8c796cef8db8b124 (patch)
tree79ed7a1763a89a1be268af86dda6110a2d0c2a1a /fetch-pack.c
parent6871d0cec62dc12d0c5f7390eee8a80614919578 (diff)
downloadgit-9c1e657a8fd26fa3ed8d13fb8c796cef8db8b124.tar.gz
fetch: teach independent negotiation (no packfile)
Currently, the packfile negotiation step within a Git fetch cannot be done independent of sending the packfile, even though there is at least one application wherein this is useful. Therefore, make it possible for this negotiation step to be done independently. A subsequent commit will use this for one such application - push negotiation. This feature is for protocol v2 only. (An implementation for protocol v0 would require a separate implementation in the fetch, transport, and transport helper code.) In the protocol, the main hindrance towards independent negotiation is that the server can unilaterally decide to send the packfile. This is solved by a "wait-for-done" argument: the server will then wait for the client to say "done". In practice, the client will never say it; instead it will cease requests once it is satisfied. In the client, the main change lies in the transport and transport helper code. fetch_refs_via_pack() performs everything needed - protocol version and capability checks, and the negotiation itself. There are 2 code paths that do not go through fetch_refs_via_pack() that needed to be individually excluded: the bundle transport (excluded through requiring smart_options, which the bundle transport doesn't support) and transport helpers that do not support takeover. If or when we support independent negotiation for protocol v0, we will need to modify these 2 code paths to support it. But for now, report failure if independent negotiation is requested in these cases. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'fetch-pack.c')
-rw-r--r--fetch-pack.c111
1 files changed, 107 insertions, 4 deletions
diff --git a/fetch-pack.c b/fetch-pack.c
index 512fe5450d..c135635e34 100644
--- a/fetch-pack.c
+++ b/fetch-pack.c
@@ -23,6 +23,8 @@
#include "fetch-negotiator.h"
#include "fsck.h"
#include "shallow.h"
+#include "commit-reach.h"
+#include "commit-graph.h"
static int transfer_unpack_limit = -1;
static int fetch_unpack_limit = -1;
@@ -45,6 +47,8 @@ static struct string_list uri_protocols = STRING_LIST_INIT_DUP;
/* Remember to update object flag allocation in object.h */
#define COMPLETE (1U << 0)
#define ALTERNATE (1U << 1)
+#define COMMON (1U << 6)
+#define REACH_SCRATCH (1U << 7)
/*
* After sending this many "have"s if we do not get any new ACK , we
@@ -1523,10 +1527,10 @@ enum fetch_state {
FETCH_DONE,
};
-static void do_check_stateless_delimiter(const struct fetch_pack_args *args,
+static void do_check_stateless_delimiter(int stateless_rpc,
struct packet_reader *reader)
{
- check_stateless_delimiter(args->stateless_rpc, reader,
+ check_stateless_delimiter(stateless_rpc, reader,
_("git fetch-pack: expected response end packet"));
}
@@ -1622,7 +1626,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
*/
state = FETCH_GET_PACK;
} else {
- do_check_stateless_delimiter(args, &reader);
+ do_check_stateless_delimiter(args->stateless_rpc, &reader);
state = FETCH_SEND_REQUEST;
}
break;
@@ -1645,7 +1649,7 @@ static struct ref *do_fetch_pack_v2(struct fetch_pack_args *args,
packfile_uris.nr ? &index_pack_args : NULL,
sought, nr_sought, &fsck_options.gitmodules_found))
die(_("git fetch-pack: fetch failed."));
- do_check_stateless_delimiter(args, &reader);
+ do_check_stateless_delimiter(args->stateless_rpc, &reader);
state = FETCH_DONE;
break;
@@ -1962,6 +1966,105 @@ cleanup:
return ref_cpy;
}
+static int add_to_object_array(const struct object_id *oid, void *data)
+{
+ struct object_array *a = data;
+
+ add_object_array(lookup_object(the_repository, oid), "", a);
+ return 0;
+}
+
+static void clear_common_flag(struct oidset *s)
+{
+ struct oidset_iter iter;
+ const struct object_id *oid;
+ oidset_iter_init(s, &iter);
+
+ while ((oid = oidset_iter_next(&iter))) {
+ struct object *obj = lookup_object(the_repository, oid);
+ obj->flags &= ~COMMON;
+ }
+}
+
+void negotiate_using_fetch(const struct oid_array *negotiation_tips,
+ const struct string_list *server_options,
+ int stateless_rpc,
+ int fd[],
+ struct oidset *acked_commits)
+{
+ struct fetch_negotiator negotiator;
+ struct packet_reader reader;
+ struct object_array nt_object_array = OBJECT_ARRAY_INIT;
+ struct strbuf req_buf = STRBUF_INIT;
+ int haves_to_send = INITIAL_FLUSH;
+ int in_vain = 0;
+ int seen_ack = 0;
+ int last_iteration = 0;
+ timestamp_t min_generation = GENERATION_NUMBER_INFINITY;
+
+ fetch_negotiator_init(the_repository, &negotiator);
+ mark_tips(&negotiator, negotiation_tips);
+
+ packet_reader_init(&reader, fd[0], NULL, 0,
+ PACKET_READ_CHOMP_NEWLINE |
+ PACKET_READ_DIE_ON_ERR_PACKET);
+
+ oid_array_for_each((struct oid_array *) negotiation_tips,
+ add_to_object_array,
+ &nt_object_array);
+
+ while (!last_iteration) {
+ int haves_added;
+ struct object_id common_oid;
+ int received_ready = 0;
+
+ strbuf_reset(&req_buf);
+ write_fetch_command_and_capabilities(&req_buf, server_options);
+
+ packet_buf_write(&req_buf, "wait-for-done");
+
+ haves_added = add_haves(&negotiator, &req_buf, &haves_to_send);
+ in_vain += haves_added;
+ if (!haves_added || (seen_ack && in_vain >= MAX_IN_VAIN))
+ last_iteration = 1;
+
+ /* Send request */
+ packet_buf_flush(&req_buf);
+ if (write_in_full(fd[1], req_buf.buf, req_buf.len) < 0)
+ die_errno(_("unable to write request to remote"));
+
+ /* Process ACKs/NAKs */
+ process_section_header(&reader, "acknowledgments", 0);
+ while (process_ack(&negotiator, &reader, &common_oid,
+ &received_ready)) {
+ struct commit *commit = lookup_commit(the_repository,
+ &common_oid);
+ if (commit) {
+ timestamp_t generation;
+
+ parse_commit_or_die(commit);
+ commit->object.flags |= COMMON;
+ generation = commit_graph_generation(commit);
+ if (generation < min_generation)
+ min_generation = generation;
+ }
+ in_vain = 0;
+ seen_ack = 1;
+ oidset_insert(acked_commits, &common_oid);
+ }
+ if (received_ready)
+ die(_("unexpected 'ready' from remote"));
+ else
+ do_check_stateless_delimiter(stateless_rpc, &reader);
+ if (can_all_from_reach_with_flag(&nt_object_array, COMMON,
+ REACH_SCRATCH, 0,
+ min_generation))
+ last_iteration = 1;
+ }
+ clear_common_flag(acked_commits);
+ strbuf_release(&req_buf);
+}
+
int report_unmatched_refs(struct ref **sought, int nr_sought)
{
int i, ret = 0;