14 static z_stream stream;
26 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
27 struct buffer *buffer) {
28 size_t size = eltsize * nmemb;
29 if (size > buffer->size - buffer->posn)
30 size = buffer->size - buffer->posn;
31 memcpy(buffer->buffer + buffer->posn, ptr, size);
36 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
38 unsigned char expn[4096];
39 size_t size = eltsize * nmemb;
42 ssize_t retval = write(local, ptr + posn, size - posn);
46 } while (posn < size);
48 stream.avail_in = size;
51 stream.next_out = expn;
52 stream.avail_out = sizeof(expn);
53 zret = inflate(&stream, Z_SYNC_FLUSH);
54 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
55 } while (stream.avail_in && zret == Z_OK);
59 int fetch(unsigned char *sha1)
61 char *hex = sha1_to_hex(sha1);
62 char *filename = sha1_file_name(sha1);
63 unsigned char real_sha1[20];
67 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
70 return error("Couldn't open %s\n", filename);
72 memset(&stream, 0, sizeof(stream));
78 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
79 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
81 url = xmalloc(strlen(base) + 50);
83 posn = url + strlen(base);
84 strcpy(posn, "objects/");
89 strcpy(posn, hex + 2);
91 curl_easy_setopt(curl, CURLOPT_URL, url);
93 if (curl_easy_perform(curl))
94 return error("Couldn't get %s for %s\n", url, hex);
98 SHA1_Final(real_sha1, &c);
99 if (zret != Z_STREAM_END) {
101 return error("File %s (%s) corrupt\n", hex, url);
103 if (memcmp(sha1, real_sha1, 20)) {
105 return error("File %s has bad hash\n", hex);
108 pull_say("got %s\n", hex);
112 int fetch_ref(char *ref, unsigned char *sha1)
116 struct buffer buffer;
122 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
123 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
125 url = xmalloc(strlen(base) + 6 + strlen(ref));
127 posn = url + strlen(base);
128 strcpy(posn, "refs/");
132 curl_easy_setopt(curl, CURLOPT_URL, url);
134 if (curl_easy_perform(curl))
135 return error("Couldn't get %s for %s\n", url, ref);
138 get_sha1_hex(hex, sha1);
142 int main(int argc, char **argv)
148 while (arg < argc && argv[arg][0] == '-') {
149 if (argv[arg][1] == 't') {
151 } else if (argv[arg][1] == 'c') {
153 } else if (argv[arg][1] == 'a') {
157 } else if (argv[arg][1] == 'v') {
159 } else if (argv[arg][1] == 'w') {
160 write_ref = argv[arg + 1];
165 if (argc < arg + 2) {
166 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
169 commit_id = argv[arg];
172 curl_global_init(CURL_GLOBAL_ALL);
174 curl = curl_easy_init();
181 curl_global_cleanup();