connect: discover protocol version outside of get_remote_heads

In order to prepare for the addition of protocol_v2 push the protocol
version discovery outside of 'get_remote_heads()'.  This will allow for
keeping the logic for processing the reference advertisement for
protocol_v1 and protocol_v0 separate from the logic for protocol_v2.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Brandon Williams 2018-03-14 11:31:45 -07:00 committed by Junio C Hamano
parent 7e3e479b90
commit ad6ac1244f
7 changed files with 83 additions and 29 deletions

View file

@ -4,6 +4,7 @@
#include "remote.h" #include "remote.h"
#include "connect.h" #include "connect.h"
#include "sha1-array.h" #include "sha1-array.h"
#include "protocol.h"
static const char fetch_pack_usage[] = static const char fetch_pack_usage[] =
"git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] " "git fetch-pack [--all] [--stdin] [--quiet | -q] [--keep | -k] [--thin] "
@ -52,6 +53,7 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
struct fetch_pack_args args; struct fetch_pack_args args;
struct oid_array shallow = OID_ARRAY_INIT; struct oid_array shallow = OID_ARRAY_INIT;
struct string_list deepen_not = STRING_LIST_INIT_DUP; struct string_list deepen_not = STRING_LIST_INIT_DUP;
struct packet_reader reader;
packet_trace_identity("fetch-pack"); packet_trace_identity("fetch-pack");
@ -193,7 +195,19 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
if (!conn) if (!conn)
return args.diag_url ? 0 : 1; return args.diag_url ? 0 : 1;
} }
get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
packet_reader_init(&reader, fd[0], NULL, 0,
PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_GENTLE_ON_EOF);
switch (discover_version(&reader)) {
case protocol_v1:
case protocol_v0:
get_remote_heads(&reader, &ref, 0, NULL, &shallow);
break;
case protocol_unknown_version:
BUG("unknown protocol version");
}
ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought, ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
&shallow, pack_lockfile_ptr); &shallow, pack_lockfile_ptr);

View file

@ -14,6 +14,7 @@
#include "sha1-array.h" #include "sha1-array.h"
#include "gpg-interface.h" #include "gpg-interface.h"
#include "gettext.h" #include "gettext.h"
#include "protocol.h"
static const char * const send_pack_usage[] = { static const char * const send_pack_usage[] = {
N_("git send-pack [--all | --mirror] [--dry-run] [--force] " N_("git send-pack [--all | --mirror] [--dry-run] [--force] "
@ -154,6 +155,7 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
int progress = -1; int progress = -1;
int from_stdin = 0; int from_stdin = 0;
struct push_cas_option cas = {0}; struct push_cas_option cas = {0};
struct packet_reader reader;
struct option options[] = { struct option options[] = {
OPT__VERBOSITY(&verbose), OPT__VERBOSITY(&verbose),
@ -256,8 +258,19 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
args.verbose ? CONNECT_VERBOSE : 0); args.verbose ? CONNECT_VERBOSE : 0);
} }
get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL, packet_reader_init(&reader, fd[0], NULL, 0,
&extra_have, &shallow); PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_GENTLE_ON_EOF);
switch (discover_version(&reader)) {
case protocol_v1:
case protocol_v0:
get_remote_heads(&reader, &remote_refs, REF_NORMAL,
&extra_have, &shallow);
break;
case protocol_unknown_version:
BUG("unknown protocol version");
}
transport_verify_remote_names(nr_refspecs, refspecs); transport_verify_remote_names(nr_refspecs, refspecs);

View file

@ -62,7 +62,7 @@ static void die_initial_contact(int unexpected)
"and the repository exists.")); "and the repository exists."));
} }
static enum protocol_version discover_version(struct packet_reader *reader) enum protocol_version discover_version(struct packet_reader *reader)
{ {
enum protocol_version version = protocol_unknown_version; enum protocol_version version = protocol_unknown_version;
@ -233,7 +233,7 @@ enum get_remote_heads_state {
/* /*
* Read all the refs from the other end * Read all the refs from the other end
*/ */
struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, struct ref **get_remote_heads(struct packet_reader *reader,
struct ref **list, unsigned int flags, struct ref **list, unsigned int flags,
struct oid_array *extra_have, struct oid_array *extra_have,
struct oid_array *shallow_points) struct oid_array *shallow_points)
@ -241,24 +241,17 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
struct ref **orig_list = list; struct ref **orig_list = list;
int len = 0; int len = 0;
enum get_remote_heads_state state = EXPECTING_FIRST_REF; enum get_remote_heads_state state = EXPECTING_FIRST_REF;
struct packet_reader reader;
const char *arg; const char *arg;
packet_reader_init(&reader, in, src_buf, src_len,
PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_GENTLE_ON_EOF);
discover_version(&reader);
*list = NULL; *list = NULL;
while (state != EXPECTING_DONE) { while (state != EXPECTING_DONE) {
switch (packet_reader_read(&reader)) { switch (packet_reader_read(reader)) {
case PACKET_READ_EOF: case PACKET_READ_EOF:
die_initial_contact(1); die_initial_contact(1);
case PACKET_READ_NORMAL: case PACKET_READ_NORMAL:
len = reader.pktlen; len = reader->pktlen;
if (len > 4 && skip_prefix(reader.line, "ERR ", &arg)) if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
die("remote error: %s", arg); die("remote error: %s", arg);
break; break;
case PACKET_READ_FLUSH: case PACKET_READ_FLUSH:
@ -270,22 +263,22 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
switch (state) { switch (state) {
case EXPECTING_FIRST_REF: case EXPECTING_FIRST_REF:
process_capabilities(reader.line, &len); process_capabilities(reader->line, &len);
if (process_dummy_ref(reader.line)) { if (process_dummy_ref(reader->line)) {
state = EXPECTING_SHALLOW; state = EXPECTING_SHALLOW;
break; break;
} }
state = EXPECTING_REF; state = EXPECTING_REF;
/* fallthrough */ /* fallthrough */
case EXPECTING_REF: case EXPECTING_REF:
if (process_ref(reader.line, len, &list, flags, extra_have)) if (process_ref(reader->line, len, &list, flags, extra_have))
break; break;
state = EXPECTING_SHALLOW; state = EXPECTING_SHALLOW;
/* fallthrough */ /* fallthrough */
case EXPECTING_SHALLOW: case EXPECTING_SHALLOW:
if (process_shallow(reader.line, len, shallow_points)) if (process_shallow(reader->line, len, shallow_points))
break; break;
die("protocol error: unexpected '%s'", reader.line); die("protocol error: unexpected '%s'", reader->line);
case EXPECTING_DONE: case EXPECTING_DONE:
break; break;
} }

View file

@ -13,4 +13,7 @@ extern int parse_feature_request(const char *features, const char *feature);
extern const char *server_feature_value(const char *feature, int *len_ret); extern const char *server_feature_value(const char *feature, int *len_ret);
extern int url_is_local_not_ssh(const char *url); extern int url_is_local_not_ssh(const char *url);
struct packet_reader;
extern enum protocol_version discover_version(struct packet_reader *reader);
#endif #endif

View file

@ -1,6 +1,7 @@
#include "cache.h" #include "cache.h"
#include "config.h" #include "config.h"
#include "remote.h" #include "remote.h"
#include "connect.h"
#include "strbuf.h" #include "strbuf.h"
#include "walker.h" #include "walker.h"
#include "http.h" #include "http.h"
@ -13,6 +14,7 @@
#include "credential.h" #include "credential.h"
#include "sha1-array.h" #include "sha1-array.h"
#include "send-pack.h" #include "send-pack.h"
#include "protocol.h"
static struct remote *remote; static struct remote *remote;
/* always ends with a trailing slash */ /* always ends with a trailing slash */
@ -176,8 +178,22 @@ static struct discovery *last_discovery;
static struct ref *parse_git_refs(struct discovery *heads, int for_push) static struct ref *parse_git_refs(struct discovery *heads, int for_push)
{ {
struct ref *list = NULL; struct ref *list = NULL;
get_remote_heads(-1, heads->buf, heads->len, &list, struct packet_reader reader;
for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
packet_reader_init(&reader, -1, heads->buf, heads->len,
PACKET_READ_CHOMP_NEWLINE |
PACKET_READ_GENTLE_ON_EOF);
switch (discover_version(&reader)) {
case protocol_v1:
case protocol_v0:
get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
NULL, &heads->shallow);
break;
case protocol_unknown_version:
BUG("unknown protocol version");
}
return list; return list;
} }

View file

@ -150,10 +150,11 @@ int check_ref_type(const struct ref *ref, int flags);
void free_refs(struct ref *ref); void free_refs(struct ref *ref);
struct oid_array; struct oid_array;
extern struct ref **get_remote_heads(int in, char *src_buf, size_t src_len, struct packet_reader;
extern struct ref **get_remote_heads(struct packet_reader *reader,
struct ref **list, unsigned int flags, struct ref **list, unsigned int flags,
struct oid_array *extra_have, struct oid_array *extra_have,
struct oid_array *shallow); struct oid_array *shallow_points);
int resolve_remote_symref(struct ref *ref, struct ref *list); int resolve_remote_symref(struct ref *ref, struct ref *list);
int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid); int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);

View file

@ -18,6 +18,7 @@
#include "sha1-array.h" #include "sha1-array.h"
#include "sigchain.h" #include "sigchain.h"
#include "transport-internal.h" #include "transport-internal.h"
#include "protocol.h"
static void set_upstreams(struct transport *transport, struct ref *refs, static void set_upstreams(struct transport *transport, struct ref *refs,
int pretend) int pretend)
@ -190,13 +191,26 @@ static int connect_setup(struct transport *transport, int for_push)
static struct ref *get_refs_via_connect(struct transport *transport, int for_push) static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
{ {
struct git_transport_data *data = transport->data; struct git_transport_data *data = transport->data;
struct ref *refs; struct ref *refs = NULL;
struct packet_reader reader;
connect_setup(transport, for_push); connect_setup(transport, for_push);
get_remote_heads(data->fd[0], NULL, 0, &refs,
for_push ? REF_NORMAL : 0, packet_reader_init(&reader, data->fd[0], NULL, 0,
&data->extra_have, PACKET_READ_CHOMP_NEWLINE |
&data->shallow); PACKET_READ_GENTLE_ON_EOF);
switch (discover_version(&reader)) {
case protocol_v1:
case protocol_v0:
get_remote_heads(&reader, &refs,
for_push ? REF_NORMAL : 0,
&data->extra_have,
&data->shallow);
break;
case protocol_unknown_version:
BUG("unknown protocol version");
}
data->got_remote_heads = 1; data->got_remote_heads = 1;
return refs; return refs;