9 #if LIBCURL_VERSION_NUM < 0x070704
10 #define curl_global_cleanup() do { /* nothing */ } while(0)
12 #if LIBCURL_VERSION_NUM < 0x070800
13 #define curl_global_init(a) do { /* nothing */ } while(0)
15 #if LIBCURL_VERSION_NUM < 0x070907
16 #define curl_easy_setopt(a, b, c) do { /* nothing */ } while(0)
24 static z_stream stream
;
29 static int curl_ssl_verify
;
38 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
39 struct buffer
*buffer
) {
40 size_t size
= eltsize
* nmemb
;
41 if (size
> buffer
->size
- buffer
->posn
)
42 size
= buffer
->size
- buffer
->posn
;
43 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
48 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
50 unsigned char expn
[4096];
51 size_t size
= eltsize
* nmemb
;
54 ssize_t retval
= write(local
, ptr
+ posn
, size
- posn
);
58 } while (posn
< size
);
60 stream
.avail_in
= size
;
63 stream
.next_out
= expn
;
64 stream
.avail_out
= sizeof(expn
);
65 zret
= inflate(&stream
, Z_SYNC_FLUSH
);
66 SHA1_Update(&c
, expn
, sizeof(expn
) - stream
.avail_out
);
67 } while (stream
.avail_in
&& zret
== Z_OK
);
71 int fetch(unsigned char *sha1
)
73 char *hex
= sha1_to_hex(sha1
);
74 char *filename
= sha1_file_name(sha1
);
75 unsigned char real_sha1
[20];
79 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
82 return error("Couldn't open %s\n", filename
);
84 memset(&stream
, 0, sizeof(stream
));
90 curl_easy_setopt(curl
, CURLOPT_FILE
, NULL
);
91 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
93 url
= xmalloc(strlen(base
) + 50);
95 posn
= url
+ strlen(base
);
96 strcpy(posn
, "objects/");
101 strcpy(posn
, hex
+ 2);
103 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
105 if (curl_easy_perform(curl
))
106 return error("Couldn't get %s for %s\n", url
, hex
);
110 SHA1_Final(real_sha1
, &c
);
111 if (zret
!= Z_STREAM_END
) {
113 return error("File %s (%s) corrupt\n", hex
, url
);
115 if (memcmp(sha1
, real_sha1
, 20)) {
117 return error("File %s has bad hash\n", hex
);
120 pull_say("got %s\n", hex
);
124 int fetch_ref(char *ref
, unsigned char *sha1
)
128 struct buffer buffer
;
134 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
135 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
137 url
= xmalloc(strlen(base
) + 6 + strlen(ref
));
139 posn
= url
+ strlen(base
);
140 strcpy(posn
, "refs/");
144 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
146 if (curl_easy_perform(curl
))
147 return error("Couldn't get %s for %s\n", url
, ref
);
150 get_sha1_hex(hex
, sha1
);
154 int main(int argc
, char **argv
)
160 while (arg
< argc
&& argv
[arg
][0] == '-') {
161 if (argv
[arg
][1] == 't') {
163 } else if (argv
[arg
][1] == 'c') {
165 } else if (argv
[arg
][1] == 'a') {
169 } else if (argv
[arg
][1] == 'v') {
171 } else if (argv
[arg
][1] == 'w') {
172 write_ref
= argv
[arg
+ 1];
177 if (argc
< arg
+ 2) {
178 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
181 commit_id
= argv
[arg
];
184 curl_global_init(CURL_GLOBAL_ALL
);
186 curl
= curl_easy_init();
188 curl_ssl_verify
= gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
189 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
190 curl_easy_setopt(curl
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
197 curl_global_cleanup();