Tutorial typofix.
[git/kirr.git] / http-pull.c
blobb2cecaea396625b41f12604944d9364383650f0a
1 #include "cache.h"
2 #include "commit.h"
4 #include "pull.h"
6 #include <curl/curl.h>
7 #include <curl/easy.h>
9 static CURL *curl;
11 static char *base;
13 static SHA_CTX c;
14 static z_stream stream;
16 static int local;
17 static int zret;
19 static int curl_ssl_verify;
21 struct buffer
23 size_t posn;
24 size_t size;
25 void *buffer;
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;
38 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
39 void *data) {
40 unsigned char expn[4096];
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);
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;
61 int fetch(unsigned char *sha1)
63 char *hex = sha1_to_hex(sha1);
64 char *filename = sha1_file_name(sha1);
65 unsigned char real_sha1[20];
66 char *url;
67 char *posn;
69 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
71 if (local < 0)
72 return error("Couldn't open %s\n", filename);
74 memset(&stream, 0, sizeof(stream));
76 inflateInit(&stream);
78 SHA1_Init(&c);
80 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
81 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
83 url = xmalloc(strlen(base) + 50);
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);
93 curl_easy_setopt(curl, CURLOPT_URL, url);
95 if (curl_easy_perform(curl))
96 return error("Couldn't get %s for %s\n", url, hex);
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);
105 if (memcmp(sha1, real_sha1, 20)) {
106 unlink(filename);
107 return error("File %s has bad hash\n", hex);
110 pull_say("got %s\n", hex);
111 return 0;
114 int fetch_ref(char *ref, unsigned char *sha1)
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';
124 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
125 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
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);
134 curl_easy_setopt(curl, CURLOPT_URL, url);
136 if (curl_easy_perform(curl))
137 return error("Couldn't get %s for %s\n", url, ref);
139 hex[40] = '\0';
140 get_sha1_hex(hex, sha1);
141 return 0;
144 int main(int argc, char **argv)
146 char *commit_id;
147 char *url;
148 int arg = 1;
150 while (arg < argc && argv[arg][0] == '-') {
151 if (argv[arg][1] == 't') {
152 get_tree = 1;
153 } else if (argv[arg][1] == 'c') {
154 get_history = 1;
155 } else if (argv[arg][1] == 'a') {
156 get_all = 1;
157 get_tree = 1;
158 get_history = 1;
159 } else if (argv[arg][1] == 'v') {
160 get_verbosely = 1;
161 } else if (argv[arg][1] == 'w') {
162 write_ref = argv[arg + 1];
163 arg++;
165 arg++;
167 if (argc < arg + 2) {
168 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
169 return 1;
171 commit_id = argv[arg];
172 url = argv[arg + 1];
174 curl_global_init(CURL_GLOBAL_ALL);
176 curl = curl_easy_init();
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);
182 base = url;
184 if (pull(commit_id))
185 return 1;
187 curl_global_cleanup();
188 return 0;