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