]>
Commit | Line | Data |
---|---|---|
9b288516 DB |
1 | #include "cache.h" |
2 | #include "transport.h" | |
3 | #include "run-command.h" | |
c29727d5 DB |
4 | #include "pkt-line.h" |
5 | #include "fetch-pack.h" | |
40cb4fab | 6 | #include "send-pack.h" |
c29727d5 | 7 | #include "walker.h" |
c7a8a162 | 8 | #include "bundle.h" |
cd547b48 JS |
9 | #include "dir.h" |
10 | #include "refs.h" | |
11 | ||
12 | /* rsync support */ | |
13 | ||
14 | /* | |
15 | * We copy packed-refs and refs/ into a temporary file, then read the | |
16 | * loose refs recursively (sorting whenever possible), and then inserting | |
17 | * those packed refs that are not yet in the list (not validating, but | |
18 | * assuming that the file is sorted). | |
19 | * | |
20 | * Appears refactoring this from refs.c is too cumbersome. | |
21 | */ | |
22 | ||
23 | static int str_cmp(const void *a, const void *b) | |
24 | { | |
25 | const char *s1 = a; | |
26 | const char *s2 = b; | |
27 | ||
28 | return strcmp(s1, s2); | |
29 | } | |
30 | ||
31 | /* path->buf + name_offset is expected to point to "refs/" */ | |
32 | ||
33 | static int read_loose_refs(struct strbuf *path, int name_offset, | |
34 | struct ref **tail) | |
35 | { | |
36 | DIR *dir = opendir(path->buf); | |
37 | struct dirent *de; | |
38 | struct { | |
39 | char **entries; | |
40 | int nr, alloc; | |
41 | } list; | |
42 | int i, pathlen; | |
43 | ||
44 | if (!dir) | |
45 | return -1; | |
46 | ||
47 | memset (&list, 0, sizeof(list)); | |
48 | ||
49 | while ((de = readdir(dir))) { | |
8ca12c0d | 50 | if (is_dot_or_dotdot(de->d_name)) |
cd547b48 JS |
51 | continue; |
52 | ALLOC_GROW(list.entries, list.nr + 1, list.alloc); | |
53 | list.entries[list.nr++] = xstrdup(de->d_name); | |
54 | } | |
55 | closedir(dir); | |
56 | ||
57 | /* sort the list */ | |
58 | ||
59 | qsort(list.entries, list.nr, sizeof(char *), str_cmp); | |
60 | ||
61 | pathlen = path->len; | |
62 | strbuf_addch(path, '/'); | |
63 | ||
64 | for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) { | |
65 | strbuf_addstr(path, list.entries[i]); | |
66 | if (read_loose_refs(path, name_offset, tail)) { | |
67 | int fd = open(path->buf, O_RDONLY); | |
68 | char buffer[40]; | |
69 | struct ref *next; | |
70 | ||
71 | if (fd < 0) | |
72 | continue; | |
59c69c0c | 73 | next = alloc_ref(path->buf + name_offset); |
cd547b48 JS |
74 | if (read_in_full(fd, buffer, 40) != 40 || |
75 | get_sha1_hex(buffer, next->old_sha1)) { | |
76 | close(fd); | |
77 | free(next); | |
78 | continue; | |
79 | } | |
80 | close(fd); | |
cd547b48 JS |
81 | (*tail)->next = next; |
82 | *tail = next; | |
83 | } | |
84 | } | |
85 | strbuf_setlen(path, pathlen); | |
86 | ||
87 | for (i = 0; i < list.nr; i++) | |
88 | free(list.entries[i]); | |
89 | free(list.entries); | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | /* insert the packed refs for which no loose refs were found */ | |
95 | ||
96 | static void insert_packed_refs(const char *packed_refs, struct ref **list) | |
97 | { | |
98 | FILE *f = fopen(packed_refs, "r"); | |
99 | static char buffer[PATH_MAX]; | |
100 | ||
101 | if (!f) | |
102 | return; | |
103 | ||
104 | for (;;) { | |
47ec7943 | 105 | int cmp = cmp, len; |
cd547b48 JS |
106 | |
107 | if (!fgets(buffer, sizeof(buffer), f)) { | |
108 | fclose(f); | |
109 | return; | |
110 | } | |
111 | ||
112 | if (hexval(buffer[0]) > 0xf) | |
113 | continue; | |
114 | len = strlen(buffer); | |
872c930d | 115 | if (len && buffer[len - 1] == '\n') |
cd547b48 JS |
116 | buffer[--len] = '\0'; |
117 | if (len < 41) | |
118 | continue; | |
119 | while ((*list)->next && | |
120 | (cmp = strcmp(buffer + 41, | |
121 | (*list)->next->name)) > 0) | |
122 | list = &(*list)->next; | |
123 | if (!(*list)->next || cmp < 0) { | |
59c69c0c | 124 | struct ref *next = alloc_ref(buffer + 41); |
cd547b48 JS |
125 | buffer[40] = '\0'; |
126 | if (get_sha1_hex(buffer, next->old_sha1)) { | |
127 | warning ("invalid SHA-1: %s", buffer); | |
128 | free(next); | |
129 | continue; | |
130 | } | |
cd547b48 JS |
131 | next->next = (*list)->next; |
132 | (*list)->next = next; | |
133 | list = &(*list)->next; | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
7efaeba2 JS |
138 | static const char *rsync_url(const char *url) |
139 | { | |
140 | return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url; | |
141 | } | |
142 | ||
64fcef2d | 143 | static struct ref *get_refs_via_rsync(struct transport *transport, int for_push) |
cd547b48 JS |
144 | { |
145 | struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; | |
146 | struct ref dummy, *tail = &dummy; | |
147 | struct child_process rsync; | |
148 | const char *args[5]; | |
149 | int temp_dir_len; | |
150 | ||
64fcef2d DB |
151 | if (for_push) |
152 | return NULL; | |
153 | ||
cd547b48 JS |
154 | /* copy the refs to the temporary directory */ |
155 | ||
156 | strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX")); | |
157 | if (!mkdtemp(temp_dir.buf)) | |
0721c314 | 158 | die_errno ("Could not make temporary directory"); |
cd547b48 JS |
159 | temp_dir_len = temp_dir.len; |
160 | ||
7efaeba2 | 161 | strbuf_addstr(&buf, rsync_url(transport->url)); |
cd547b48 JS |
162 | strbuf_addstr(&buf, "/refs"); |
163 | ||
164 | memset(&rsync, 0, sizeof(rsync)); | |
165 | rsync.argv = args; | |
166 | rsync.stdout_to_stderr = 1; | |
167 | args[0] = "rsync"; | |
2b5a06ed | 168 | args[1] = (transport->verbose > 0) ? "-rv" : "-r"; |
cd547b48 JS |
169 | args[2] = buf.buf; |
170 | args[3] = temp_dir.buf; | |
171 | args[4] = NULL; | |
172 | ||
173 | if (run_command(&rsync)) | |
174 | die ("Could not run rsync to get refs"); | |
175 | ||
176 | strbuf_reset(&buf); | |
7efaeba2 | 177 | strbuf_addstr(&buf, rsync_url(transport->url)); |
cd547b48 JS |
178 | strbuf_addstr(&buf, "/packed-refs"); |
179 | ||
180 | args[2] = buf.buf; | |
181 | ||
182 | if (run_command(&rsync)) | |
183 | die ("Could not run rsync to get refs"); | |
184 | ||
185 | /* read the copied refs */ | |
186 | ||
187 | strbuf_addstr(&temp_dir, "/refs"); | |
188 | read_loose_refs(&temp_dir, temp_dir_len + 1, &tail); | |
189 | strbuf_setlen(&temp_dir, temp_dir_len); | |
190 | ||
191 | tail = &dummy; | |
192 | strbuf_addstr(&temp_dir, "/packed-refs"); | |
193 | insert_packed_refs(temp_dir.buf, &tail); | |
194 | strbuf_setlen(&temp_dir, temp_dir_len); | |
195 | ||
196 | if (remove_dir_recursively(&temp_dir, 0)) | |
197 | warning ("Error removing temporary directory %s.", | |
198 | temp_dir.buf); | |
199 | ||
200 | strbuf_release(&buf); | |
201 | strbuf_release(&temp_dir); | |
202 | ||
203 | return dummy.next; | |
204 | } | |
205 | ||
206 | static int fetch_objs_via_rsync(struct transport *transport, | |
37148311 | 207 | int nr_objs, struct ref **to_fetch) |
cd547b48 JS |
208 | { |
209 | struct strbuf buf = STRBUF_INIT; | |
210 | struct child_process rsync; | |
211 | const char *args[8]; | |
212 | int result; | |
213 | ||
7efaeba2 | 214 | strbuf_addstr(&buf, rsync_url(transport->url)); |
cd547b48 JS |
215 | strbuf_addstr(&buf, "/objects/"); |
216 | ||
217 | memset(&rsync, 0, sizeof(rsync)); | |
218 | rsync.argv = args; | |
219 | rsync.stdout_to_stderr = 1; | |
220 | args[0] = "rsync"; | |
2b5a06ed | 221 | args[1] = (transport->verbose > 0) ? "-rv" : "-r"; |
cd547b48 JS |
222 | args[2] = "--ignore-existing"; |
223 | args[3] = "--exclude"; | |
224 | args[4] = "info"; | |
225 | args[5] = buf.buf; | |
226 | args[6] = get_object_directory(); | |
227 | args[7] = NULL; | |
228 | ||
229 | /* NEEDSWORK: handle one level of alternates */ | |
230 | result = run_command(&rsync); | |
231 | ||
232 | strbuf_release(&buf); | |
233 | ||
234 | return result; | |
235 | } | |
236 | ||
237 | static int write_one_ref(const char *name, const unsigned char *sha1, | |
238 | int flags, void *data) | |
239 | { | |
240 | struct strbuf *buf = data; | |
241 | int len = buf->len; | |
242 | FILE *f; | |
243 | ||
244 | /* when called via for_each_ref(), flags is non-zero */ | |
245 | if (flags && prefixcmp(name, "refs/heads/") && | |
246 | prefixcmp(name, "refs/tags/")) | |
247 | return 0; | |
248 | ||
249 | strbuf_addstr(buf, name); | |
250 | if (safe_create_leading_directories(buf->buf) || | |
251 | !(f = fopen(buf->buf, "w")) || | |
252 | fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 || | |
253 | fclose(f)) | |
254 | return error("problems writing temporary file %s", buf->buf); | |
255 | strbuf_setlen(buf, len); | |
256 | return 0; | |
257 | } | |
258 | ||
259 | static int write_refs_to_temp_dir(struct strbuf *temp_dir, | |
260 | int refspec_nr, const char **refspec) | |
261 | { | |
262 | int i; | |
263 | ||
264 | for (i = 0; i < refspec_nr; i++) { | |
265 | unsigned char sha1[20]; | |
266 | char *ref; | |
267 | ||
268 | if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1) | |
269 | return error("Could not get ref %s", refspec[i]); | |
270 | ||
271 | if (write_one_ref(ref, sha1, 0, temp_dir)) { | |
272 | free(ref); | |
273 | return -1; | |
274 | } | |
275 | free(ref); | |
276 | } | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static int rsync_transport_push(struct transport *transport, | |
281 | int refspec_nr, const char **refspec, int flags) | |
282 | { | |
283 | struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; | |
284 | int result = 0, i; | |
285 | struct child_process rsync; | |
ee020f35 | 286 | const char *args[10]; |
cd547b48 | 287 | |
94c89ba6 AW |
288 | if (flags & TRANSPORT_PUSH_MIRROR) |
289 | return error("rsync transport does not support mirror mode"); | |
290 | ||
cd547b48 JS |
291 | /* first push the objects */ |
292 | ||
7efaeba2 | 293 | strbuf_addstr(&buf, rsync_url(transport->url)); |
cd547b48 JS |
294 | strbuf_addch(&buf, '/'); |
295 | ||
296 | memset(&rsync, 0, sizeof(rsync)); | |
297 | rsync.argv = args; | |
298 | rsync.stdout_to_stderr = 1; | |
ee020f35 SP |
299 | i = 0; |
300 | args[i++] = "rsync"; | |
301 | args[i++] = "-a"; | |
302 | if (flags & TRANSPORT_PUSH_DRY_RUN) | |
303 | args[i++] = "--dry-run"; | |
304 | if (transport->verbose > 0) | |
305 | args[i++] = "-v"; | |
306 | args[i++] = "--ignore-existing"; | |
307 | args[i++] = "--exclude"; | |
308 | args[i++] = "info"; | |
309 | args[i++] = get_object_directory(); | |
310 | args[i++] = buf.buf; | |
311 | args[i++] = NULL; | |
cd547b48 JS |
312 | |
313 | if (run_command(&rsync)) | |
7efaeba2 JS |
314 | return error("Could not push objects to %s", |
315 | rsync_url(transport->url)); | |
cd547b48 JS |
316 | |
317 | /* copy the refs to the temporary directory; they could be packed. */ | |
318 | ||
319 | strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX")); | |
320 | if (!mkdtemp(temp_dir.buf)) | |
0721c314 | 321 | die_errno ("Could not make temporary directory"); |
cd547b48 JS |
322 | strbuf_addch(&temp_dir, '/'); |
323 | ||
324 | if (flags & TRANSPORT_PUSH_ALL) { | |
325 | if (for_each_ref(write_one_ref, &temp_dir)) | |
326 | return -1; | |
327 | } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec)) | |
328 | return -1; | |
329 | ||
ee020f35 SP |
330 | i = 2; |
331 | if (flags & TRANSPORT_PUSH_DRY_RUN) | |
332 | args[i++] = "--dry-run"; | |
333 | if (!(flags & TRANSPORT_PUSH_FORCE)) | |
334 | args[i++] = "--ignore-existing"; | |
cd547b48 | 335 | args[i++] = temp_dir.buf; |
7efaeba2 | 336 | args[i++] = rsync_url(transport->url); |
cd547b48 JS |
337 | args[i++] = NULL; |
338 | if (run_command(&rsync)) | |
7efaeba2 JS |
339 | result = error("Could not push to %s", |
340 | rsync_url(transport->url)); | |
cd547b48 JS |
341 | |
342 | if (remove_dir_recursively(&temp_dir, 0)) | |
343 | warning ("Could not remove temporary directory %s.", | |
344 | temp_dir.buf); | |
345 | ||
346 | strbuf_release(&buf); | |
347 | strbuf_release(&temp_dir); | |
348 | ||
349 | return result; | |
350 | } | |
c29727d5 | 351 | |
c7a8a162 JS |
352 | struct bundle_transport_data { |
353 | int fd; | |
354 | struct bundle_header header; | |
355 | }; | |
356 | ||
64fcef2d | 357 | static struct ref *get_refs_from_bundle(struct transport *transport, int for_push) |
c7a8a162 JS |
358 | { |
359 | struct bundle_transport_data *data = transport->data; | |
360 | struct ref *result = NULL; | |
361 | int i; | |
362 | ||
64fcef2d DB |
363 | if (for_push) |
364 | return NULL; | |
365 | ||
c7a8a162 JS |
366 | if (data->fd > 0) |
367 | close(data->fd); | |
368 | data->fd = read_bundle_header(transport->url, &data->header); | |
369 | if (data->fd < 0) | |
370 | die ("Could not read bundle '%s'.", transport->url); | |
371 | for (i = 0; i < data->header.references.nr; i++) { | |
372 | struct ref_list_entry *e = data->header.references.list + i; | |
59c69c0c | 373 | struct ref *ref = alloc_ref(e->name); |
c7a8a162 | 374 | hashcpy(ref->old_sha1, e->sha1); |
c7a8a162 JS |
375 | ref->next = result; |
376 | result = ref; | |
377 | } | |
378 | return result; | |
379 | } | |
380 | ||
1788c39c | 381 | static int fetch_refs_from_bundle(struct transport *transport, |
37148311 | 382 | int nr_heads, struct ref **to_fetch) |
c7a8a162 JS |
383 | { |
384 | struct bundle_transport_data *data = transport->data; | |
385 | return unbundle(&data->header, data->fd); | |
386 | } | |
387 | ||
388 | static int close_bundle(struct transport *transport) | |
389 | { | |
390 | struct bundle_transport_data *data = transport->data; | |
391 | if (data->fd > 0) | |
392 | close(data->fd); | |
f4e95765 | 393 | free(data); |
c7a8a162 JS |
394 | return 0; |
395 | } | |
396 | ||
9b288516 | 397 | struct git_transport_data { |
aa5af974 | 398 | struct git_transport_options options; |
ba227857 DB |
399 | struct child_process *conn; |
400 | int fd[2]; | |
61b075bd | 401 | unsigned got_remote_heads : 1; |
64fcef2d | 402 | struct extra_have_objects extra_have; |
9b288516 DB |
403 | }; |
404 | ||
aa5af974 | 405 | static int set_git_option(struct git_transport_options *opts, |
9b288516 DB |
406 | const char *name, const char *value) |
407 | { | |
c29727d5 | 408 | if (!strcmp(name, TRANS_OPT_UPLOADPACK)) { |
aa5af974 | 409 | opts->uploadpack = value; |
c29727d5 DB |
410 | return 0; |
411 | } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) { | |
aa5af974 | 412 | opts->receivepack = value; |
9b288516 DB |
413 | return 0; |
414 | } else if (!strcmp(name, TRANS_OPT_THIN)) { | |
aa5af974 | 415 | opts->thin = !!value; |
9b288516 | 416 | return 0; |
41fa7d2e | 417 | } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) { |
aa5af974 | 418 | opts->followtags = !!value; |
41fa7d2e | 419 | return 0; |
c29727d5 | 420 | } else if (!strcmp(name, TRANS_OPT_KEEP)) { |
aa5af974 | 421 | opts->keep = !!value; |
c29727d5 | 422 | return 0; |
c29727d5 DB |
423 | } else if (!strcmp(name, TRANS_OPT_DEPTH)) { |
424 | if (!value) | |
aa5af974 | 425 | opts->depth = 0; |
c29727d5 | 426 | else |
aa5af974 | 427 | opts->depth = atoi(value); |
c29727d5 | 428 | return 0; |
9b288516 DB |
429 | } |
430 | return 1; | |
431 | } | |
432 | ||
64fcef2d | 433 | static int connect_setup(struct transport *transport, int for_push, int verbose) |
ba227857 DB |
434 | { |
435 | struct git_transport_data *data = transport->data; | |
61b075bd IL |
436 | |
437 | if (data->conn) | |
438 | return 0; | |
439 | ||
64fcef2d | 440 | data->conn = git_connect(data->fd, transport->url, |
aa5af974 IL |
441 | for_push ? data->options.receivepack : |
442 | data->options.uploadpack, | |
64fcef2d | 443 | verbose ? CONNECT_VERBOSE : 0); |
61b075bd | 444 | |
ba227857 DB |
445 | return 0; |
446 | } | |
447 | ||
64fcef2d | 448 | static struct ref *get_refs_via_connect(struct transport *transport, int for_push) |
c29727d5 DB |
449 | { |
450 | struct git_transport_data *data = transport->data; | |
451 | struct ref *refs; | |
c29727d5 | 452 | |
64fcef2d DB |
453 | connect_setup(transport, for_push, 0); |
454 | get_remote_heads(data->fd[0], &refs, 0, NULL, | |
455 | for_push ? REF_NORMAL : 0, &data->extra_have); | |
61b075bd | 456 | data->got_remote_heads = 1; |
c29727d5 DB |
457 | |
458 | return refs; | |
459 | } | |
460 | ||
1788c39c | 461 | static int fetch_refs_via_pack(struct transport *transport, |
37148311 | 462 | int nr_heads, struct ref **to_fetch) |
c29727d5 DB |
463 | { |
464 | struct git_transport_data *data = transport->data; | |
425b1393 | 465 | char **heads = xmalloc(nr_heads * sizeof(*heads)); |
e4cd6c7a | 466 | char **origh = xmalloc(nr_heads * sizeof(*origh)); |
ba227857 | 467 | const struct ref *refs; |
c29727d5 DB |
468 | char *dest = xstrdup(transport->url); |
469 | struct fetch_pack_args args; | |
425b1393 | 470 | int i; |
00183cbb | 471 | struct ref *refs_tmp = NULL; |
c29727d5 | 472 | |
fa740529 | 473 | memset(&args, 0, sizeof(args)); |
aa5af974 IL |
474 | args.uploadpack = data->options.uploadpack; |
475 | args.keep_pack = data->options.keep; | |
fa740529 | 476 | args.lock_pack = 1; |
aa5af974 IL |
477 | args.use_thin_pack = data->options.thin; |
478 | args.include_tag = data->options.followtags; | |
9b373229 | 479 | args.verbose = (transport->verbose > 0); |
fe8aa148 | 480 | args.quiet = (transport->verbose < 0); |
21188b1e | 481 | args.no_progress = args.quiet || (!transport->progress && !isatty(1)); |
aa5af974 | 482 | args.depth = data->options.depth; |
c29727d5 | 483 | |
425b1393 | 484 | for (i = 0; i < nr_heads; i++) |
e4cd6c7a | 485 | origh[i] = heads[i] = xstrdup(to_fetch[i]->name); |
ba227857 | 486 | |
61b075bd | 487 | if (!data->got_remote_heads) { |
64fcef2d | 488 | connect_setup(transport, 0, 0); |
40c155ff | 489 | get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0, NULL); |
61b075bd | 490 | data->got_remote_heads = 1; |
ba227857 DB |
491 | } |
492 | ||
00183cbb DB |
493 | refs = fetch_pack(&args, data->fd, data->conn, |
494 | refs_tmp ? refs_tmp : transport->remote_refs, | |
ba227857 DB |
495 | dest, nr_heads, heads, &transport->pack_lockfile); |
496 | close(data->fd[0]); | |
497 | close(data->fd[1]); | |
498 | if (finish_connect(data->conn)) | |
499 | refs = NULL; | |
500 | data->conn = NULL; | |
61b075bd | 501 | data->got_remote_heads = 0; |
c29727d5 | 502 | |
00183cbb DB |
503 | free_refs(refs_tmp); |
504 | ||
425b1393 | 505 | for (i = 0; i < nr_heads; i++) |
e4cd6c7a SP |
506 | free(origh[i]); |
507 | free(origh); | |
508 | free(heads); | |
c29727d5 | 509 | free(dest); |
c6bc4005 | 510 | return (refs ? 0 : -1); |
c29727d5 DB |
511 | } |
512 | ||
481c7a6d JK |
513 | static int push_had_errors(struct ref *ref) |
514 | { | |
515 | for (; ref; ref = ref->next) { | |
516 | switch (ref->status) { | |
517 | case REF_STATUS_NONE: | |
518 | case REF_STATUS_UPTODATE: | |
519 | case REF_STATUS_OK: | |
520 | break; | |
521 | default: | |
522 | return 1; | |
523 | } | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
64fcef2d DB |
528 | static int refs_pushed(struct ref *ref) |
529 | { | |
530 | for (; ref; ref = ref->next) { | |
531 | switch(ref->status) { | |
532 | case REF_STATUS_NONE: | |
533 | case REF_STATUS_UPTODATE: | |
534 | break; | |
535 | default: | |
536 | return 1; | |
537 | } | |
538 | } | |
539 | return 0; | |
540 | } | |
541 | ||
542 | static void update_tracking_ref(struct remote *remote, struct ref *ref, int verbose) | |
543 | { | |
544 | struct refspec rs; | |
545 | ||
546 | if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE) | |
547 | return; | |
548 | ||
549 | rs.src = ref->name; | |
550 | rs.dst = NULL; | |
551 | ||
552 | if (!remote_find_tracking(remote, &rs)) { | |
553 | if (verbose) | |
554 | fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); | |
555 | if (ref->deletion) { | |
556 | delete_ref(rs.dst, NULL, 0); | |
557 | } else | |
558 | update_ref("update by push", rs.dst, | |
559 | ref->new_sha1, NULL, 0, 0); | |
560 | free(rs.dst); | |
561 | } | |
562 | } | |
563 | ||
564 | #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3) | |
565 | ||
1965ff74 | 566 | static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg, int porcelain) |
64fcef2d | 567 | { |
1965ff74 LA |
568 | if (porcelain) { |
569 | if (from) | |
570 | fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); | |
571 | else | |
572 | fprintf(stdout, "%c\t:%s\t", flag, to->name); | |
573 | if (msg) | |
574 | fprintf(stdout, "%s (%s)\n", summary, msg); | |
575 | else | |
576 | fprintf(stdout, "%s\n", summary); | |
577 | } else { | |
578 | fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary); | |
579 | if (from) | |
580 | fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name)); | |
581 | else | |
582 | fputs(prettify_refname(to->name), stderr); | |
583 | if (msg) { | |
584 | fputs(" (", stderr); | |
585 | fputs(msg, stderr); | |
586 | fputc(')', stderr); | |
587 | } | |
588 | fputc('\n', stderr); | |
64fcef2d | 589 | } |
64fcef2d DB |
590 | } |
591 | ||
592 | static const char *status_abbrev(unsigned char sha1[20]) | |
593 | { | |
594 | return find_unique_abbrev(sha1, DEFAULT_ABBREV); | |
595 | } | |
596 | ||
1965ff74 | 597 | static void print_ok_ref_status(struct ref *ref, int porcelain) |
64fcef2d DB |
598 | { |
599 | if (ref->deletion) | |
1965ff74 | 600 | print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain); |
64fcef2d DB |
601 | else if (is_null_sha1(ref->old_sha1)) |
602 | print_ref_status('*', | |
603 | (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" : | |
1965ff74 LA |
604 | "[new branch]"), |
605 | ref, ref->peer_ref, NULL, porcelain); | |
64fcef2d DB |
606 | else { |
607 | char quickref[84]; | |
608 | char type; | |
609 | const char *msg; | |
610 | ||
611 | strcpy(quickref, status_abbrev(ref->old_sha1)); | |
612 | if (ref->nonfastforward) { | |
613 | strcat(quickref, "..."); | |
614 | type = '+'; | |
615 | msg = "forced update"; | |
616 | } else { | |
617 | strcat(quickref, ".."); | |
618 | type = ' '; | |
619 | msg = NULL; | |
620 | } | |
621 | strcat(quickref, status_abbrev(ref->new_sha1)); | |
622 | ||
1965ff74 | 623 | print_ref_status(type, quickref, ref, ref->peer_ref, msg, porcelain); |
64fcef2d DB |
624 | } |
625 | } | |
626 | ||
1965ff74 | 627 | static int print_one_push_status(struct ref *ref, const char *dest, int count, int porcelain) |
64fcef2d DB |
628 | { |
629 | if (!count) | |
630 | fprintf(stderr, "To %s\n", dest); | |
631 | ||
632 | switch(ref->status) { | |
633 | case REF_STATUS_NONE: | |
1965ff74 | 634 | print_ref_status('X', "[no match]", ref, NULL, NULL, porcelain); |
64fcef2d DB |
635 | break; |
636 | case REF_STATUS_REJECT_NODELETE: | |
637 | print_ref_status('!', "[rejected]", ref, NULL, | |
1965ff74 | 638 | "remote does not support deleting refs", porcelain); |
64fcef2d DB |
639 | break; |
640 | case REF_STATUS_UPTODATE: | |
641 | print_ref_status('=', "[up to date]", ref, | |
1965ff74 | 642 | ref->peer_ref, NULL, porcelain); |
64fcef2d DB |
643 | break; |
644 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
645 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
a75d7b54 | 646 | "non-fast-forward", porcelain); |
64fcef2d DB |
647 | break; |
648 | case REF_STATUS_REMOTE_REJECT: | |
649 | print_ref_status('!', "[remote rejected]", ref, | |
1965ff74 LA |
650 | ref->deletion ? NULL : ref->peer_ref, |
651 | ref->remote_status, porcelain); | |
64fcef2d DB |
652 | break; |
653 | case REF_STATUS_EXPECTING_REPORT: | |
654 | print_ref_status('!', "[remote failure]", ref, | |
1965ff74 LA |
655 | ref->deletion ? NULL : ref->peer_ref, |
656 | "remote failed to report status", porcelain); | |
64fcef2d DB |
657 | break; |
658 | case REF_STATUS_OK: | |
1965ff74 | 659 | print_ok_ref_status(ref, porcelain); |
64fcef2d DB |
660 | break; |
661 | } | |
662 | ||
663 | return 1; | |
664 | } | |
665 | ||
1965ff74 | 666 | static void print_push_status(const char *dest, struct ref *refs, |
07436e43 | 667 | int verbose, int porcelain, int * nonfastforward) |
64fcef2d DB |
668 | { |
669 | struct ref *ref; | |
670 | int n = 0; | |
671 | ||
672 | if (verbose) { | |
673 | for (ref = refs; ref; ref = ref->next) | |
674 | if (ref->status == REF_STATUS_UPTODATE) | |
1965ff74 | 675 | n += print_one_push_status(ref, dest, n, porcelain); |
64fcef2d DB |
676 | } |
677 | ||
678 | for (ref = refs; ref; ref = ref->next) | |
679 | if (ref->status == REF_STATUS_OK) | |
1965ff74 | 680 | n += print_one_push_status(ref, dest, n, porcelain); |
64fcef2d | 681 | |
07436e43 | 682 | *nonfastforward = 0; |
64fcef2d DB |
683 | for (ref = refs; ref; ref = ref->next) { |
684 | if (ref->status != REF_STATUS_NONE && | |
685 | ref->status != REF_STATUS_UPTODATE && | |
686 | ref->status != REF_STATUS_OK) | |
1965ff74 | 687 | n += print_one_push_status(ref, dest, n, porcelain); |
07436e43 MM |
688 | if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) |
689 | *nonfastforward = 1; | |
64fcef2d DB |
690 | } |
691 | } | |
692 | ||
693 | static void verify_remote_names(int nr_heads, const char **heads) | |
694 | { | |
695 | int i; | |
696 | ||
697 | for (i = 0; i < nr_heads; i++) { | |
698 | const char *local = heads[i]; | |
699 | const char *remote = strrchr(heads[i], ':'); | |
700 | ||
701 | if (*local == '+') | |
702 | local++; | |
703 | ||
704 | /* A matching refspec is okay. */ | |
705 | if (remote == local && remote[1] == '\0') | |
706 | continue; | |
707 | ||
708 | remote = remote ? (remote + 1) : local; | |
709 | switch (check_ref_format(remote)) { | |
710 | case 0: /* ok */ | |
711 | case CHECK_REF_FORMAT_ONELEVEL: | |
712 | /* ok but a single level -- that is fine for | |
713 | * a match pattern. | |
714 | */ | |
715 | case CHECK_REF_FORMAT_WILDCARD: | |
716 | /* ok but ends with a pattern-match character */ | |
717 | continue; | |
718 | } | |
719 | die("remote part of refspec is not a valid name in %s", | |
720 | heads[i]); | |
721 | } | |
722 | } | |
723 | ||
724 | static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags) | |
f3fa1838 | 725 | { |
9b288516 | 726 | struct git_transport_data *data = transport->data; |
40cb4fab | 727 | struct send_pack_args args; |
64fcef2d DB |
728 | int ret; |
729 | ||
61b075bd | 730 | if (!data->got_remote_heads) { |
64fcef2d DB |
731 | struct ref *tmp_refs; |
732 | connect_setup(transport, 1, 0); | |
733 | ||
734 | get_remote_heads(data->fd[0], &tmp_refs, 0, NULL, REF_NORMAL, | |
735 | NULL); | |
61b075bd | 736 | data->got_remote_heads = 1; |
64fcef2d | 737 | } |
9b288516 | 738 | |
de1a2fdd | 739 | memset(&args, 0, sizeof(args)); |
94c89ba6 | 740 | args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); |
40cb4fab | 741 | args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); |
aa5af974 | 742 | args.use_thin_pack = data->options.thin; |
a108e538 | 743 | args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE); |
12070329 | 744 | args.quiet = !!(flags & TRANSPORT_PUSH_QUIET); |
40cb4fab DB |
745 | args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); |
746 | ||
64fcef2d DB |
747 | ret = send_pack(&args, data->fd, data->conn, remote_refs, |
748 | &data->extra_have); | |
749 | ||
750 | close(data->fd[1]); | |
751 | close(data->fd[0]); | |
752 | ret |= finish_connect(data->conn); | |
753 | data->conn = NULL; | |
61b075bd | 754 | data->got_remote_heads = 0; |
64fcef2d DB |
755 | |
756 | return ret; | |
9b288516 DB |
757 | } |
758 | ||
b236752a IL |
759 | static int connect_git(struct transport *transport, const char *name, |
760 | const char *executable, int fd[2]) | |
761 | { | |
762 | struct git_transport_data *data = transport->data; | |
763 | data->conn = git_connect(data->fd, transport->url, | |
764 | executable, 0); | |
765 | fd[0] = data->fd[0]; | |
766 | fd[1] = data->fd[1]; | |
767 | return 0; | |
768 | } | |
769 | ||
f4e95765 SP |
770 | static int disconnect_git(struct transport *transport) |
771 | { | |
ba227857 DB |
772 | struct git_transport_data *data = transport->data; |
773 | if (data->conn) { | |
61b075bd IL |
774 | if (data->got_remote_heads) |
775 | packet_flush(data->fd[1]); | |
ba227857 DB |
776 | close(data->fd[0]); |
777 | close(data->fd[1]); | |
778 | finish_connect(data->conn); | |
779 | } | |
780 | ||
781 | free(data); | |
f4e95765 SP |
782 | return 0; |
783 | } | |
784 | ||
61b075bd IL |
785 | void transport_take_over(struct transport *transport, |
786 | struct child_process *child) | |
787 | { | |
788 | struct git_transport_data *data; | |
789 | ||
790 | if (!transport->smart_options) | |
791 | die("Bug detected: Taking over transport requires non-NULL " | |
792 | "smart_options field."); | |
793 | ||
794 | data = xcalloc(1, sizeof(*data)); | |
795 | data->options = *transport->smart_options; | |
796 | data->conn = child; | |
797 | data->fd[0] = data->conn->out; | |
798 | data->fd[1] = data->conn->in; | |
799 | data->got_remote_heads = 0; | |
800 | transport->data = data; | |
801 | ||
802 | transport->set_option = NULL; | |
803 | transport->get_refs_list = get_refs_via_connect; | |
804 | transport->fetch = fetch_refs_via_pack; | |
805 | transport->push = NULL; | |
806 | transport->push_refs = git_transport_push; | |
807 | transport->disconnect = disconnect_git; | |
808 | transport->smart_options = &(data->options); | |
809 | } | |
810 | ||
9b288516 DB |
811 | static int is_local(const char *url) |
812 | { | |
813 | const char *colon = strchr(url, ':'); | |
814 | const char *slash = strchr(url, '/'); | |
be501813 JS |
815 | return !colon || (slash && slash < colon) || |
816 | has_dos_drive_prefix(url); | |
9b288516 DB |
817 | } |
818 | ||
819 | static int is_file(const char *url) | |
820 | { | |
821 | struct stat buf; | |
822 | if (stat(url, &buf)) | |
823 | return 0; | |
824 | return S_ISREG(buf.st_mode); | |
825 | } | |
826 | ||
25d5cc48 IL |
827 | static int is_url(const char *url) |
828 | { | |
829 | const char *url2, *first_slash; | |
830 | ||
831 | if (!url) | |
832 | return 0; | |
833 | url2 = url; | |
834 | first_slash = strchr(url, '/'); | |
835 | ||
836 | /* Input with no slash at all or slash first can't be URL. */ | |
837 | if (!first_slash || first_slash == url) | |
838 | return 0; | |
839 | /* Character before must be : and next must be /. */ | |
840 | if (first_slash[-1] != ':' || first_slash[1] != '/') | |
841 | return 0; | |
842 | /* There must be something before the :// */ | |
843 | if (first_slash == url + 1) | |
844 | return 0; | |
845 | /* | |
846 | * Check all characters up to first slash - 1. Only alphanum | |
847 | * is allowed. | |
848 | */ | |
849 | url2 = url; | |
850 | while (url2 < first_slash - 1) { | |
851 | if (!isalnum((unsigned char)*url2)) | |
852 | return 0; | |
853 | url2++; | |
854 | } | |
855 | ||
856 | /* Valid enough. */ | |
857 | return 1; | |
858 | } | |
859 | ||
860 | static int external_specification_len(const char *url) | |
861 | { | |
862 | return strchr(url, ':') - url; | |
863 | } | |
864 | ||
e5f4e214 | 865 | struct transport *transport_get(struct remote *remote, const char *url) |
9b288516 | 866 | { |
8eb554ae SP |
867 | struct transport *ret = xcalloc(1, sizeof(*ret)); |
868 | ||
c1d45cf7 DB |
869 | if (!remote) |
870 | die("No remote provided to transport_get()"); | |
871 | ||
8eb554ae | 872 | ret->remote = remote; |
fb0cc87e DB |
873 | |
874 | if (!url && remote && remote->url) | |
875 | url = remote->url[0]; | |
8eb554ae | 876 | ret->url = url; |
8eb554ae | 877 | |
27a557a9 IL |
878 | /* In case previous URL had helper forced, reset it. */ |
879 | remote->foreign_vcs = NULL; | |
880 | ||
87422439 JS |
881 | /* maybe it is a foreign URL? */ |
882 | if (url) { | |
883 | const char *p = url; | |
884 | ||
885 | while (isalnum(*p)) | |
886 | p++; | |
887 | if (!prefixcmp(p, "::")) | |
888 | remote->foreign_vcs = xstrndup(url, p - url); | |
889 | } | |
890 | ||
c578f51d DB |
891 | if (remote && remote->foreign_vcs) { |
892 | transport_helper_init(ret, remote->foreign_vcs); | |
25d5cc48 | 893 | } else if (!prefixcmp(url, "rsync:")) { |
cd547b48 JS |
894 | ret->get_refs_list = get_refs_via_rsync; |
895 | ret->fetch = fetch_objs_via_rsync; | |
896 | ret->push = rsync_transport_push; | |
aa5af974 | 897 | ret->smart_options = NULL; |
9b288516 | 898 | } else if (is_local(url) && is_file(url)) { |
c7a8a162 | 899 | struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); |
c7a8a162 | 900 | ret->data = data; |
824d5776 SP |
901 | ret->get_refs_list = get_refs_from_bundle; |
902 | ret->fetch = fetch_refs_from_bundle; | |
903 | ret->disconnect = close_bundle; | |
aa5af974 | 904 | ret->smart_options = NULL; |
25d5cc48 IL |
905 | } else if (!is_url(url) |
906 | || !prefixcmp(url, "file://") | |
907 | || !prefixcmp(url, "git://") | |
908 | || !prefixcmp(url, "ssh://") | |
909 | || !prefixcmp(url, "git+ssh://") | |
910 | || !prefixcmp(url, "ssh+git://")) { | |
911 | /* These are builtin smart transports. */ | |
9b288516 | 912 | struct git_transport_data *data = xcalloc(1, sizeof(*data)); |
9b288516 | 913 | ret->data = data; |
aa5af974 | 914 | ret->set_option = NULL; |
824d5776 SP |
915 | ret->get_refs_list = get_refs_via_connect; |
916 | ret->fetch = fetch_refs_via_pack; | |
64fcef2d | 917 | ret->push_refs = git_transport_push; |
b236752a | 918 | ret->connect = connect_git; |
f4e95765 | 919 | ret->disconnect = disconnect_git; |
aa5af974 | 920 | ret->smart_options = &(data->options); |
824d5776 | 921 | |
ba227857 | 922 | data->conn = NULL; |
61b075bd | 923 | data->got_remote_heads = 0; |
25d5cc48 IL |
924 | } else { |
925 | /* Unknown protocol in URL. Pass to external handler. */ | |
926 | int len = external_specification_len(url); | |
927 | char *handler = xmalloc(len + 1); | |
928 | handler[len] = 0; | |
929 | strncpy(handler, url, len); | |
930 | transport_helper_init(ret, handler); | |
9b288516 | 931 | } |
8eb554ae | 932 | |
aa5af974 IL |
933 | if (ret->smart_options) { |
934 | ret->smart_options->thin = 1; | |
935 | ret->smart_options->uploadpack = "git-upload-pack"; | |
936 | if (remote->uploadpack) | |
937 | ret->smart_options->uploadpack = remote->uploadpack; | |
938 | ret->smart_options->receivepack = "git-receive-pack"; | |
939 | if (remote->receivepack) | |
940 | ret->smart_options->receivepack = remote->receivepack; | |
941 | } | |
942 | ||
9b288516 DB |
943 | return ret; |
944 | } | |
945 | ||
946 | int transport_set_option(struct transport *transport, | |
947 | const char *name, const char *value) | |
948 | { | |
aa5af974 IL |
949 | int git_reports = 1, protocol_reports = 1; |
950 | ||
951 | if (transport->smart_options) | |
952 | git_reports = set_git_option(transport->smart_options, | |
953 | name, value); | |
954 | ||
824d5776 | 955 | if (transport->set_option) |
aa5af974 IL |
956 | protocol_reports = transport->set_option(transport, name, |
957 | value); | |
958 | ||
959 | /* If either report is 0, report 0 (success). */ | |
960 | if (!git_reports || !protocol_reports) | |
961 | return 0; | |
962 | /* If either reports -1 (invalid value), report -1. */ | |
963 | if ((git_reports == -1) || (protocol_reports == -1)) | |
964 | return -1; | |
965 | /* Otherwise if both report unknown, report unknown. */ | |
ab865e6e | 966 | return 1; |
9b288516 DB |
967 | } |
968 | ||
969 | int transport_push(struct transport *transport, | |
07436e43 | 970 | int refspec_nr, const char **refspec, int flags, |
bb8cccd0 | 971 | int *nonfastforward) |
9b288516 | 972 | { |
bb8cccd0 | 973 | *nonfastforward = 0; |
64fcef2d DB |
974 | verify_remote_names(refspec_nr, refspec); |
975 | ||
61b075bd | 976 | if (transport->push) { |
64fcef2d | 977 | return transport->push(transport, refspec_nr, refspec, flags); |
61b075bd | 978 | } else if (transport->push_refs) { |
64fcef2d DB |
979 | struct ref *remote_refs = |
980 | transport->get_refs_list(transport, 1); | |
64fcef2d DB |
981 | struct ref *local_refs = get_local_heads(); |
982 | int match_flags = MATCH_REFS_NONE; | |
983 | int verbose = flags & TRANSPORT_PUSH_VERBOSE; | |
481c7a6d | 984 | int quiet = flags & TRANSPORT_PUSH_QUIET; |
1965ff74 | 985 | int porcelain = flags & TRANSPORT_PUSH_PORCELAIN; |
64fcef2d DB |
986 | int ret; |
987 | ||
988 | if (flags & TRANSPORT_PUSH_ALL) | |
989 | match_flags |= MATCH_REFS_ALL; | |
990 | if (flags & TRANSPORT_PUSH_MIRROR) | |
991 | match_flags |= MATCH_REFS_MIRROR; | |
992 | ||
6d2bf96e | 993 | if (match_refs(local_refs, &remote_refs, |
64fcef2d DB |
994 | refspec_nr, refspec, match_flags)) { |
995 | return -1; | |
996 | } | |
997 | ||
998 | ret = transport->push_refs(transport, remote_refs, flags); | |
999 | ||
481c7a6d JK |
1000 | if (!quiet || push_had_errors(remote_refs)) |
1001 | print_push_status(transport->url, remote_refs, | |
6ffd7812 JH |
1002 | verbose | porcelain, porcelain, |
1003 | nonfastforward); | |
64fcef2d DB |
1004 | |
1005 | if (!(flags & TRANSPORT_PUSH_DRY_RUN)) { | |
1006 | struct ref *ref; | |
1007 | for (ref = remote_refs; ref; ref = ref->next) | |
1008 | update_tracking_ref(transport->remote, ref, verbose); | |
1009 | } | |
1010 | ||
2e5ed5f2 | 1011 | if (!quiet && !ret && !refs_pushed(remote_refs)) |
64fcef2d DB |
1012 | fprintf(stderr, "Everything up-to-date\n"); |
1013 | return ret; | |
1014 | } | |
1015 | return 1; | |
9b288516 DB |
1016 | } |
1017 | ||
4577370e | 1018 | const struct ref *transport_get_remote_refs(struct transport *transport) |
c29727d5 DB |
1019 | { |
1020 | if (!transport->remote_refs) | |
64fcef2d | 1021 | transport->remote_refs = transport->get_refs_list(transport, 0); |
61b075bd | 1022 | |
c29727d5 DB |
1023 | return transport->remote_refs; |
1024 | } | |
1025 | ||
37148311 | 1026 | int transport_fetch_refs(struct transport *transport, struct ref *refs) |
c29727d5 | 1027 | { |
425b1393 | 1028 | int rc; |
86386829 | 1029 | int nr_heads = 0, nr_alloc = 0, nr_refs = 0; |
37148311 DB |
1030 | struct ref **heads = NULL; |
1031 | struct ref *rm; | |
c29727d5 DB |
1032 | |
1033 | for (rm = refs; rm; rm = rm->next) { | |
86386829 | 1034 | nr_refs++; |
c29727d5 | 1035 | if (rm->peer_ref && |
37148311 | 1036 | !is_null_sha1(rm->old_sha1) && |
c29727d5 DB |
1037 | !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1)) |
1038 | continue; | |
7a2bff45 | 1039 | ALLOC_GROW(heads, nr_heads + 1, nr_alloc); |
425b1393 | 1040 | heads[nr_heads++] = rm; |
c29727d5 DB |
1041 | } |
1042 | ||
86386829 NP |
1043 | if (!nr_heads) { |
1044 | /* | |
1045 | * When deepening of a shallow repository is requested, | |
1046 | * then local and remote refs are likely to still be equal. | |
1047 | * Just feed them all to the fetch method in that case. | |
1048 | * This condition shouldn't be met in a non-deepening fetch | |
1049 | * (see builtin-fetch.c:quickfetch()). | |
1050 | */ | |
1051 | heads = xmalloc(nr_refs * sizeof(*heads)); | |
1052 | for (rm = refs; rm; rm = rm->next) | |
1053 | heads[nr_heads++] = rm; | |
1054 | } | |
1055 | ||
824d5776 | 1056 | rc = transport->fetch(transport, nr_heads, heads); |
61b075bd | 1057 | |
c29727d5 | 1058 | free(heads); |
425b1393 | 1059 | return rc; |
c29727d5 DB |
1060 | } |
1061 | ||
1788c39c SP |
1062 | void transport_unlock_pack(struct transport *transport) |
1063 | { | |
1064 | if (transport->pack_lockfile) { | |
691f1a28 | 1065 | unlink_or_warn(transport->pack_lockfile); |
1788c39c SP |
1066 | free(transport->pack_lockfile); |
1067 | transport->pack_lockfile = NULL; | |
1068 | } | |
1069 | } | |
1070 | ||
b236752a IL |
1071 | int transport_connect(struct transport *transport, const char *name, |
1072 | const char *exec, int fd[2]) | |
1073 | { | |
1074 | if (transport->connect) | |
1075 | return transport->connect(transport, name, exec, fd); | |
1076 | else | |
1077 | die("Operation not supported by protocol"); | |
1078 | } | |
1079 | ||
9b288516 DB |
1080 | int transport_disconnect(struct transport *transport) |
1081 | { | |
1082 | int ret = 0; | |
824d5776 SP |
1083 | if (transport->disconnect) |
1084 | ret = transport->disconnect(transport); | |
9b288516 DB |
1085 | free(transport); |
1086 | return ret; | |
1087 | } | |
47abd85b AE |
1088 | |
1089 | /* | |
1090 | * Strip username (and password) from an url and return | |
1091 | * it in a newly allocated string. | |
1092 | */ | |
1093 | char *transport_anonymize_url(const char *url) | |
1094 | { | |
1095 | char *anon_url, *scheme_prefix, *anon_part; | |
1096 | size_t anon_len, prefix_len = 0; | |
1097 | ||
1098 | anon_part = strchr(url, '@'); | |
1099 | if (is_local(url) || !anon_part) | |
1100 | goto literal_copy; | |
1101 | ||
1102 | anon_len = strlen(++anon_part); | |
1103 | scheme_prefix = strstr(url, "://"); | |
1104 | if (!scheme_prefix) { | |
1105 | if (!strchr(anon_part, ':')) | |
1106 | /* cannot be "me@there:/path/name" */ | |
1107 | goto literal_copy; | |
1108 | } else { | |
1109 | const char *cp; | |
1110 | /* make sure scheme is reasonable */ | |
1111 | for (cp = url; cp < scheme_prefix; cp++) { | |
1112 | switch (*cp) { | |
1113 | /* RFC 1738 2.1 */ | |
1114 | case '+': case '.': case '-': | |
1115 | break; /* ok */ | |
1116 | default: | |
1117 | if (isalnum(*cp)) | |
1118 | break; | |
1119 | /* it isn't */ | |
1120 | goto literal_copy; | |
1121 | } | |
1122 | } | |
1123 | /* @ past the first slash does not count */ | |
1124 | cp = strchr(scheme_prefix + 3, '/'); | |
1125 | if (cp && cp < anon_part) | |
1126 | goto literal_copy; | |
1127 | prefix_len = scheme_prefix - url + 3; | |
1128 | } | |
1129 | anon_url = xcalloc(1, 1 + prefix_len + anon_len); | |
1130 | memcpy(anon_url, url, prefix_len); | |
1131 | memcpy(anon_url + prefix_len, anon_part, anon_len); | |
1132 | return anon_url; | |
1133 | literal_copy: | |
1134 | return xstrdup(url); | |
1135 | } |