@matheusbsbs / matheus-bagliano / commits / 824d5776c3
Fork 0

Refactor struct transport_ops inlined into struct transport

Aside from reducing the code by 20 lines this refactoring removes a level of indirection when trying to access the operations of a given transport "instance", making the code clearer and easier to follow. It also has the nice effect of giving us the benefits of C99 style struct initialization (namely ".fetch = X") without requiring that level of language support from our compiler. We don't need to worry about new operation methods being added as they will now be NULL'd out automatically by the xcalloc() we use to create the new struct transport we supply to the caller. This pattern already exists in struct walker, so we already have a precedent for it in Git. We also don't really need to worry about any sort of performance decreases that may occur as a result of filling out 4-8 op pointers when we make a "struct transport". The extra few CPU cycles this requires over filling in the "struct transport_ops" is killed by the time it will take Git to actually *use* one of those functions, as most transport operations are going over the wire or will be copying object data locally between two directories. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Shawn O. Pearce committed Sep 19, 2007 at 00:49 UTC 824d5776c3f275c644c71b8c7e254bd2d7b08071
3 files changed +30 -51
builtin-fetch.c
+1 -2
@@ -392,8 +392,7 @@ static int do_fetch(struct transport *transport,
392 if (transport->remote->fetch_tags == -1)
393 no_tags = 1;
394
395 - if (!transport->ops || !transport->ops->get_refs_list ||
396 - !transport->ops->fetch)
395 + if (!transport->get_refs_list || !transport->fetch)
396 die("Don't know how to fetch from %s", transport->url);
397
398 /* if not appending, truncate FETCH_HEAD */
transport.c
+24 -38
@@ -44,8 +44,6 @@ static int disconnect_walker(struct transport *transport)
44 return 0;
45 }
46
47 -static const struct transport_ops rsync_transport;
48 -
47 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) {
48 const char **argv;
49 int argc;
@@ -199,14 +197,6 @@ static int fetch_objs_via_curl(struct transport *transport,
197
198 #endif
199
202 -static const struct transport_ops curl_transport = {
203 - /* set_option */ NULL,
204 - /* get_refs_list */ get_refs_via_curl,
205 - /* fetch */ fetch_objs_via_curl,
206 - /* push */ curl_transport_push,
207 - /* disconnect */ disconnect_walker
208 -};
209 -
200 struct bundle_transport_data {
201 int fd;
202 struct bundle_header header;
@@ -249,14 +239,6 @@ static int close_bundle(struct transport *transport)
239 return 0;
240 }
241
252 -static const struct transport_ops bundle_transport = {
253 - /* set_option */ NULL,
254 - /* get_refs_list */ get_refs_from_bundle,
255 - /* fetch */ fetch_refs_from_bundle,
256 - /* push */ NULL,
257 - /* disconnect */ close_bundle
258 -};
259 -
242 struct git_transport_data {
243 unsigned thin : 1;
244 unsigned keep : 1;
@@ -401,13 +383,6 @@ static int git_transport_push(struct transport *transport, int refspec_nr, const
383 return !!err;
384 }
385
404 -static const struct transport_ops git_transport = {
405 - /* set_option */ set_git_option,
406 - /* get_refs_list */ get_refs_via_connect,
407 - /* fetch */ fetch_refs_via_pack,
408 - /* push */ git_transport_push
409 -};
410 -
386 static int is_local(const char *url)
387 {
388 const char *colon = strchr(url, ':');
@@ -431,18 +406,31 @@ struct transport *transport_get(struct remote *remote, const char *url)
406 ret->url = url;
407
408 if (!prefixcmp(url, "rsync://")) {
434 - ret->ops = &rsync_transport;
409 + /* not supported; don't populate any ops */
410 +
411 } else if (!prefixcmp(url, "http://")
412 || !prefixcmp(url, "https://")
413 || !prefixcmp(url, "ftp://")) {
438 - ret->ops = &curl_transport;
414 + ret->get_refs_list = get_refs_via_curl;
415 + ret->fetch = fetch_objs_via_curl;
416 + ret->push = curl_transport_push;
417 + ret->disconnect = disconnect_walker;
418 +
419 } else if (is_local(url) && is_file(url)) {
420 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
421 ret->data = data;
442 - ret->ops = &bundle_transport;
422 + ret->get_refs_list = get_refs_from_bundle;
423 + ret->fetch = fetch_refs_from_bundle;
424 + ret->disconnect = close_bundle;
425 +
426 } else {
427 struct git_transport_data *data = xcalloc(1, sizeof(*data));
428 ret->data = data;
429 + ret->set_option = set_git_option;
430 + ret->get_refs_list = get_refs_via_connect;
431 + ret->fetch = fetch_refs_via_pack;
432 + ret->push = git_transport_push;
433 +
434 data->thin = 1;
435 data->uploadpack = "git-upload-pack";
436 if (remote && remote->uploadpack)
@@ -451,7 +439,6 @@ struct transport *transport_get(struct remote *remote, const char *url)
439 if (remote && remote->receivepack)
440 data->receivepack = remote->receivepack;
441 data->unpacklimit = -1;
454 - ret->ops = &git_transport;
442 }
443
444 return ret;
@@ -460,24 +447,23 @@ struct transport *transport_get(struct remote *remote, const char *url)
447 int transport_set_option(struct transport *transport,
448 const char *name, const char *value)
449 {
463 - if (transport->ops->set_option)
464 - return transport->ops->set_option(transport, name, value);
450 + if (transport->set_option)
451 + return transport->set_option(transport, name, value);
452 return 1;
453 }
454
455 int transport_push(struct transport *transport,
456 int refspec_nr, const char **refspec, int flags)
457 {
471 - if (!transport->ops->push)
458 + if (!transport->push)
459 return 1;
473 - return transport->ops->push(transport, refspec_nr, refspec, flags);
460 + return transport->push(transport, refspec_nr, refspec, flags);
461 }
462
463 struct ref *transport_get_remote_refs(struct transport *transport)
464 {
465 if (!transport->remote_refs)
479 - transport->remote_refs =
480 - transport->ops->get_refs_list(transport);
466 + transport->remote_refs = transport->get_refs_list(transport);
467 return transport->remote_refs;
468 }
469
@@ -496,7 +482,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs)
482 heads[nr_heads++] = rm;
483 }
484
499 - rc = transport->ops->fetch(transport, nr_heads, heads);
485 + rc = transport->fetch(transport, nr_heads, heads);
486 free(heads);
487 return rc;
488 }
@@ -513,8 +499,8 @@ void transport_unlock_pack(struct transport *transport)
499 int transport_disconnect(struct transport *transport)
500 {
501 int ret = 0;
516 - if (transport->ops->disconnect)
517 - ret = transport->ops->disconnect(transport);
502 + if (transport->disconnect)
503 + ret = transport->disconnect(transport);
504 free(transport);
505 return ret;
506 }
transport.h
+5 -11
@@ -5,22 +5,11 @@
5 #include "remote.h"
6
7 struct transport {
8 - unsigned verbose : 1;
8 struct remote *remote;
9 const char *url;
11 -
10 void *data;
13 -
11 struct ref *remote_refs;
12
16 - const struct transport_ops *ops;
17 - char *pack_lockfile;
18 -};
19 -
20 -#define TRANSPORT_PUSH_ALL 1
21 -#define TRANSPORT_PUSH_FORCE 2
22 -
23 -struct transport_ops {
13 /**
14 * Returns 0 if successful, positive if the option is not
15 * recognized or is inapplicable, and negative if the option
@@ -34,8 +23,13 @@ struct transport_ops {
23 int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags);
24
25 int (*disconnect)(struct transport *connection);
26 + char *pack_lockfile;
27 + unsigned verbose : 1;
28 };
29
30 +#define TRANSPORT_PUSH_ALL 1
31 +#define TRANSPORT_PUSH_FORCE 2
32 +
33 /* Returns a transport suitable for the url */
34 struct transport *transport_get(struct remote *, const char *);
35