Make rev-list flush the stdio buffers after each rev.
[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 static CURL *curl;
10
11 static char *base;
12
13 static SHA_CTX c;
14 static z_stream stream;
15
16 static int local;
17 static int zret;
18
19 struct buffer
20 {
21         size_t posn;
22         size_t size;
23         void *buffer;
24 };
25
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;
34 }
35
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);
47
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;
57 }
58
59 int fetch(unsigned char *sha1)
60 {
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;
66
67         local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
68
69         if (local < 0)
70                 return error("Couldn't open %s\n", filename);
71
72         memset(&stream, 0, sizeof(stream));
73
74         inflateInit(&stream);
75
76         SHA1_Init(&c);
77
78         curl_easy_setopt(curl, CURLOPT_FILE, NULL);
79         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
80
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);
90
91         curl_easy_setopt(curl, CURLOPT_URL, url);
92
93         if (curl_easy_perform(curl))
94                 return error("Couldn't get %s for %s\n", url, hex);
95
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);
102         }
103         if (memcmp(sha1, real_sha1, 20)) {
104                 unlink(filename);
105                 return error("File %s has bad hash\n", hex);
106         }
107         
108         pull_say("got %s\n", hex);
109         return 0;
110 }
111
112 int fetch_ref(char *ref, unsigned char *sha1)
113 {
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';
121         
122         curl_easy_setopt(curl, CURLOPT_FILE, &buffer);
123         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
124
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);
131
132         curl_easy_setopt(curl, CURLOPT_URL, url);
133
134         if (curl_easy_perform(curl))
135                 return error("Couldn't get %s for %s\n", url, ref);
136
137         hex[40] = '\0';
138         get_sha1_hex(hex, sha1);
139         return 0;
140 }
141
142 int main(int argc, char **argv)
143 {
144         char *commit_id;
145         char *url;
146         int arg = 1;
147
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++;
162                 }
163                 arg++;
164         }
165         if (argc < arg + 2) {
166                 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
167                 return 1;
168         }
169         commit_id = argv[arg];
170         url = argv[arg + 1];
171
172         curl_global_init(CURL_GLOBAL_ALL);
173
174         curl = curl_easy_init();
175
176         base = url;
177
178         if (pull(commit_id))
179                 return 1;
180
181         curl_global_cleanup();
182         return 0;
183 }