@matheusbsbs / matheus-bagliano / commits / 30ae764b1e
Fork 0

Modularize commit-walker

This turns the extern functions to be provided by the backend into a struct of pointers, renames the functions to be more namespace-friendly, and updates http-fetch to this interface. It removes the unused include from http-push.c. It makes git-http-fetch a builtin (with the implementation a separate file, accessible directly). Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Daniel Barkalow committed Sep 10, 2007 at 23:02 UTC 30ae764b1e11f10b5fca723a876a0f3de3ca11ab
9 files changed +271 -230
Makefile
+9 -8
@@ -514,7 +514,9 @@ else
514 CC_LD_DYNPATH = -R
515 endif
516
517 -ifndef NO_CURL
517 +ifdef NO_CURL
518 + BASIC_CFLAGS += -DNO_CURL
519 +else
520 ifdef CURLDIR
521 # Try "-Wl,-rpath=$(CURLDIR)/$(lib)" in such a case.
522 BASIC_CFLAGS += -I$(CURLDIR)/include
@@ -522,7 +524,9 @@ ifndef NO_CURL
524 else
525 CURL_LIBCURL = -lcurl
526 endif
525 - PROGRAMS += git-http-fetch$X
527 + BUILTIN_OBJS += builtin-http-fetch.o
528 + EXTLIBS += $(CURL_LIBCURL)
529 + LIB_OBJS += http.o walker.o http-walker.o
530 curl_check := $(shell (echo 070908; curl-config --vernum) | sort -r | sed -ne 2p)
531 ifeq "$(curl_check)" "070908"
532 ifndef NO_EXPAT
@@ -884,7 +888,7 @@ http.o: http.c GIT-CFLAGS
888 $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $<
889
890 ifdef NO_EXPAT
887 -http-fetch.o: http-fetch.c http.h GIT-CFLAGS
891 +http-walker.o: http-walker.c http.h GIT-CFLAGS
892 $(QUIET_CC)$(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_EXPAT $<
893 endif
894
@@ -893,16 +897,13 @@ git-%$X: %.o $(GITLIBS)
897
898 git-imap-send$X: imap-send.o $(LIB_FILE)
899
896 -http.o http-fetch.o http-push.o: http.h
897 -git-http-fetch$X: fetch.o http.o http-fetch.o $(GITLIBS)
898 - $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
899 - $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
900 +http.o http-walker.o http-push.o: http.h
901
902 git-http-push$X: revision.o http.o http-push.o $(GITLIBS)
903 $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
904 $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)
905
905 -$(LIB_OBJS) $(BUILTIN_OBJS) fetch.o: $(LIB_H)
906 +$(LIB_OBJS) $(BUILTIN_OBJS) walker.o: $(LIB_H)
907 $(patsubst git-%$X,%.o,$(PROGRAMS)): $(LIB_H) $(wildcard */*.h)
908 $(DIFF_OBJS): diffcore.h
909
builtin-http-fetch.c new
+77
@@ -0,0 +1,77 @@
1 +#include "cache.h"
2 +#include "walker.h"
3 +
4 +int cmd_http_fetch(int argc, const char **argv, const char *prefix)
5 +{
6 + struct walker *walker;
7 + int commits_on_stdin = 0;
8 + int commits;
9 + const char **write_ref = NULL;
10 + char **commit_id;
11 + const char *url;
12 + int arg = 1;
13 + int rc = 0;
14 + int get_tree = 0;
15 + int get_history = 0;
16 + int get_all = 0;
17 + int get_verbosely = 0;
18 + int get_recover = 0;
19 +
20 + git_config(git_default_config);
21 +
22 + while (arg < argc && argv[arg][0] == '-') {
23 + if (argv[arg][1] == 't') {
24 + get_tree = 1;
25 + } else if (argv[arg][1] == 'c') {
26 + get_history = 1;
27 + } else if (argv[arg][1] == 'a') {
28 + get_all = 1;
29 + get_tree = 1;
30 + get_history = 1;
31 + } else if (argv[arg][1] == 'v') {
32 + get_verbosely = 1;
33 + } else if (argv[arg][1] == 'w') {
34 + write_ref = &argv[arg + 1];
35 + arg++;
36 + } else if (!strcmp(argv[arg], "--recover")) {
37 + get_recover = 1;
38 + } else if (!strcmp(argv[arg], "--stdin")) {
39 + commits_on_stdin = 1;
40 + }
41 + arg++;
42 + }
43 + if (argc < arg + 2 - commits_on_stdin) {
44 + usage("git-http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url");
45 + return 1;
46 + }
47 + if (commits_on_stdin) {
48 + commits = walker_targets_stdin(&commit_id, &write_ref);
49 + } else {
50 + commit_id = (char **) &argv[arg++];
51 + commits = 1;
52 + }
53 + url = argv[arg];
54 +
55 + walker = get_http_walker(url);
56 + walker->get_tree = get_tree;
57 + walker->get_history = get_history;
58 + walker->get_all = get_all;
59 + walker->get_verbosely = get_verbosely;
60 + walker->get_recover = get_recover;
61 +
62 + rc = walker_fetch(walker, commits, commit_id, write_ref, url);
63 +
64 + if (commits_on_stdin)
65 + walker_targets_free(commits, commit_id, write_ref);
66 +
67 + if (walker->corrupt_object_found) {
68 + fprintf(stderr,
69 +"Some loose object were found to be corrupt, but they might be just\n"
70 +"a false '404 Not Found' error message sent with incorrect HTTP\n"
71 +"status code. Suggest running git-fsck.\n");
72 + }
73 +
74 + walker_free(walker);
75 +
76 + return rc;
77 +}
builtin.h
+1
@@ -40,6 +40,7 @@ extern int cmd_gc(int argc, const char **argv, const char *prefix);
40 extern int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
41 extern int cmd_grep(int argc, const char **argv, const char *prefix);
42 extern int cmd_help(int argc, const char **argv, const char *prefix);
43 +extern int cmd_http_fetch(int argc, const char **argv, const char *prefix);
44 extern int cmd_init_db(int argc, const char **argv, const char *prefix);
45 extern int cmd_log(int argc, const char **argv, const char *prefix);
46 extern int cmd_log_reflog(int argc, const char **argv, const char *prefix);
fetch.h deleted
-54
@@ -1,54 +0,0 @@
1 -#ifndef PULL_H
2 -#define PULL_H
3 -
4 -/*
5 - * Fetch object given SHA1 from the remote, and store it locally under
6 - * GIT_OBJECT_DIRECTORY. Return 0 on success, -1 on failure. To be
7 - * provided by the particular implementation.
8 - */
9 -extern int fetch(unsigned char *sha1);
10 -
11 -/*
12 - * Fetch the specified object and store it locally; fetch() will be
13 - * called later to determine success. To be provided by the particular
14 - * implementation.
15 - */
16 -extern void prefetch(unsigned char *sha1);
17 -
18 -/*
19 - * Fetch ref (relative to $GIT_DIR/refs) from the remote, and store
20 - * the 20-byte SHA1 in sha1. Return 0 on success, -1 on failure. To
21 - * be provided by the particular implementation.
22 - */
23 -extern int fetch_ref(char *ref, unsigned char *sha1);
24 -
25 -/* Set to fetch the target tree. */
26 -extern int get_tree;
27 -
28 -/* Set to fetch the commit history. */
29 -extern int get_history;
30 -
31 -/* Set to fetch the trees in the commit history. */
32 -extern int get_all;
33 -
34 -/* Set to be verbose */
35 -extern int get_verbosely;
36 -
37 -/* Set to check on all reachable objects. */
38 -extern int get_recover;
39 -
40 -/* Report what we got under get_verbosely */
41 -extern void pull_say(const char *, const char *);
42 -
43 -/* Load pull targets from stdin */
44 -extern int pull_targets_stdin(char ***target, const char ***write_ref);
45 -
46 -/* Free up loaded targets */
47 -extern void pull_targets_free(int targets, char **target, const char **write_ref);
48 -
49 -/* If write_ref is set, the ref filename to write the target value to. */
50 -/* If write_ref_log_details is set, additional text will appear in the ref log. */
51 -extern int pull(int targets, char **target, const char **write_ref,
52 - const char *write_ref_log_details);
53 -
54 -#endif /* PULL_H */
git.c
+3
@@ -344,6 +344,9 @@ static void handle_internal_command(int argc, const char **argv)
344 { "get-tar-commit-id", cmd_get_tar_commit_id },
345 { "grep", cmd_grep, RUN_SETUP | USE_PAGER },
346 { "help", cmd_help },
347 +#ifndef NO_CURL
348 + { "http-fetch", cmd_http_fetch, RUN_SETUP },
349 +#endif
350 { "init", cmd_init_db },
351 { "init-db", cmd_init_db },
352 { "log", cmd_log, RUN_SETUP | USE_PAGER },
http-push.c
-1
@@ -1,7 +1,6 @@
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 -#include "fetch.h"
4 #include "tag.h"
5 #include "blob.h"
6 #include "http.h"
http-walker.c renamed
+102 -126
@@ -1,19 +1,12 @@
1 #include "cache.h"
2 #include "commit.h"
3 #include "pack.h"
4 -#include "fetch.h"
4 +#include "walker.h"
5 #include "http.h"
6
7 #define PREV_BUF_SIZE 4096
8 #define RANGE_HEADER_SIZE 30
9
10 -static int commits_on_stdin;
11 -
12 -static int got_alternates = -1;
13 -static int corrupt_object_found;
14 -
15 -static struct curl_slist *no_pragma_header;
16 -
10 struct alt_base
11 {
12 char *base;
@@ -22,8 +15,6 @@ struct alt_base
15 struct alt_base *next;
16 };
17
25 -static struct alt_base *alt;
26 -
18 enum object_request_state {
19 WAITING,
20 ABORTED,
@@ -33,6 +24,7 @@ enum object_request_state {
24
25 struct object_request
26 {
27 + struct walker *walker;
28 unsigned char sha1[20];
29 struct alt_base *repo;
30 char *url;
@@ -53,6 +45,7 @@ struct object_request
45 };
46
47 struct alternates_request {
48 + struct walker *walker;
49 const char *base;
50 char *url;
51 struct buffer *buffer;
@@ -60,6 +53,13 @@ struct alternates_request {
53 int http_specific;
54 };
55
56 +struct walker_data {
57 + const char *url;
58 + int got_alternates;
59 + struct alt_base *alt;
60 + struct curl_slist *no_pragma_header;
61 +};
62 +
63 static struct object_request *object_queue_head;
64
65 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
@@ -103,11 +103,12 @@ static int missing__target(int code, int result)
103
104 #define missing_target(a) missing__target((a)->http_code, (a)->curl_result)
105
106 -static void fetch_alternates(const char *base);
106 +static void fetch_alternates(struct walker *walker, const char *base);
107
108 static void process_object_response(void *callback_data);
109
110 -static void start_object_request(struct object_request *obj_req)
110 +static void start_object_request(struct walker *walker,
111 + struct object_request *obj_req)
112 {
113 char *hex = sha1_to_hex(obj_req->sha1);
114 char prevfile[PATH_MAX];
@@ -120,6 +121,7 @@ static void start_object_request(struct object_request *obj_req)
121 char range[RANGE_HEADER_SIZE];
122 struct curl_slist *range_header = NULL;
123 struct active_request_slot *slot;
124 + struct walker_data *data = walker->data;
125
126 snprintf(prevfile, sizeof(prevfile), "%s.prev", obj_req->filename);
127 unlink(prevfile);
@@ -212,12 +214,12 @@ static void start_object_request(struct object_request *obj_req)
214 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
215 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, obj_req->errorstr);
216 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
215 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
217 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
218
219 /* If we have successfully processed data from a previous fetch
220 attempt, only fetch the data we don't already have. */
221 if (prev_posn>0) {
220 - if (get_verbosely)
222 + if (walker->get_verbosely)
223 fprintf(stderr,
224 "Resuming fetch of object %s at byte %ld\n",
225 hex, prev_posn);
@@ -268,13 +270,16 @@ static void finish_object_request(struct object_request *obj_req)
270 move_temp_to_file(obj_req->tmpfile, obj_req->filename);
271
272 if (obj_req->rename == 0)
271 - pull_say("got %s\n", sha1_to_hex(obj_req->sha1));
273 + walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
274 }
275
276 static void process_object_response(void *callback_data)
277 {
278 struct object_request *obj_req =
279 (struct object_request *)callback_data;
280 + struct walker *walker = obj_req->walker;
281 + struct walker_data *data = walker->data;
282 + struct alt_base *alt = data->alt;
283
284 obj_req->curl_result = obj_req->slot->curl_result;
285 obj_req->http_code = obj_req->slot->http_code;
@@ -283,13 +288,13 @@ static void process_object_response(void *callback_data)
288
289 /* Use alternates if necessary */
290 if (missing_target(obj_req)) {
286 - fetch_alternates(alt->base);
291 + fetch_alternates(walker, alt->base);
292 if (obj_req->repo->next != NULL) {
293 obj_req->repo =
294 obj_req->repo->next;
295 close(obj_req->local);
296 obj_req->local = -1;
292 - start_object_request(obj_req);
297 + start_object_request(walker, obj_req);
298 return;
299 }
300 }
@@ -317,7 +322,7 @@ static void release_object_request(struct object_request *obj_req)
322 }
323
324 #ifdef USE_CURL_MULTI
320 -static int fill_active_slot(void *unused)
325 +static int fill_active_slot(struct walker *walker)
326 {
327 struct object_request *obj_req;
328
@@ -326,7 +331,7 @@ static int fill_active_slot(void *unused)
331 if (has_sha1_file(obj_req->sha1))
332 obj_req->state = COMPLETE;
333 else {
329 - start_object_request(obj_req);
334 + start_object_request(walker, obj_req);
335 return 1;
336 }
337 }
@@ -335,15 +340,17 @@ static int fill_active_slot(void *unused)
340 }
341 #endif
342
338 -void prefetch(unsigned char *sha1)
343 +static void prefetch(struct walker *walker, unsigned char *sha1)
344 {
345 struct object_request *newreq;
346 struct object_request *tail;
347 + struct walker_data *data = walker->data;
348 char *filename = sha1_file_name(sha1);
349
350 newreq = xmalloc(sizeof(*newreq));
351 + newreq->walker = walker;
352 hashcpy(newreq->sha1, sha1);
346 - newreq->repo = alt;
353 + newreq->repo = data->alt;
354 newreq->url = NULL;
355 newreq->local = -1;
356 newreq->state = WAITING;
@@ -369,7 +376,7 @@ void prefetch(unsigned char *sha1)
376 #endif
377 }
378
372 -static int fetch_index(struct alt_base *repo, unsigned char *sha1)
379 +static int fetch_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
380 {
381 char *hex = sha1_to_hex(sha1);
382 char *filename;
@@ -378,6 +385,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
385 long prev_posn = 0;
386 char range[RANGE_HEADER_SIZE];
387 struct curl_slist *range_header = NULL;
388 + struct walker_data *data = walker->data;
389
390 FILE *indexfile;
391 struct active_request_slot *slot;
@@ -386,7 +394,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
394 if (has_pack_index(sha1))
395 return 0;
396
389 - if (get_verbosely)
397 + if (walker->get_verbosely)
398 fprintf(stderr, "Getting index for pack %s\n", hex);
399
400 url = xmalloc(strlen(repo->base) + 64);
@@ -404,14 +412,14 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
412 curl_easy_setopt(slot->curl, CURLOPT_FILE, indexfile);
413 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
414 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
407 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
415 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
416 slot->local = indexfile;
417
418 /* If there is data present from a previous transfer attempt,
419 resume where it left off */
420 prev_posn = ftell(indexfile);
421 if (prev_posn>0) {
414 - if (get_verbosely)
422 + if (walker->get_verbosely)
423 fprintf(stderr,
424 "Resuming fetch of index for pack %s at byte %ld\n",
425 hex, prev_posn);
@@ -437,13 +445,13 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
445 return move_temp_to_file(tmpfile, filename);
446 }
447
440 -static int setup_index(struct alt_base *repo, unsigned char *sha1)
448 +static int setup_index(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
449 {
450 struct packed_git *new_pack;
451 if (has_pack_file(sha1))
452 return 0; /* don't list this as something we can get */
453
446 - if (fetch_index(repo, sha1))
454 + if (fetch_index(walker, repo, sha1))
455 return -1;
456
457 new_pack = parse_pack_index(sha1);
@@ -456,8 +464,10 @@ static void process_alternates_response(void *callback_data)
464 {
465 struct alternates_request *alt_req =
466 (struct alternates_request *)callback_data;
467 + struct walker *walker = alt_req->walker;
468 + struct walker_data *cdata = walker->data;
469 struct active_request_slot *slot = alt_req->slot;
460 - struct alt_base *tail = alt;
470 + struct alt_base *tail = cdata->alt;
471 const char *base = alt_req->base;
472 static const char null_byte = '\0';
473 char *data;
@@ -478,7 +488,7 @@ static void process_alternates_response(void *callback_data)
488 if (slot->finished != NULL)
489 (*slot->finished) = 0;
490 if (!start_active_slot(slot)) {
481 - got_alternates = -1;
491 + cdata->got_alternates = -1;
492 slot->in_use = 0;
493 if (slot->finished != NULL)
494 (*slot->finished) = 1;
@@ -487,7 +497,7 @@ static void process_alternates_response(void *callback_data)
497 }
498 } else if (slot->curl_result != CURLE_OK) {
499 if (!missing_target(slot)) {
490 - got_alternates = -1;
500 + cdata->got_alternates = -1;
501 return;
502 }
503 }
@@ -564,7 +574,7 @@ static void process_alternates_response(void *callback_data)
574 memcpy(target + serverlen, data + i,
575 posn - i - 7);
576 target[serverlen + posn - i - 7] = 0;
567 - if (get_verbosely)
577 + if (walker->get_verbosely)
578 fprintf(stderr,
579 "Also look at %s\n", target);
580 newalt = xmalloc(sizeof(*newalt));
@@ -581,39 +591,40 @@ static void process_alternates_response(void *callback_data)
591 i = posn + 1;
592 }
593
584 - got_alternates = 1;
594 + cdata->got_alternates = 1;
595 }
596
587 -static void fetch_alternates(const char *base)
597 +static void fetch_alternates(struct walker *walker, const char *base)
598 {
599 struct buffer buffer;
600 char *url;
601 char *data;
602 struct active_request_slot *slot;
603 struct alternates_request alt_req;
604 + struct walker_data *cdata = walker->data;
605
606 /* If another request has already started fetching alternates,
607 wait for them to arrive and return to processing this request's
608 curl message */
609 #ifdef USE_CURL_MULTI
599 - while (got_alternates == 0) {
610 + while (cdata->got_alternates == 0) {
611 step_active_slots();
612 }
613 #endif
614
615 /* Nothing to do if they've already been fetched */
605 - if (got_alternates == 1)
616 + if (cdata->got_alternates == 1)
617 return;
618
619 /* Start the fetch */
609 - got_alternates = 0;
620 + cdata->got_alternates = 0;
621
622 data = xmalloc(4096);
623 buffer.size = 4096;
624 buffer.posn = 0;
625 buffer.buffer = data;
626
616 - if (get_verbosely)
627 + if (walker->get_verbosely)
628 fprintf(stderr, "Getting alternates list for %s\n", base);
629
630 url = xmalloc(strlen(base) + 31);
@@ -623,6 +634,7 @@ static void fetch_alternates(const char *base)
634 may fail and need to have alternates loaded before continuing */
635 slot = get_active_slot();
636 slot->callback_func = process_alternates_response;
637 + alt_req.walker = walker;
638 slot->callback_data = &alt_req;
639
640 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
@@ -638,13 +650,13 @@ static void fetch_alternates(const char *base)
650 if (start_active_slot(slot))
651 run_active_slot(slot);
652 else
641 - got_alternates = -1;
653 + cdata->got_alternates = -1;
654
655 free(data);
656 free(url);
657 }
658
647 -static int fetch_indices(struct alt_base *repo)
659 +static int fetch_indices(struct walker *walker, struct alt_base *repo)
660 {
661 unsigned char sha1[20];
662 char *url;
@@ -663,7 +675,7 @@ static int fetch_indices(struct alt_base *repo)
675 buffer.posn = 0;
676 buffer.buffer = data;
677
666 - if (get_verbosely)
678 + if (walker->get_verbosely)
679 fprintf(stderr, "Getting pack list for %s\n", repo->base);
680
681 url = xmalloc(strlen(repo->base) + 21);
@@ -703,7 +715,7 @@ static int fetch_indices(struct alt_base *repo)
715 !prefixcmp(data + i, " pack-") &&
716 !prefixcmp(data + i + 46, ".pack\n")) {
717 get_sha1_hex(data + i + 6, sha1);
706 - setup_index(repo, sha1);
718 + setup_index(walker, repo, sha1);
719 i += 51;
720 break;
721 }
@@ -719,7 +731,7 @@ static int fetch_indices(struct alt_base *repo)
731 return 0;
732 }
733
722 -static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
734 +static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
735 {
736 char *url;
737 struct packed_git *target;
@@ -731,17 +743,18 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
743 long prev_posn = 0;
744 char range[RANGE_HEADER_SIZE];
745 struct curl_slist *range_header = NULL;
746 + struct walker_data *data = walker->data;
747
748 struct active_request_slot *slot;
749 struct slot_results results;
750
738 - if (fetch_indices(repo))
751 + if (fetch_indices(walker, repo))
752 return -1;
753 target = find_sha1_pack(sha1, repo->packs);
754 if (!target)
755 return -1;
756
744 - if (get_verbosely) {
757 + if (walker->get_verbosely) {
758 fprintf(stderr, "Getting pack %s\n",
759 sha1_to_hex(target->sha1));
760 fprintf(stderr, " which contains %s\n",
@@ -764,14 +777,14 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
777 curl_easy_setopt(slot->curl, CURLOPT_FILE, packfile);
778 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
779 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
767 - curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
780 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, data->no_pragma_header);
781 slot->local = packfile;
782
783 /* If there is data present from a previous transfer attempt,
784 resume where it left off */
785 prev_posn = ftell(packfile);
786 if (prev_posn>0) {
774 - if (get_verbosely)
787 + if (walker->get_verbosely)
788 fprintf(stderr,
789 "Resuming fetch of pack %s at byte %ld\n",
790 sha1_to_hex(target->sha1), prev_posn);
@@ -825,7 +838,7 @@ static void abort_object_request(struct object_request *obj_req)
838 release_object_request(obj_req);
839 }
840
828 -static int fetch_object(struct alt_base *repo, unsigned char *sha1)
841 +static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
842 {
843 char *hex = sha1_to_hex(sha1);
844 int ret = 0;
@@ -846,7 +859,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
859 step_active_slots();
860 }
861 #else
849 - start_object_request(obj_req);
862 + start_object_request(walker, obj_req);
863 #endif
864
865 while (obj_req->state == ACTIVE) {
@@ -867,7 +880,7 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
880 obj_req->errorstr, obj_req->curl_result,
881 obj_req->http_code, hex);
882 } else if (obj_req->zret != Z_STREAM_END) {
870 - corrupt_object_found++;
883 + walker->corrupt_object_found++;
884 ret = error("File %s (%s) corrupt", hex, obj_req->url);
885 } else if (hashcmp(obj_req->sha1, obj_req->real_sha1)) {
886 ret = error("File %s has bad hash", hex);
@@ -880,20 +893,21 @@ static int fetch_object(struct alt_base *repo, unsigned char *sha1)
893 return ret;
894 }
895
883 -int fetch(unsigned char *sha1)
896 +static int fetch(struct walker *walker, unsigned char *sha1)
897 {
885 - struct alt_base *altbase = alt;
898 + struct walker_data *data = walker->data;
899 + struct alt_base *altbase = data->alt;
900
887 - if (!fetch_object(altbase, sha1))
901 + if (!fetch_object(walker, altbase, sha1))
902 return 0;
903 while (altbase) {
890 - if (!fetch_pack(altbase, sha1))
904 + if (!fetch_pack(walker, altbase, sha1))
905 return 0;
892 - fetch_alternates(alt->base);
906 + fetch_alternates(walker, data->alt->base);
907 altbase = altbase->next;
908 }
909 return error("Unable to find %s under %s", sha1_to_hex(sha1),
896 - alt->base);
910 + data->alt->base);
911 }
912
913 static inline int needs_quote(int ch)
@@ -942,12 +956,13 @@ static char *quote_ref_url(const char *base, const char *ref)
956 return qref;
957 }
958
945 -int fetch_ref(char *ref, unsigned char *sha1)
959 +static int fetch_ref(struct walker *walker, char *ref, unsigned char *sha1)
960 {
961 char *url;
962 char hex[42];
963 struct buffer buffer;
950 - const char *base = alt->base;
964 + struct walker_data *data = walker->data;
965 + const char *base = data->alt->base;
966 struct active_request_slot *slot;
967 struct slot_results results;
968 buffer.size = 41;
@@ -976,84 +991,45 @@ int fetch_ref(char *ref, unsigned char *sha1)
991 return 0;
992 }
993
979 -int main(int argc, const char **argv)
994 +static void cleanup(struct walker *walker)
995 +{
996 + struct walker_data *data = walker->data;
997 + http_cleanup();
998 +
999 + curl_slist_free_all(data->no_pragma_header);
1000 +}
1001 +
1002 +struct walker *get_http_walker(const char *url)
1003 {
981 - int commits;
982 - const char **write_ref = NULL;
983 - char **commit_id;
984 - const char *url;
1004 char *s;
986 - int arg = 1;
987 - int rc = 0;
988 -
989 - setup_git_directory();
990 - git_config(git_default_config);
991 -
992 - while (arg < argc && argv[arg][0] == '-') {
993 - if (argv[arg][1] == 't') {
994 - get_tree = 1;
995 - } else if (argv[arg][1] == 'c') {
996 - get_history = 1;
997 - } else if (argv[arg][1] == 'a') {
998 - get_all = 1;
999 - get_tree = 1;
1000 - get_history = 1;
1001 - } else if (argv[arg][1] == 'v') {
1002 - get_verbosely = 1;
1003 - } else if (argv[arg][1] == 'w') {
1004 - write_ref = &argv[arg + 1];
1005 - arg++;
1006 - } else if (!strcmp(argv[arg], "--recover")) {
1007 - get_recover = 1;
1008 - } else if (!strcmp(argv[arg], "--stdin")) {
1009 - commits_on_stdin = 1;
1010 - }
1011 - arg++;
1012 - }
1013 - if (argc < arg + 2 - commits_on_stdin) {
1014 - usage("git-http-fetch [-c] [-t] [-a] [-v] [--recover] [-w ref] [--stdin] commit-id url");
1015 - return 1;
1016 - }
1017 - if (commits_on_stdin) {
1018 - commits = pull_targets_stdin(&commit_id, &write_ref);
1019 - } else {
1020 - commit_id = (char **) &argv[arg++];
1021 - commits = 1;
1022 - }
1023 - url = argv[arg];
1005 + struct walker_data *data = xmalloc(sizeof(struct walker_data));
1006 + struct walker *walker = xmalloc(sizeof(struct walker));
1007
1008 http_init();
1009
1027 - no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
1010 + data->no_pragma_header = curl_slist_append(NULL, "Pragma:");
1011
1029 - alt = xmalloc(sizeof(*alt));
1030 - alt->base = xmalloc(strlen(url) + 1);
1031 - strcpy(alt->base, url);
1032 - for (s = alt->base + strlen(alt->base) - 1; *s == '/'; --s)
1012 + data->alt = xmalloc(sizeof(*data->alt));
1013 + data->alt->base = xmalloc(strlen(url) + 1);
1014 + strcpy(data->alt->base, url);
1015 + for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
1016 *s = 0;
1034 - alt->got_indices = 0;
1035 - alt->packs = NULL;
1036 - alt->next = NULL;
1017
1038 -#ifdef USE_CURL_MULTI
1039 - add_fill_function(NULL, fill_active_slot);
1040 -#endif
1018 + data->alt->got_indices = 0;
1019 + data->alt->packs = NULL;
1020 + data->alt->next = NULL;
1021 + data->got_alternates = -1;
1022
1042 - if (pull(commits, commit_id, write_ref, url))
1043 - rc = 1;
1023 + walker->corrupt_object_found = 0;
1024 + walker->fetch = fetch;
1025 + walker->fetch_ref = fetch_ref;
1026 + walker->prefetch = prefetch;
1027 + walker->cleanup = cleanup;
1028 + walker->data = data;
1029
1045 - http_cleanup();
1046 -
1047 - curl_slist_free_all(no_pragma_header);
1048 -
1049 - if (commits_on_stdin)
1050 - pull_targets_free(commits, commit_id, write_ref);
1030 +#ifdef USE_CURL_MULTI
1031 + add_fill_function(walker, (int (*)(void *)) fill_active_slot);
1032 +#endif
1033
1052 - if (corrupt_object_found) {
1053 - fprintf(stderr,
1054 -"Some loose object were found to be corrupt, but they might be just\n"
1055 -"a false '404 Not Found' error message sent with incorrect HTTP\n"
1056 -"status code. Suggest running git-fsck.\n");
1057 - }
1058 - return rc;
1034 + return walker;
1035 }
walker.c renamed
+42 -41
@@ -1,5 +1,5 @@
1 #include "cache.h"
2 -#include "fetch.h"
2 +#include "walker.h"
3 #include "commit.h"
4 #include "tree.h"
5 #include "tree-walk.h"
@@ -8,16 +8,11 @@
8 #include "refs.h"
9 #include "strbuf.h"
10
11 -int get_tree = 0;
12 -int get_history = 0;
13 -int get_all = 0;
14 -int get_verbosely = 0;
15 -int get_recover = 0;
11 static unsigned char current_commit_sha1[20];
12
18 -void pull_say(const char *fmt, const char *hex)
13 +void walker_say(struct walker *walker, const char *fmt, const char *hex)
14 {
20 - if (get_verbosely)
15 + if (walker->get_verbosely)
16 fprintf(stderr, fmt, hex);
17 }
18
@@ -32,9 +27,9 @@ static void report_missing(const struct object *obj)
27 sha1_to_hex(current_commit_sha1));
28 }
29
35 -static int process(struct object *obj);
30 +static int process(struct walker *walker, struct object *obj);
31
37 -static int process_tree(struct tree *tree)
32 +static int process_tree(struct walker *walker, struct tree *tree)
33 {
34 struct tree_desc desc;
35 struct name_entry entry;
@@ -59,7 +54,7 @@ static int process_tree(struct tree *tree)
54 if (blob)
55 obj = &blob->object;
56 }
62 - if (!obj || process(obj))
57 + if (!obj || process(walker, obj))
58 return -1;
59 }
60 free(tree->buffer);
@@ -74,7 +69,7 @@ static int process_tree(struct tree *tree)
69
70 static struct commit_list *complete = NULL;
71
77 -static int process_commit(struct commit *commit)
72 +static int process_commit(struct walker *walker, struct commit *commit)
73 {
74 if (parse_commit(commit))
75 return -1;
@@ -88,43 +83,43 @@ static int process_commit(struct commit *commit)
83
84 hashcpy(current_commit_sha1, commit->object.sha1);
85
91 - pull_say("walk %s\n", sha1_to_hex(commit->object.sha1));
86 + walker_say(walker, "walk %s\n", sha1_to_hex(commit->object.sha1));
87
93 - if (get_tree) {
94 - if (process(&commit->tree->object))
88 + if (walker->get_tree) {
89 + if (process(walker, &commit->tree->object))
90 return -1;
96 - if (!get_all)
97 - get_tree = 0;
91 + if (!walker->get_all)
92 + walker->get_tree = 0;
93 }
99 - if (get_history) {
94 + if (walker->get_history) {
95 struct commit_list *parents = commit->parents;
96 for (; parents; parents = parents->next) {
102 - if (process(&parents->item->object))
97 + if (process(walker, &parents->item->object))
98 return -1;
99 }
100 }
101 return 0;
102 }
103
109 -static int process_tag(struct tag *tag)
104 +static int process_tag(struct walker *walker, struct tag *tag)
105 {
106 if (parse_tag(tag))
107 return -1;
113 - return process(tag->tagged);
108 + return process(walker, tag->tagged);
109 }
110
111 static struct object_list *process_queue = NULL;
112 static struct object_list **process_queue_end = &process_queue;
113
119 -static int process_object(struct object *obj)
114 +static int process_object(struct walker *walker, struct object *obj)
115 {
116 if (obj->type == OBJ_COMMIT) {
122 - if (process_commit((struct commit *)obj))
117 + if (process_commit(walker, (struct commit *)obj))
118 return -1;
119 return 0;
120 }
121 if (obj->type == OBJ_TREE) {
127 - if (process_tree((struct tree *)obj))
122 + if (process_tree(walker, (struct tree *)obj))
123 return -1;
124 return 0;
125 }
@@ -132,7 +127,7 @@ static int process_object(struct object *obj)
127 return 0;
128 }
129 if (obj->type == OBJ_TAG) {
135 - if (process_tag((struct tag *)obj))
130 + if (process_tag(walker, (struct tag *)obj))
131 return -1;
132 return 0;
133 }
@@ -141,7 +136,7 @@ static int process_object(struct object *obj)
136 typename(obj->type), sha1_to_hex(obj->sha1));
137 }
138
144 -static int process(struct object *obj)
139 +static int process(struct walker *walker, struct object *obj)
140 {
141 if (obj->flags & SEEN)
142 return 0;
@@ -154,7 +149,7 @@ static int process(struct object *obj)
149 else {
150 if (obj->flags & COMPLETE)
151 return 0;
157 - prefetch(obj->sha1);
152 + walker->prefetch(walker, obj->sha1);
153 }
154
155 object_list_insert(obj, process_queue_end);
@@ -162,7 +157,7 @@ static int process(struct object *obj)
157 return 0;
158 }
159
165 -static int loop(void)
160 +static int loop(struct walker *walker)
161 {
162 struct object_list *elem;
163
@@ -178,25 +173,25 @@ static int loop(void)
173 * the queue because we needed to fetch it first.
174 */
175 if (! (obj->flags & TO_SCAN)) {
181 - if (fetch(obj->sha1)) {
176 + if (walker->fetch(walker, obj->sha1)) {
177 report_missing(obj);
178 return -1;
179 }
180 }
181 if (!obj->type)
182 parse_object(obj->sha1);
188 - if (process_object(obj))
183 + if (process_object(walker, obj))
184 return -1;
185 }
186 return 0;
187 }
188
194 -static int interpret_target(char *target, unsigned char *sha1)
189 +static int interpret_target(struct walker *walker, char *target, unsigned char *sha1)
190 {
191 if (!get_sha1_hex(target, sha1))
192 return 0;
193 if (!check_ref_format(target)) {
199 - if (!fetch_ref(target, sha1)) {
194 + if (!walker->fetch_ref(walker, target, sha1)) {
195 return 0;
196 }
197 }
@@ -213,7 +208,7 @@ static int mark_complete(const char *path, const unsigned char *sha1, int flag,
208 return 0;
209 }
210
216 -int pull_targets_stdin(char ***target, const char ***write_ref)
211 +int walker_targets_stdin(char ***target, const char ***write_ref)
212 {
213 int targets = 0, targets_alloc = 0;
214 struct strbuf buf;
@@ -243,7 +238,7 @@ int pull_targets_stdin(char ***target, const char ***write_ref)
238 return targets;
239 }
240
246 -void pull_targets_free(int targets, char **target, const char **write_ref)
241 +void walker_targets_free(int targets, char **target, const char **write_ref)
242 {
243 while (targets--) {
244 free(target[targets]);
@@ -252,8 +247,8 @@ void pull_targets_free(int targets, char **target, const char **write_ref)
247 }
248 }
249
255 -int pull(int targets, char **target, const char **write_ref,
256 - const char *write_ref_log_details)
250 +int walker_fetch(struct walker *walker, int targets, char **target,
251 + const char **write_ref, const char *write_ref_log_details)
252 {
253 struct ref_lock **lock = xcalloc(targets, sizeof(struct ref_lock *));
254 unsigned char *sha1 = xmalloc(targets * 20);
@@ -275,19 +270,19 @@ int pull(int targets, char **target, const char **write_ref,
270 }
271 }
272
278 - if (!get_recover)
273 + if (!walker->get_recover)
274 for_each_ref(mark_complete, NULL);
275
276 for (i = 0; i < targets; i++) {
282 - if (interpret_target(target[i], &sha1[20 * i])) {
277 + if (interpret_target(walker, target[i], &sha1[20 * i])) {
278 error("Could not interpret %s as something to pull", target[i]);
279 goto unlock_and_fail;
280 }
286 - if (process(lookup_unknown_object(&sha1[20 * i])))
281 + if (process(walker, lookup_unknown_object(&sha1[20 * i])))
282 goto unlock_and_fail;
283 }
284
290 - if (loop())
285 + if (loop(walker))
286 goto unlock_and_fail;
287
288 if (write_ref_log_details) {
@@ -308,10 +303,16 @@ int pull(int targets, char **target, const char **write_ref,
303
304 return 0;
305
311 -
306 unlock_and_fail:
307 for (i = 0; i < targets; i++)
308 if (lock[i])
309 unlock_ref(lock[i]);
310 +
311 return -1;
312 }
313 +
314 +void walker_free(struct walker *walker)
315 +{
316 + walker->cleanup(walker);
317 + free(walker);
318 +}
walker.h new
+37
@@ -0,0 +1,37 @@
1 +#ifndef WALKER_H
2 +#define WALKER_H
3 +
4 +struct walker {
5 + void *data;
6 + int (*fetch_ref)(struct walker *, char *ref, unsigned char *sha1);
7 + void (*prefetch)(struct walker *, unsigned char *sha1);
8 + int (*fetch)(struct walker *, unsigned char *sha1);
9 + void (*cleanup)(struct walker *);
10 + int get_tree;
11 + int get_history;
12 + int get_all;
13 + int get_verbosely;
14 + int get_recover;
15 +
16 + int corrupt_object_found;
17 +};
18 +
19 +/* Report what we got under get_verbosely */
20 +void walker_say(struct walker *walker, const char *, const char *);
21 +
22 +/* Load pull targets from stdin */
23 +int walker_targets_stdin(char ***target, const char ***write_ref);
24 +
25 +/* Free up loaded targets */
26 +void walker_targets_free(int targets, char **target, const char **write_ref);
27 +
28 +/* If write_ref is set, the ref filename to write the target value to. */
29 +/* If write_ref_log_details is set, additional text will appear in the ref log. */
30 +int walker_fetch(struct walker *impl, int targets, char **target,
31 + const char **write_ref, const char *write_ref_log_details);
32 +
33 +void walker_free(struct walker *walker);
34 +
35 +struct walker *get_http_walker(const char *url);
36 +
37 +#endif /* WALKER_H */