14 static z_stream stream;
19 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
21 unsigned char expn[4096];
22 size_t size = eltsize * nmemb;
25 ssize_t retval = write(local, ptr + posn, size - posn);
29 } while (posn < size);
31 stream.avail_in = size;
34 stream.next_out = expn;
35 stream.avail_out = sizeof(expn);
36 zret = inflate(&stream, Z_SYNC_FLUSH);
37 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
38 } while (stream.avail_in && zret == Z_OK);
42 int fetch(unsigned char *sha1)
44 char *hex = sha1_to_hex(sha1);
45 char *filename = sha1_file_name(sha1);
46 unsigned char real_sha1[20];
50 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
53 return error("Couldn't open %s\n", filename);
55 memset(&stream, 0, sizeof(stream));
61 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
62 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
64 url = xmalloc(strlen(base) + 50);
66 posn = url + strlen(base);
67 strcpy(posn, "objects/");
72 strcpy(posn, hex + 2);
74 curl_easy_setopt(curl, CURLOPT_URL, url);
76 if (curl_easy_perform(curl))
77 return error("Couldn't get %s for %s\n", url, hex);
81 SHA1_Final(real_sha1, &c);
82 if (zret != Z_STREAM_END) {
84 return error("File %s (%s) corrupt\n", hex, url);
86 if (memcmp(sha1, real_sha1, 20)) {
88 return error("File %s has bad hash\n", hex);
91 pull_say("got %s\n", hex);
95 int fetch_ref(char *ref, unsigned char *sha1)
100 int main(int argc, char **argv)
106 while (arg < argc && argv[arg][0] == '-') {
107 if (argv[arg][1] == 't') {
109 } else if (argv[arg][1] == 'c') {
111 } else if (argv[arg][1] == 'd') {
113 } else if (!strcmp(argv[arg], "--recover")) {
115 } else if (argv[arg][1] == 'a') {
119 } else if (argv[arg][1] == 'v') {
124 if (argc < arg + 2) {
125 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] commit-id url");
128 commit_id = argv[arg];
131 curl_global_init(CURL_GLOBAL_ALL);
133 curl = curl_easy_init();
140 curl_global_cleanup();