9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
21 static z_stream stream;
26 static int curl_ssl_verify;
35 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
36 struct buffer *buffer) {
37 size_t size = eltsize * nmemb;
38 if (size > buffer->size - buffer->posn)
39 size = buffer->size - buffer->posn;
40 memcpy(buffer->buffer + buffer->posn, ptr, size);
45 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
47 unsigned char expn[4096];
48 size_t size = eltsize * nmemb;
51 ssize_t retval = write(local, ptr + posn, size - posn);
55 } while (posn < size);
57 stream.avail_in = size;
60 stream.next_out = expn;
61 stream.avail_out = sizeof(expn);
62 zret = inflate(&stream, Z_SYNC_FLUSH);
63 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
64 } while (stream.avail_in && zret == Z_OK);
68 int fetch(unsigned char *sha1)
70 char *hex = sha1_to_hex(sha1);
71 char *filename = sha1_file_name(sha1);
72 unsigned char real_sha1[20];
76 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
79 return error("Couldn't open %s\n", filename);
81 memset(&stream, 0, sizeof(stream));
87 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
88 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
90 url = xmalloc(strlen(base) + 50);
92 posn = url + strlen(base);
93 strcpy(posn, "objects/");
98 strcpy(posn, hex + 2);
100 curl_easy_setopt(curl, CURLOPT_URL, url);
102 if (curl_easy_perform(curl))
103 return error("Couldn't get %s for %s\n", url, hex);
107 SHA1_Final(real_sha1, &c);
108 if (zret != Z_STREAM_END) {
110 return error("File %s (%s) corrupt\n", hex, url);
112 if (memcmp(sha1, real_sha1, 20)) {
114 return error("File %s has bad hash\n", hex);
117 pull_say("got %s\n", hex);
121 int fetch_ref(char *ref, unsigned char *sha1)
125 struct buffer buffer;
131 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
132 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
134 url = xmalloc(strlen(base) + 6 + strlen(ref));
136 posn = url + strlen(base);
137 strcpy(posn, "refs/");
141 curl_easy_setopt(curl, CURLOPT_URL, url);
143 if (curl_easy_perform(curl))
144 return error("Couldn't get %s for %s\n", url, ref);
147 get_sha1_hex(hex, sha1);
151 int main(int argc, char **argv)
157 while (arg < argc && argv[arg][0] == '-') {
158 if (argv[arg][1] == 't') {
160 } else if (argv[arg][1] == 'c') {
162 } else if (argv[arg][1] == 'a') {
166 } else if (argv[arg][1] == 'v') {
168 } else if (argv[arg][1] == 'w') {
169 write_ref = argv[arg + 1];
174 if (argc < arg + 2) {
175 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
178 commit_id = argv[arg];
181 curl_global_init(CURL_GLOBAL_ALL);
183 curl = curl_easy_init();
185 curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
186 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
187 #if LIBCURL_VERSION_NUM >= 0x070907
188 curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
196 curl_global_cleanup();