14 static z_stream stream
;
19 static int curl_ssl_verify
;
28 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
29 struct buffer
*buffer
) {
30 size_t size
= eltsize
* nmemb
;
31 if (size
> buffer
->size
- buffer
->posn
)
32 size
= buffer
->size
- buffer
->posn
;
33 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
38 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
40 unsigned char expn
[4096];
41 size_t size
= eltsize
* nmemb
;
44 ssize_t retval
= write(local
, ptr
+ posn
, size
- posn
);
48 } while (posn
< size
);
50 stream
.avail_in
= size
;
53 stream
.next_out
= expn
;
54 stream
.avail_out
= sizeof(expn
);
55 zret
= inflate(&stream
, Z_SYNC_FLUSH
);
56 SHA1_Update(&c
, expn
, sizeof(expn
) - stream
.avail_out
);
57 } while (stream
.avail_in
&& zret
== Z_OK
);
61 int fetch(unsigned char *sha1
)
63 char *hex
= sha1_to_hex(sha1
);
64 char *filename
= sha1_file_name(sha1
);
65 unsigned char real_sha1
[20];
69 local
= open(filename
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
72 return error("Couldn't open %s\n", filename
);
74 memset(&stream
, 0, sizeof(stream
));
80 curl_easy_setopt(curl
, CURLOPT_FILE
, NULL
);
81 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
83 url
= xmalloc(strlen(base
) + 50);
85 posn
= url
+ strlen(base
);
86 strcpy(posn
, "objects/");
91 strcpy(posn
, hex
+ 2);
93 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
95 if (curl_easy_perform(curl
))
96 return error("Couldn't get %s for %s\n", url
, hex
);
100 SHA1_Final(real_sha1
, &c
);
101 if (zret
!= Z_STREAM_END
) {
103 return error("File %s (%s) corrupt\n", hex
, url
);
105 if (memcmp(sha1
, real_sha1
, 20)) {
107 return error("File %s has bad hash\n", hex
);
110 pull_say("got %s\n", hex
);
114 int fetch_ref(char *ref
, unsigned char *sha1
)
118 struct buffer buffer
;
124 curl_easy_setopt(curl
, CURLOPT_FILE
, &buffer
);
125 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
127 url
= xmalloc(strlen(base
) + 6 + strlen(ref
));
129 posn
= url
+ strlen(base
);
130 strcpy(posn
, "refs/");
134 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
136 if (curl_easy_perform(curl
))
137 return error("Couldn't get %s for %s\n", url
, ref
);
140 get_sha1_hex(hex
, sha1
);
144 int main(int argc
, char **argv
)
150 while (arg
< argc
&& argv
[arg
][0] == '-') {
151 if (argv
[arg
][1] == 't') {
153 } else if (argv
[arg
][1] == 'c') {
155 } else if (argv
[arg
][1] == 'a') {
159 } else if (argv
[arg
][1] == 'v') {
161 } else if (argv
[arg
][1] == 'w') {
162 write_ref
= argv
[arg
+ 1];
167 if (argc
< arg
+ 2) {
168 usage("git-http-pull [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
171 commit_id
= argv
[arg
];
174 curl_global_init(CURL_GLOBAL_ALL
);
176 curl
= curl_easy_init();
178 curl_ssl_verify
= gitenv("GIT_SSL_NO_VERIFY") ? 0 : 1;
179 curl_easy_setopt(curl
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
180 curl_easy_setopt(curl
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
187 curl_global_cleanup();