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