@matheusbsbs / matheus-bagliano / commits / 425b139313
Fork 0

Simplify fetch transport API to just one function

Commit walkers need to know the SHA-1 name of any objects they have been asked to fetch while the native pack transport only wants to know the names of the remote refs as the remote side must do the name->SHA-1 translation. Since we only have three fetch implementations and one of them (bundle) doesn't even need the name information we can reduce the code required to perform a fetch by having just one function and passing of the filtered list of refs to be fetched. Each transport can then obtain the information it needs from that ref array to construct its own internal operation state. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Conflicts: transport.c Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Sep 14, 2007 at 03:31 UTC 425b1393139d99d89c7a95906686d9b041f2ee3d
3 files changed +27 -39
builtin-fetch.c
+1 -1
@@ -355,7 +355,7 @@ static int do_fetch(struct transport *transport,
355 no_tags = 1;
356
357 if (!transport->ops || !transport->ops->get_refs_list ||
358 - !(transport->ops->fetch_refs || transport->ops->fetch_objs))
358 + !transport->ops->fetch)
359 die("Don't know how to fetch from %s", transport->url);
360
361 /* if not appending, truncate FETCH_HEAD */
transport.c
+25 -34
@@ -10,10 +10,12 @@
10 /* Generic functions for using commit walkers */
11
12 static int fetch_objs_via_walker(const struct transport *transport,
13 - int nr_objs, char **objs)
13 + int nr_objs, struct ref **to_fetch)
14 {
15 char *dest = xstrdup(transport->url);
16 struct walker *walker = transport->data;
17 + char **objs = xmalloc(nr_objs * sizeof(*objs));
18 + int i;
19
20 walker->get_all = 1;
21 walker->get_tree = 1;
@@ -21,9 +23,15 @@ static int fetch_objs_via_walker(const struct transport *transport,
23 walker->get_verbosely = transport->verbose;
24 walker->get_recover = 0;
25
26 + for (i = 0; i < nr_objs; i++)
27 + objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
28 +
29 if (walker_fetch(walker, nr_objs, objs, NULL, dest))
30 die("Fetch failed.");
31
32 + for (i = 0; i < nr_objs; i++)
33 + free(objs[i]);
34 + free(objs);
35 free(dest);
36 return 0;
37 }
@@ -179,8 +187,7 @@ static struct ref *get_refs_via_curl(const struct transport *transport)
187 static const struct transport_ops curl_transport = {
188 /* set_option */ NULL,
189 /* get_refs_list */ get_refs_via_curl,
182 - /* fetch_refs */ NULL,
183 - /* fetch_objs */ fetch_objs_via_walker,
190 + /* fetch */ fetch_objs_via_walker,
191 /* push */ curl_transport_push,
192 /* disconnect */ disconnect_walker
193 };
@@ -213,7 +220,7 @@ static struct ref *get_refs_from_bundle(const struct transport *transport)
220 }
221
222 static int fetch_refs_from_bundle(const struct transport *transport,
216 - int nr_heads, char **heads)
223 + int nr_heads, struct ref **to_fetch)
224 {
225 struct bundle_transport_data *data = transport->data;
226 return unbundle(&data->header, data->fd);
@@ -230,8 +237,7 @@ static int close_bundle(struct transport *transport)
237 static const struct transport_ops bundle_transport = {
238 /* set_option */ NULL,
239 /* get_refs_list */ get_refs_from_bundle,
233 - /* fetch_refs */ fetch_refs_from_bundle,
234 - /* fetch_objs */ NULL,
240 + /* fetch */ fetch_refs_from_bundle,
241 /* push */ NULL,
242 /* disconnect */ close_bundle
243 };
@@ -301,12 +307,14 @@ static struct ref *get_refs_via_connect(const struct transport *transport)
307 }
308
309 static int fetch_refs_via_pack(const struct transport *transport,
304 - int nr_heads, char **heads)
310 + int nr_heads, struct ref **to_fetch)
311 {
312 struct git_transport_data *data = transport->data;
313 + char **heads = xmalloc(nr_heads * sizeof(*heads));
314 struct ref *refs;
315 char *dest = xstrdup(transport->url);
316 struct fetch_pack_args args;
317 + int i;
318
319 args.uploadpack = data->uploadpack;
320 args.quiet = 0;
@@ -320,14 +328,13 @@ static int fetch_refs_via_pack(const struct transport *transport,
328
329 setup_fetch_pack(&args);
330
331 + for (i = 0; i < nr_heads; i++)
332 + heads[i] = xstrdup(to_fetch[i]->name);
333 refs = fetch_pack(dest, nr_heads, heads);
334
325 - // ???? check that refs got everything?
326 -
327 - /* free the memory used for the refs list ... */
328 -
335 + for (i = 0; i < nr_heads; i++)
336 + free(heads[i]);
337 free_refs(refs);
330 -
338 free(dest);
339 return 0;
340 }
@@ -379,8 +386,7 @@ static int git_transport_push(struct transport *transport, int refspec_nr, const
386 static const struct transport_ops git_transport = {
387 /* set_option */ set_git_option,
388 /* get_refs_list */ get_refs_via_connect,
382 - /* fetch_refs */ fetch_refs_via_pack,
383 - /* fetch_objs */ NULL,
389 + /* fetch */ fetch_refs_via_pack,
390 /* push */ git_transport_push
391 };
392
@@ -476,37 +482,22 @@ struct ref *transport_get_remote_refs(struct transport *transport)
482
483 int transport_fetch_refs(struct transport *transport, struct ref *refs)
484 {
479 - int i;
485 + int rc;
486 int nr_heads = 0, nr_alloc = 0;
481 - char **heads = NULL;
487 + struct ref **heads = NULL;
488 struct ref *rm;
483 - int use_objs = !transport->ops->fetch_refs;
489
490 for (rm = refs; rm; rm = rm->next) {
491 if (rm->peer_ref &&
492 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
493 continue;
494 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
490 - if (use_objs) {
491 - heads[nr_heads++] = xstrdup(sha1_to_hex(rm->old_sha1));
492 - } else {
493 - heads[nr_heads++] = xstrdup(rm->name);
494 - }
495 - }
496 -
497 - if (use_objs) {
498 - if (transport->ops->fetch_objs(transport, nr_heads, heads))
499 - return -1;
500 - } else {
501 - if (transport->ops->fetch_refs(transport, nr_heads, heads))
502 - return -1;
495 + heads[nr_heads++] = rm;
496 }
497
505 - /* free the memory used for the heads list ... */
506 - for (i = 0; i < nr_heads; i++)
507 - free(heads[i]);
498 + rc = transport->ops->fetch(transport, nr_heads, heads);
499 free(heads);
509 - return 0;
500 + return rc;
501 }
502
503 int transport_disconnect(struct transport *transport)
transport.h
+1 -4
@@ -30,10 +30,7 @@ struct transport_ops {
30 const char *value);
31
32 struct ref *(*get_refs_list)(const struct transport *transport);
33 - int (*fetch_refs)(const struct transport *transport,
34 - int nr_refs, char **refs);
35 - int (*fetch_objs)(const struct transport *transport,
36 - int nr_objs, char **objs);
33 + int (*fetch)(const struct transport *transport, int refs_nr, struct ref **refs);
34 int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
35
36 int (*disconnect)(struct transport *connection);