[PATCH] Remove the explicit Makefile dependencies description
[git.git] / http-pull.c
1 #include "cache.h"
2 #include "commit.h"
3
4 #include "pull.h"
5
6 #include <curl/curl.h>
7 #include <curl/easy.h>
8
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
15 #if LIBCURL_VERSION_NUM < 0x070907
16 #define curl_easy_setopt(a, b, c) do { /* nothing */ } while(0)
17 #endif
18
19 static CURL *curl;
20
21 static char *base;
22
23 static SHA_CTX c;
24 static z_stream stream;
25
26 static int local;
27 static int zret;
28
29 static int curl_ssl_verify;
30
31 struct buffer
32 {
33         size_t posn;
34         size_t size;
35         void *buffer;
36 };
37
38 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
39                             struct buffer *buffer) {
40         size_t size = eltsize * nmemb;
41         if (size > buffer->size - buffer->posn)
42                 size = buffer->size - buffer->posn;
43         memcpy(buffer->buffer + buffer->posn, ptr, size);
44         buffer->posn += size;
45         return size;
46 }
47
48 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, 
49                                void *data) {
50         unsigned char expn[4096];
51         size_t size = eltsize * nmemb;
52         int posn = 0;
53         do {
54                 ssize_t retval = write(local, ptr + posn, size - posn);
55                 if (retval < 0)
56                         return posn;
57                 posn += retval;
58         } while (posn < size);
59
60         stream.avail_in = size;
61         stream.next_in = ptr;
62         do {
63                 stream.next_out = expn;
64                 stream.avail_out = sizeof(expn);
65                 zret = inflate(&stream, Z_SYNC_FLUSH);
66                 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
67         } while (stream.avail_in && zret == Z_OK);
68         return size;
69 }
70
71 int fetch(unsigned char *sha1)
72 {
73         char *hex = sha1_to_hex(sha1);
74         char *filename = sha1_file_name(sha1);
75         unsigned char real_sha1[20];
76         char *url;
77         char *posn;
78
79         local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
80
81         if (local < 0)
82                 return error("Couldn't open %s\n", filename);
83
84         memset(&stream, 0, sizeof(stream));
85
86         inflateInit(&stream);
87
88         SHA1_Init(&c);
89
90         curl_easy_setopt(curl, CURLOPT_FILE, NULL);
91         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
92
93         url = xmalloc(strlen(base) + 50);
94         strcpy(url, base);
95         posn = url + strlen(base);
96         strcpy(posn, "objects/");
97         posn += 8;
98         memcpy(posn, hex, 2);
99         posn += 2;
100         *(posn++) = '/';
101         strcpy(posn, hex + 2);
102
103         curl_easy_setopt(curl, CURLOPT_URL, url);
104
105         if (curl_easy_perform(curl))
106                 return error("Couldn't get %s for %s\n", url, hex);
107
108         close(local);
109         inflateEnd(&stream);
110         SHA1_Final(real_sha1, &c);
111         if (zret != Z_STREAM_END) {
112                 unlink(filename);
113                 return error("File %s (%s) corrupt\n", hex, url);
114         }
115         if (memcmp(sha1, real_sha1, 20)) {
116                 unlink(filename);
117                 return error("File %s has bad hash\n", hex);
118         }
119         
120         pull_say("got %s\n", hex);
121         return 0;
122 }
123
124 int fetch_ref(char *ref, unsigned char *sha1)
125 {
126         char *url, *posn;
127         char hex[42];
128         struct buffer buffer;
129         buffer.size = 41;
130         buffer.posn = 0;
131         buffer.buffer = hex;
132         hex[41] = '\0';
133         
134         curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
135         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
136
137         url = xmalloc(strlen(base) + 6 + strlen(ref));
138         strcpy(url, base);
139         posn = url + strlen(base);
140         strcpy(posn, "refs/");
141         posn += 5;
142         strcpy(posn, ref);
143
144         curl_easy_setopt(curl, CURLOPT_URL, url);
145
146         if (curl_easy_perform(curl))
147                 return error("Couldn't get %s for %s\n", url, ref);
148
149         hex[40] = '\0';
150         get_sha1_hex(hex, sha1);
151         return 0;
152 }
153
154 int main(int argc, char **argv)
155 {
156         char *commit_id;
157         char *url;
158         int arg = 1;
159
160         while (arg < argc && argv[arg][0] == '-') {
161                 if (argv[arg][1] == 't') {
162                         get_tree = 1;
163                 } else if (argv[arg][1] == 'c') {
164                         get_history = 1;
165                 } else if (argv[arg][1] == 'a') {
166                         get_all = 1;
167                         get_tree = 1;
168                         get_history = 1;
169                 } else if (argv[arg][1] == 'v') {
170                         get_verbosely = 1;
171                 } else if (argv[arg][1] == 'w') {
172                         write_ref = argv[arg + 1];
173                         arg++;
174                 }
175                 arg++;
176         }
177         if (argc < arg + 2) {
178                 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
179                 return 1;
180         }
181         commit_id = argv[arg];
182         url = argv[arg + 1];
183
184         curl_global_init(CURL_GLOBAL_ALL);
185
186         curl = curl_easy_init();
187
188         curl_ssl_verify = gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
189         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
190         curl_easy_setopt(curl, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
191
192         base = url;
193
194         if (pull(commit_id))
195                 return 1;
196
197         curl_global_cleanup();
198         return 0;
199 }