[PATCH] Add function to parse an object of unspecified type (take 2)
[git/mingw.git] / http-pull.c
blob192dcc370dee47c52c72915394bb6f2a79f64e12
1 #include <fcntl.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <stdlib.h>
5 #include "cache.h"
6 #include "commit.h"
7 #include <errno.h>
8 #include <stdio.h>
10 #include <curl/curl.h>
11 #include <curl/easy.h>
13 static CURL *curl;
15 static char *base;
17 static int tree = 0;
18 static int commits = 0;
19 static int all = 0;
21 static SHA_CTX c;
22 static z_stream stream;
24 static int local;
25 static int zret;
27 static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
28 void *data) {
29 char expn[4096];
30 size_t size = eltsize * nmemb;
31 int posn = 0;
32 do {
33 ssize_t retval = write(local, ptr + posn, size - posn);
34 if (retval < 0)
35 return posn;
36 posn += retval;
37 } while (posn < size);
39 stream.avail_in = size;
40 stream.next_in = ptr;
41 do {
42 stream.next_out = expn;
43 stream.avail_out = sizeof(expn);
44 zret = inflate(&stream, Z_SYNC_FLUSH);
45 SHA1_Update(&c, expn, sizeof(expn) - stream.avail_out);
46 } while (stream.avail_in && zret == Z_OK);
47 return size;
50 static int fetch(unsigned char *sha1)
52 char *hex = sha1_to_hex(sha1);
53 char *filename = sha1_file_name(sha1);
54 char real_sha1[20];
55 char *url;
56 char *posn;
58 if (has_sha1_file(sha1)) {
59 return 0;
62 local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
64 if (local < 0)
65 return error("Couldn't open %s\n", filename);
67 memset(&stream, 0, sizeof(stream));
69 inflateInit(&stream);
71 SHA1_Init(&c);
73 curl_easy_setopt(curl, CURLOPT_FILE, NULL);
74 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
76 url = xmalloc(strlen(base) + 50);
77 strcpy(url, base);
78 posn = url + strlen(base);
79 strcpy(posn, "objects/");
80 posn += 8;
81 memcpy(posn, hex, 2);
82 posn += 2;
83 *(posn++) = '/';
84 strcpy(posn, hex + 2);
86 curl_easy_setopt(curl, CURLOPT_URL, url);
88 /*printf("Getting %s\n", hex);*/
90 if (curl_easy_perform(curl))
91 return error("Couldn't get %s for %s\n", url, hex);
93 close(local);
94 inflateEnd(&stream);
95 SHA1_Final(real_sha1, &c);
96 if (zret != Z_STREAM_END) {
97 unlink(filename);
98 return error("File %s (%s) corrupt\n", hex, url);
100 if (memcmp(sha1, real_sha1, 20)) {
101 unlink(filename);
102 return error("File %s has bad hash\n", hex);
105 return 0;
108 static int process_tree(unsigned char *sha1)
110 struct tree *tree = lookup_tree(sha1);
111 struct tree_entry_list *entries;
113 if (parse_tree(tree))
114 return -1;
116 for (entries = tree->entries; entries; entries = entries->next) {
117 if (fetch(entries->item.tree->object.sha1))
118 return -1;
119 if (entries->directory) {
120 if (process_tree(entries->item.tree->object.sha1))
121 return -1;
124 return 0;
127 static int process_commit(unsigned char *sha1)
129 struct commit *obj = lookup_commit(sha1);
131 if (fetch(sha1))
132 return -1;
134 if (parse_commit(obj))
135 return -1;
137 if (tree) {
138 if (fetch(obj->tree->object.sha1))
139 return -1;
140 if (process_tree(obj->tree->object.sha1))
141 return -1;
142 if (!all)
143 tree = 0;
145 if (commits) {
146 struct commit_list *parents = obj->parents;
147 for (; parents; parents = parents->next) {
148 if (has_sha1_file(parents->item->object.sha1))
149 continue;
150 if (fetch(parents->item->object.sha1)) {
151 /* The server might not have it, and
152 * we don't mind.
154 continue;
156 if (process_commit(parents->item->object.sha1))
157 return -1;
160 return 0;
163 int main(int argc, char **argv)
165 char *commit_id;
166 char *url;
167 int arg = 1;
168 unsigned char sha1[20];
170 while (arg < argc && argv[arg][0] == '-') {
171 if (argv[arg][1] == 't') {
172 tree = 1;
173 } else if (argv[arg][1] == 'c') {
174 commits = 1;
175 } else if (argv[arg][1] == 'a') {
176 all = 1;
177 tree = 1;
178 commits = 1;
180 arg++;
182 if (argc < arg + 2) {
183 usage("http-pull [-c] [-t] [-a] commit-id url");
184 return 1;
186 commit_id = argv[arg];
187 url = argv[arg + 1];
189 get_sha1_hex(commit_id, sha1);
191 curl_global_init(CURL_GLOBAL_ALL);
193 curl = curl_easy_init();
195 base = url;
197 if (fetch(sha1))
198 return 1;
199 if (process_commit(sha1))
200 return 1;
202 curl_global_cleanup();
203 return 0;