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>

Brandon Williams committed Mar 14, 2018 at 11:31 UTC ad6ac1244fd175d08bcee62060a9a0b7975930fb
7 files changed +83 -29
builtin/fetch-pack.c
+15 -1
@@ -4,6 +4,7 @@
4 #include "remote.h"
5 #include "connect.h"
6 #include "sha1-array.h"
7 +#include "protocol.h"
8
9 static const char fetch_pack_usage[] =
10 "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)
53 struct fetch_pack_args args;
54 struct oid_array shallow = OID_ARRAY_INIT;
55 struct string_list deepen_not = STRING_LIST_INIT_DUP;
56 + struct packet_reader reader;
57
58 packet_trace_identity("fetch-pack");
59
@@ -193,7 +195,19 @@ int cmd_fetch_pack(int argc, const char **argv, const char *prefix)
195 if (!conn)
196 return args.diag_url ? 0 : 1;
197 }
196 - get_remote_heads(fd[0], NULL, 0, &ref, 0, NULL, &shallow);
198 +
199 + packet_reader_init(&reader, fd[0], NULL, 0,
200 + PACKET_READ_CHOMP_NEWLINE |
201 + PACKET_READ_GENTLE_ON_EOF);
202 +
203 + switch (discover_version(&reader)) {
204 + case protocol_v1:
205 + case protocol_v0:
206 + get_remote_heads(&reader, &ref, 0, NULL, &shallow);
207 + break;
208 + case protocol_unknown_version:
209 + BUG("unknown protocol version");
210 + }
211
212 ref = fetch_pack(&args, fd, conn, ref, dest, sought, nr_sought,
213 &shallow, pack_lockfile_ptr);
builtin/send-pack.c
+15 -2
@@ -14,6 +14,7 @@
14 #include "sha1-array.h"
15 #include "gpg-interface.h"
16 #include "gettext.h"
17 +#include "protocol.h"
18
19 static const char * const send_pack_usage[] = {
20 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)
155 int progress = -1;
156 int from_stdin = 0;
157 struct push_cas_option cas = {0};
158 + struct packet_reader reader;
159
160 struct option options[] = {
161 OPT__VERBOSITY(&verbose),
@@ -256,8 +258,19 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
258 args.verbose ? CONNECT_VERBOSE : 0);
259 }
260
259 - get_remote_heads(fd[0], NULL, 0, &remote_refs, REF_NORMAL,
260 - &extra_have, &shallow);
261 + packet_reader_init(&reader, fd[0], NULL, 0,
262 + PACKET_READ_CHOMP_NEWLINE |
263 + PACKET_READ_GENTLE_ON_EOF);
264 +
265 + switch (discover_version(&reader)) {
266 + case protocol_v1:
267 + case protocol_v0:
268 + get_remote_heads(&reader, &remote_refs, REF_NORMAL,
269 + &extra_have, &shallow);
270 + break;
271 + case protocol_unknown_version:
272 + BUG("unknown protocol version");
273 + }
274
275 transport_verify_remote_names(nr_refspecs, refspecs);
276
connect.c
+10 -17
@@ -62,7 +62,7 @@ static void die_initial_contact(int unexpected)
62 "and the repository exists."));
63 }
64
65 -static enum protocol_version discover_version(struct packet_reader *reader)
65 +enum protocol_version discover_version(struct packet_reader *reader)
66 {
67 enum protocol_version version = protocol_unknown_version;
68
@@ -233,7 +233,7 @@ enum get_remote_heads_state {
233 /*
234 * Read all the refs from the other end
235 */
236 -struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
236 +struct ref **get_remote_heads(struct packet_reader *reader,
237 struct ref **list, unsigned int flags,
238 struct oid_array *extra_have,
239 struct oid_array *shallow_points)
@@ -241,24 +241,17 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
241 struct ref **orig_list = list;
242 int len = 0;
243 enum get_remote_heads_state state = EXPECTING_FIRST_REF;
244 - struct packet_reader reader;
244 const char *arg;
245
247 - packet_reader_init(&reader, in, src_buf, src_len,
248 - PACKET_READ_CHOMP_NEWLINE |
249 - PACKET_READ_GENTLE_ON_EOF);
250 -
251 - discover_version(&reader);
252 -
246 *list = NULL;
247
248 while (state != EXPECTING_DONE) {
256 - switch (packet_reader_read(&reader)) {
249 + switch (packet_reader_read(reader)) {
250 case PACKET_READ_EOF:
251 die_initial_contact(1);
252 case PACKET_READ_NORMAL:
260 - len = reader.pktlen;
261 - if (len > 4 && skip_prefix(reader.line, "ERR ", &arg))
253 + len = reader->pktlen;
254 + if (len > 4 && skip_prefix(reader->line, "ERR ", &arg))
255 die("remote error: %s", arg);
256 break;
257 case PACKET_READ_FLUSH:
@@ -270,22 +263,22 @@ struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
263
264 switch (state) {
265 case EXPECTING_FIRST_REF:
273 - process_capabilities(reader.line, &len);
274 - if (process_dummy_ref(reader.line)) {
266 + process_capabilities(reader->line, &len);
267 + if (process_dummy_ref(reader->line)) {
268 state = EXPECTING_SHALLOW;
269 break;
270 }
271 state = EXPECTING_REF;
272 /* fallthrough */
273 case EXPECTING_REF:
281 - if (process_ref(reader.line, len, &list, flags, extra_have))
274 + if (process_ref(reader->line, len, &list, flags, extra_have))
275 break;
276 state = EXPECTING_SHALLOW;
277 /* fallthrough */
278 case EXPECTING_SHALLOW:
286 - if (process_shallow(reader.line, len, shallow_points))
279 + if (process_shallow(reader->line, len, shallow_points))
280 break;
288 - die("protocol error: unexpected '%s'", reader.line);
281 + die("protocol error: unexpected '%s'", reader->line);
282 case EXPECTING_DONE:
283 break;
284 }
connect.h
+3
@@ -13,4 +13,7 @@ extern int parse_feature_request(const char *features, const char *feature);
13 extern const char *server_feature_value(const char *feature, int *len_ret);
14 extern int url_is_local_not_ssh(const char *url);
15
16 +struct packet_reader;
17 +extern enum protocol_version discover_version(struct packet_reader *reader);
18 +
19 #endif
remote-curl.c
+18 -2
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 +#include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
@@ -13,6 +14,7 @@
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
17 +#include "protocol.h"
18
19 static struct remote *remote;
20 /* always ends with a trailing slash */
@@ -176,8 +178,22 @@ static struct discovery *last_discovery;
178 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
179 {
180 struct ref *list = NULL;
179 - get_remote_heads(-1, heads->buf, heads->len, &list,
180 - for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
181 + struct packet_reader reader;
182 +
183 + packet_reader_init(&reader, -1, heads->buf, heads->len,
184 + PACKET_READ_CHOMP_NEWLINE |
185 + PACKET_READ_GENTLE_ON_EOF);
186 +
187 + switch (discover_version(&reader)) {
188 + case protocol_v1:
189 + case protocol_v0:
190 + get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
191 + NULL, &heads->shallow);
192 + break;
193 + case protocol_unknown_version:
194 + BUG("unknown protocol version");
195 + }
196 +
197 return list;
198 }
199
remote.h
+3 -2
@@ -150,10 +150,11 @@ int check_ref_type(const struct ref *ref, int flags);
150 void free_refs(struct ref *ref);
151
152 struct oid_array;
153 -extern struct ref **get_remote_heads(int in, char *src_buf, size_t src_len,
153 +struct packet_reader;
154 +extern struct ref **get_remote_heads(struct packet_reader *reader,
155 struct ref **list, unsigned int flags,
156 struct oid_array *extra_have,
156 - struct oid_array *shallow);
157 + struct oid_array *shallow_points);
158
159 int resolve_remote_symref(struct ref *ref, struct ref *list);
160 int ref_newer(const struct object_id *new_oid, const struct object_id *old_oid);
transport.c
+19 -5
@@ -18,6 +18,7 @@
18 #include "sha1-array.h"
19 #include "sigchain.h"
20 #include "transport-internal.h"
21 +#include "protocol.h"
22
23 static void set_upstreams(struct transport *transport, struct ref *refs,
24 int pretend)
@@ -190,13 +191,26 @@ static int connect_setup(struct transport *transport, int for_push)
191 static struct ref *get_refs_via_connect(struct transport *transport, int for_push)
192 {
193 struct git_transport_data *data = transport->data;
193 - struct ref *refs;
194 + struct ref *refs = NULL;
195 + struct packet_reader reader;
196
197 connect_setup(transport, for_push);
196 - get_remote_heads(data->fd[0], NULL, 0, &refs,
197 - for_push ? REF_NORMAL : 0,
198 - &data->extra_have,
199 - &data->shallow);
198 +
199 + packet_reader_init(&reader, data->fd[0], NULL, 0,
200 + PACKET_READ_CHOMP_NEWLINE |
201 + PACKET_READ_GENTLE_ON_EOF);
202 +
203 + switch (discover_version(&reader)) {
204 + case protocol_v1:
205 + case protocol_v0:
206 + get_remote_heads(&reader, &refs,
207 + for_push ? REF_NORMAL : 0,
208 + &data->extra_have,
209 + &data->shallow);
210 + break;
211 + case protocol_unknown_version:
212 + BUG("unknown protocol version");
213 + }
214 data->got_remote_heads = 1;
215
216 return refs;