[PATCH] Fix support for old libcurl
[git/dscho.git] / http-pull.c
blob38047e695b8dfabbdbe824486b1fae9ff5664ea4
1 #include "cache.h"
2 #include "commit.h"
4 #include "pull.h"
6 #include <curl/curl.h>
7 #include <curl/easy.h>
9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
11 #endif
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
14 #endif
16 static CURL *curl;
18 static char *base;
20 static SHA_CTX c;
21 static z_stream stream;
23 static int local;
24 static int zret;
26 static int curl_ssl_verify;
28 struct buffer
30 size_t posn;
31 size_t size;
32 void *buffer;
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);
41 buffer->posn += size;
42 return size;
45 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
46 void *data) {
47 unsigned char expn[4096];
48 size_t size = eltsize * nmemb;
49 int posn = 0;
50 do {
51 ssize_t retval = write(local, ptr + posn, size - posn);
52 if (retval < 0)
53 return posn;
54 posn += retval;
55 } while (posn < size);
57 stream.avail_in = size;
58 stream.next_in = ptr;
59 do {
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);
65 return size;
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];
73 char *url;
74 char *posn;
76 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
78 if (local < 0)
79 return error("Couldn't open %s\n", filename);
81 memset(&stream, 0, sizeof(stream));
83 inflateInit(&stream);
85 SHA1_Init(&c);
87 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
88 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
90 url = xmalloc(strlen(base) + 50);
91 strcpy(url, base);
92 posn = url + strlen(base);
93 strcpy(posn, "objects/");
94 posn += 8;
95 memcpy(posn, hex, 2);
96 posn += 2;
97 *(posn++) = '/';
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);
105 close(local);
106 inflateEnd(&stream);
107 SHA1_Final(real_sha1, &c);
108 if (zret != Z_STREAM_END) {
109 unlink(filename);
110 return error("File %s (%s) corrupt\n", hex, url);
112 if (memcmp(sha1, real_sha1, 20)) {
113 unlink(filename);
114 return error("File %s has bad hash\n", hex);
117 pull_say("got %s\n", hex);
118 return 0;
121 int fetch_ref(char *ref, unsigned char *sha1)
123 char *url, *posn;
124 char hex[42];
125 struct buffer buffer;
126 buffer.size = 41;
127 buffer.posn = 0;
128 buffer.buffer = hex;
129 hex[41] = '\0';
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));
135 strcpy(url, base);
136 posn = url + strlen(base);
137 strcpy(posn, "refs/");
138 posn += 5;
139 strcpy(posn, ref);
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);
146 hex[40] = '\0';
147 get_sha1_hex(hex, sha1);
148 return 0;
151 int main(int argc, char **argv)
153 char *commit_id;
154 char *url;
155 int arg = 1;
157 while (arg < argc && argv[arg][0] == '-') {
158 if (argv[arg][1] == 't') {
159 get_tree = 1;
160 } else if (argv[arg][1] == 'c') {
161 get_history = 1;
162 } else if (argv[arg][1] == 'a') {
163 get_all = 1;
164 get_tree = 1;
165 get_history = 1;
166 } else if (argv[arg][1] == 'v') {
167 get_verbosely = 1;
168 } else if (argv[arg][1] == 'w') {
169 write_ref = argv[arg + 1];
170 arg++;
172 arg++;
174 if (argc < arg + 2) {
175 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
176 return 1;
178 commit_id = argv[arg];
179 url = argv[arg + 1];
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);
189 #endif
191 base = url;
193 if (pull(commit_id))
194 return 1;
196 curl_global_cleanup();
197 return 0;