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