[PATCH] Cleanup: git-verify-tag-script
[git/repo.git] / http-pull.c
blob1f9d60b9b1d5eed85b24d96c240666bbfc5a22ed
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 struct buffer
21 size_t posn;
22 size_t size;
23 void *buffer;
26 static size_t fwrite_buffer(void *ptr, size_t eltsize, size_t nmemb,
27 struct buffer *buffer) {
28 size_t size = eltsize * nmemb;
29 if (size > buffer->size - buffer->posn)
30 size = buffer->size - buffer->posn;
31 memcpy(buffer->buffer + buffer->posn, ptr, size);
32 buffer->posn += size;
33 return size;
36 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
37 void *data) {
38 unsigned char expn[4096];
39 size_t size = eltsize * nmemb;
40 int posn = 0;
41 do {
42 ssize_t retval = write(local, ptr + posn, size - posn);
43 if (retval < 0)
44 return posn;
45 posn += retval;
46 } while (posn < size);
48 stream.avail_in = size;
49 stream.next_in = ptr;
50 do {
51 stream.next_out = expn;
52 stream.avail_out = sizeof(expn);
53 zret = inflate(&stream, Z_SYNC_FLUSH);
54 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
55 } while (stream.avail_in && zret == Z_OK);
56 return size;
59 int fetch(unsigned char *sha1)
61 char *hex = sha1_to_hex(sha1);
62 char *filename = sha1_file_name(sha1);
63 unsigned char real_sha1[20];
64 char *url;
65 char *posn;
67 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
69 if (local < 0)
70 return error("Couldn't open %s\n", filename);
72 memset(&stream, 0, sizeof(stream));
74 inflateInit(&stream);
76 SHA1_Init(&c);
78 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
79 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
81 url = xmalloc(strlen(base) + 50);
82 strcpy(url, base);
83 posn = url + strlen(base);
84 strcpy(posn, "objects/");
85 posn += 8;
86 memcpy(posn, hex, 2);
87 posn += 2;
88 *(posn++) = '/';
89 strcpy(posn, hex + 2);
91 curl_easy_setopt(curl, CURLOPT_URL, url);
93 if (curl_easy_perform(curl))
94 return error("Couldn't get %s for %s\n", url, hex);
96 close(local);
97 inflateEnd(&stream);
98 SHA1_Final(real_sha1, &c);
99 if (zret != Z_STREAM_END) {
100 unlink(filename);
101 return error("File %s (%s) corrupt\n", hex, url);
103 if (memcmp(sha1, real_sha1, 20)) {
104 unlink(filename);
105 return error("File %s has bad hash\n", hex);
108 pull_say("got %s\n", hex);
109 return 0;
112 int fetch_ref(char *ref, unsigned char *sha1)
114 char *url, *posn;
115 char hex[42];
116 struct buffer buffer;
117 buffer.size = 41;
118 buffer.posn = 0;
119 buffer.buffer = hex;
120 hex[41] = '\0';
122 curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
123 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
125 url = xmalloc(strlen(base) + 6 + strlen(ref));
126 strcpy(url, base);
127 posn = url + strlen(base);
128 strcpy(posn, "refs/");
129 posn += 5;
130 strcpy(posn, ref);
132 curl_easy_setopt(curl, CURLOPT_URL, url);
134 if (curl_easy_perform(curl))
135 return error("Couldn't get %s for %s\n", url, ref);
137 hex[40] = '\0';
138 get_sha1_hex(hex, sha1);
139 return 0;
142 int main(int argc, char **argv)
144 char *commit_id;
145 char *url;
146 int arg = 1;
148 while (arg < argc && argv[arg][0] == '-') {
149 if (argv[arg][1] == 't') {
150 get_tree = 1;
151 } else if (argv[arg][1] == 'c') {
152 get_history = 1;
153 } else if (argv[arg][1] == 'a') {
154 get_all = 1;
155 get_tree = 1;
156 get_history = 1;
157 } else if (argv[arg][1] == 'v') {
158 get_verbosely = 1;
159 } else if (argv[arg][1] == 'w') {
160 write_ref = argv[arg + 1];
161 arg++;
163 arg++;
165 if (argc < arg + 2) {
166 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
167 return 1;
169 commit_id = argv[arg];
170 url = argv[arg + 1];
172 curl_global_init(CURL_GLOBAL_ALL);
174 curl = curl_easy_init();
176 base = url;
178 if (pull(commit_id))
179 return 1;
181 curl_global_cleanup();
182 return 0;