Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "transport.h" |
| 3 | #include "run-command.h" |
Johannes Schindelin | ccfc02a | 2007-10-15 14:52:25 +0100 | [diff] [blame] | 4 | #ifndef NO_CURL |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 5 | #include "http.h" |
Johannes Schindelin | ccfc02a | 2007-10-15 14:52:25 +0100 | [diff] [blame] | 6 | #endif |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 7 | #include "pkt-line.h" |
| 8 | #include "fetch-pack.h" |
Daniel Barkalow | 40cb4fa | 2007-10-29 22:03:42 -0400 | [diff] [blame] | 9 | #include "send-pack.h" |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 10 | #include "walker.h" |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 11 | #include "bundle.h" |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 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 | |
| 26 | static 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 | |
| 36 | static 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))) { |
| 53 | if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || |
| 54 | (de->d_name[1] == '.' && |
| 55 | de->d_name[2] == '\0'))) |
| 56 | continue; |
| 57 | ALLOC_GROW(list.entries, list.nr + 1, list.alloc); |
| 58 | list.entries[list.nr++] = xstrdup(de->d_name); |
| 59 | } |
| 60 | closedir(dir); |
| 61 | |
| 62 | /* sort the list */ |
| 63 | |
| 64 | qsort(list.entries, list.nr, sizeof(char *), str_cmp); |
| 65 | |
| 66 | pathlen = path->len; |
| 67 | strbuf_addch(path, '/'); |
| 68 | |
| 69 | for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) { |
| 70 | strbuf_addstr(path, list.entries[i]); |
| 71 | if (read_loose_refs(path, name_offset, tail)) { |
| 72 | int fd = open(path->buf, O_RDONLY); |
| 73 | char buffer[40]; |
| 74 | struct ref *next; |
| 75 | |
| 76 | if (fd < 0) |
| 77 | continue; |
| 78 | next = alloc_ref(path->len - name_offset + 1); |
| 79 | if (read_in_full(fd, buffer, 40) != 40 || |
| 80 | get_sha1_hex(buffer, next->old_sha1)) { |
| 81 | close(fd); |
| 82 | free(next); |
| 83 | continue; |
| 84 | } |
| 85 | close(fd); |
| 86 | strcpy(next->name, path->buf + name_offset); |
| 87 | (*tail)->next = next; |
| 88 | *tail = next; |
| 89 | } |
| 90 | } |
| 91 | strbuf_setlen(path, pathlen); |
| 92 | |
| 93 | for (i = 0; i < list.nr; i++) |
| 94 | free(list.entries[i]); |
| 95 | free(list.entries); |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | /* insert the packed refs for which no loose refs were found */ |
| 101 | |
| 102 | static void insert_packed_refs(const char *packed_refs, struct ref **list) |
| 103 | { |
| 104 | FILE *f = fopen(packed_refs, "r"); |
| 105 | static char buffer[PATH_MAX]; |
| 106 | |
| 107 | if (!f) |
| 108 | return; |
| 109 | |
| 110 | for (;;) { |
Blake Ramsdell | 47ec794 | 2007-11-01 19:38:22 -0700 | [diff] [blame] | 111 | int cmp = cmp, len; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 112 | |
| 113 | if (!fgets(buffer, sizeof(buffer), f)) { |
| 114 | fclose(f); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | if (hexval(buffer[0]) > 0xf) |
| 119 | continue; |
| 120 | len = strlen(buffer); |
Jim Meyering | 872c930 | 2008-01-04 18:37:41 +0100 | [diff] [blame] | 121 | if (len && buffer[len - 1] == '\n') |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 122 | buffer[--len] = '\0'; |
| 123 | if (len < 41) |
| 124 | continue; |
| 125 | while ((*list)->next && |
| 126 | (cmp = strcmp(buffer + 41, |
| 127 | (*list)->next->name)) > 0) |
| 128 | list = &(*list)->next; |
| 129 | if (!(*list)->next || cmp < 0) { |
| 130 | struct ref *next = alloc_ref(len - 40); |
| 131 | buffer[40] = '\0'; |
| 132 | if (get_sha1_hex(buffer, next->old_sha1)) { |
| 133 | warning ("invalid SHA-1: %s", buffer); |
| 134 | free(next); |
| 135 | continue; |
| 136 | } |
| 137 | strcpy(next->name, buffer + 41); |
| 138 | next->next = (*list)->next; |
| 139 | (*list)->next = next; |
| 140 | list = &(*list)->next; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 145 | static struct ref *get_refs_via_rsync(struct transport *transport) |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 146 | { |
| 147 | struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; |
| 148 | struct ref dummy, *tail = &dummy; |
| 149 | struct child_process rsync; |
| 150 | const char *args[5]; |
| 151 | int temp_dir_len; |
| 152 | |
| 153 | /* copy the refs to the temporary directory */ |
| 154 | |
| 155 | strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX")); |
| 156 | if (!mkdtemp(temp_dir.buf)) |
| 157 | die ("Could not make temporary directory"); |
| 158 | temp_dir_len = temp_dir.len; |
| 159 | |
| 160 | strbuf_addstr(&buf, transport->url); |
| 161 | strbuf_addstr(&buf, "/refs"); |
| 162 | |
| 163 | memset(&rsync, 0, sizeof(rsync)); |
| 164 | rsync.argv = args; |
| 165 | rsync.stdout_to_stderr = 1; |
| 166 | args[0] = "rsync"; |
Daniel Barkalow | 2b5a06e | 2007-10-02 22:49:15 -0400 | [diff] [blame] | 167 | args[1] = (transport->verbose > 0) ? "-rv" : "-r"; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 168 | args[2] = buf.buf; |
| 169 | args[3] = temp_dir.buf; |
| 170 | args[4] = NULL; |
| 171 | |
| 172 | if (run_command(&rsync)) |
| 173 | die ("Could not run rsync to get refs"); |
| 174 | |
| 175 | strbuf_reset(&buf); |
| 176 | strbuf_addstr(&buf, transport->url); |
| 177 | strbuf_addstr(&buf, "/packed-refs"); |
| 178 | |
| 179 | args[2] = buf.buf; |
| 180 | |
| 181 | if (run_command(&rsync)) |
| 182 | die ("Could not run rsync to get refs"); |
| 183 | |
| 184 | /* read the copied refs */ |
| 185 | |
| 186 | strbuf_addstr(&temp_dir, "/refs"); |
| 187 | read_loose_refs(&temp_dir, temp_dir_len + 1, &tail); |
| 188 | strbuf_setlen(&temp_dir, temp_dir_len); |
| 189 | |
| 190 | tail = &dummy; |
| 191 | strbuf_addstr(&temp_dir, "/packed-refs"); |
| 192 | insert_packed_refs(temp_dir.buf, &tail); |
| 193 | strbuf_setlen(&temp_dir, temp_dir_len); |
| 194 | |
| 195 | if (remove_dir_recursively(&temp_dir, 0)) |
| 196 | warning ("Error removing temporary directory %s.", |
| 197 | temp_dir.buf); |
| 198 | |
| 199 | strbuf_release(&buf); |
| 200 | strbuf_release(&temp_dir); |
| 201 | |
| 202 | return dummy.next; |
| 203 | } |
| 204 | |
| 205 | static int fetch_objs_via_rsync(struct transport *transport, |
| 206 | int nr_objs, struct ref **to_fetch) |
| 207 | { |
| 208 | struct strbuf buf = STRBUF_INIT; |
| 209 | struct child_process rsync; |
| 210 | const char *args[8]; |
| 211 | int result; |
| 212 | |
| 213 | strbuf_addstr(&buf, transport->url); |
| 214 | strbuf_addstr(&buf, "/objects/"); |
| 215 | |
| 216 | memset(&rsync, 0, sizeof(rsync)); |
| 217 | rsync.argv = args; |
| 218 | rsync.stdout_to_stderr = 1; |
| 219 | args[0] = "rsync"; |
Daniel Barkalow | 2b5a06e | 2007-10-02 22:49:15 -0400 | [diff] [blame] | 220 | args[1] = (transport->verbose > 0) ? "-rv" : "-r"; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 221 | args[2] = "--ignore-existing"; |
| 222 | args[3] = "--exclude"; |
| 223 | args[4] = "info"; |
| 224 | args[5] = buf.buf; |
| 225 | args[6] = get_object_directory(); |
| 226 | args[7] = NULL; |
| 227 | |
| 228 | /* NEEDSWORK: handle one level of alternates */ |
| 229 | result = run_command(&rsync); |
| 230 | |
| 231 | strbuf_release(&buf); |
| 232 | |
| 233 | return result; |
| 234 | } |
| 235 | |
| 236 | static int write_one_ref(const char *name, const unsigned char *sha1, |
| 237 | int flags, void *data) |
| 238 | { |
| 239 | struct strbuf *buf = data; |
| 240 | int len = buf->len; |
| 241 | FILE *f; |
| 242 | |
| 243 | /* when called via for_each_ref(), flags is non-zero */ |
| 244 | if (flags && prefixcmp(name, "refs/heads/") && |
| 245 | prefixcmp(name, "refs/tags/")) |
| 246 | return 0; |
| 247 | |
| 248 | strbuf_addstr(buf, name); |
| 249 | if (safe_create_leading_directories(buf->buf) || |
| 250 | !(f = fopen(buf->buf, "w")) || |
| 251 | fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 || |
| 252 | fclose(f)) |
| 253 | return error("problems writing temporary file %s", buf->buf); |
| 254 | strbuf_setlen(buf, len); |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int write_refs_to_temp_dir(struct strbuf *temp_dir, |
| 259 | int refspec_nr, const char **refspec) |
| 260 | { |
| 261 | int i; |
| 262 | |
| 263 | for (i = 0; i < refspec_nr; i++) { |
| 264 | unsigned char sha1[20]; |
| 265 | char *ref; |
| 266 | |
| 267 | if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1) |
| 268 | return error("Could not get ref %s", refspec[i]); |
| 269 | |
| 270 | if (write_one_ref(ref, sha1, 0, temp_dir)) { |
| 271 | free(ref); |
| 272 | return -1; |
| 273 | } |
| 274 | free(ref); |
| 275 | } |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | static int rsync_transport_push(struct transport *transport, |
| 280 | int refspec_nr, const char **refspec, int flags) |
| 281 | { |
| 282 | struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; |
| 283 | int result = 0, i; |
| 284 | struct child_process rsync; |
Shawn O. Pearce | ee020f3 | 2007-10-16 00:28:32 -0400 | [diff] [blame] | 285 | const char *args[10]; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 286 | |
Andy Whitcroft | 94c89ba | 2007-11-09 23:32:25 +0000 | [diff] [blame] | 287 | if (flags & TRANSPORT_PUSH_MIRROR) |
| 288 | return error("rsync transport does not support mirror mode"); |
| 289 | |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 290 | /* first push the objects */ |
| 291 | |
| 292 | strbuf_addstr(&buf, transport->url); |
| 293 | strbuf_addch(&buf, '/'); |
| 294 | |
| 295 | memset(&rsync, 0, sizeof(rsync)); |
| 296 | rsync.argv = args; |
| 297 | rsync.stdout_to_stderr = 1; |
Shawn O. Pearce | ee020f3 | 2007-10-16 00:28:32 -0400 | [diff] [blame] | 298 | i = 0; |
| 299 | args[i++] = "rsync"; |
| 300 | args[i++] = "-a"; |
| 301 | if (flags & TRANSPORT_PUSH_DRY_RUN) |
| 302 | args[i++] = "--dry-run"; |
| 303 | if (transport->verbose > 0) |
| 304 | args[i++] = "-v"; |
| 305 | args[i++] = "--ignore-existing"; |
| 306 | args[i++] = "--exclude"; |
| 307 | args[i++] = "info"; |
| 308 | args[i++] = get_object_directory(); |
| 309 | args[i++] = buf.buf; |
| 310 | args[i++] = NULL; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 311 | |
| 312 | if (run_command(&rsync)) |
| 313 | return error("Could not push objects to %s", transport->url); |
| 314 | |
| 315 | /* copy the refs to the temporary directory; they could be packed. */ |
| 316 | |
| 317 | strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX")); |
| 318 | if (!mkdtemp(temp_dir.buf)) |
| 319 | die ("Could not make temporary directory"); |
| 320 | strbuf_addch(&temp_dir, '/'); |
| 321 | |
| 322 | if (flags & TRANSPORT_PUSH_ALL) { |
| 323 | if (for_each_ref(write_one_ref, &temp_dir)) |
| 324 | return -1; |
| 325 | } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec)) |
| 326 | return -1; |
| 327 | |
Shawn O. Pearce | ee020f3 | 2007-10-16 00:28:32 -0400 | [diff] [blame] | 328 | i = 2; |
| 329 | if (flags & TRANSPORT_PUSH_DRY_RUN) |
| 330 | args[i++] = "--dry-run"; |
| 331 | if (!(flags & TRANSPORT_PUSH_FORCE)) |
| 332 | args[i++] = "--ignore-existing"; |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 333 | args[i++] = temp_dir.buf; |
| 334 | args[i++] = transport->url; |
| 335 | args[i++] = NULL; |
| 336 | if (run_command(&rsync)) |
| 337 | result = error("Could not push to %s", transport->url); |
| 338 | |
| 339 | if (remove_dir_recursively(&temp_dir, 0)) |
| 340 | warning ("Could not remove temporary directory %s.", |
| 341 | temp_dir.buf); |
| 342 | |
| 343 | strbuf_release(&buf); |
| 344 | strbuf_release(&temp_dir); |
| 345 | |
| 346 | return result; |
| 347 | } |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 348 | |
| 349 | /* Generic functions for using commit walkers */ |
| 350 | |
Jeff King | b9c506f | 2007-11-18 03:17:23 -0500 | [diff] [blame] | 351 | #ifndef NO_CURL /* http fetch is the only user */ |
Shawn O. Pearce | 1788c39 | 2007-09-14 03:31:23 -0400 | [diff] [blame] | 352 | static int fetch_objs_via_walker(struct transport *transport, |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 353 | int nr_objs, struct ref **to_fetch) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 354 | { |
| 355 | char *dest = xstrdup(transport->url); |
| 356 | struct walker *walker = transport->data; |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 357 | char **objs = xmalloc(nr_objs * sizeof(*objs)); |
| 358 | int i; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 359 | |
| 360 | walker->get_all = 1; |
| 361 | walker->get_tree = 1; |
| 362 | walker->get_history = 1; |
Daniel Barkalow | 2b5a06e | 2007-10-02 22:49:15 -0400 | [diff] [blame] | 363 | walker->get_verbosely = transport->verbose >= 0; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 364 | walker->get_recover = 0; |
| 365 | |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 366 | for (i = 0; i < nr_objs; i++) |
| 367 | objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1)); |
| 368 | |
Shawn O. Pearce | b6abb48 | 2007-09-15 03:23:04 -0400 | [diff] [blame] | 369 | if (walker_fetch(walker, nr_objs, objs, NULL, NULL)) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 370 | die("Fetch failed."); |
| 371 | |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 372 | for (i = 0; i < nr_objs; i++) |
| 373 | free(objs[i]); |
| 374 | free(objs); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 375 | free(dest); |
| 376 | return 0; |
| 377 | } |
Jeff King | b9c506f | 2007-11-18 03:17:23 -0500 | [diff] [blame] | 378 | #endif /* NO_CURL */ |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 379 | |
| 380 | static int disconnect_walker(struct transport *transport) |
| 381 | { |
| 382 | struct walker *walker = transport->data; |
| 383 | if (walker) |
| 384 | walker_free(walker); |
| 385 | return 0; |
| 386 | } |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 387 | |
Johannes Schindelin | ccfc02a | 2007-10-15 14:52:25 +0100 | [diff] [blame] | 388 | #ifndef NO_CURL |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 389 | static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) |
| 390 | { |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 391 | const char **argv; |
| 392 | int argc; |
| 393 | int err; |
| 394 | |
Andy Whitcroft | 94c89ba | 2007-11-09 23:32:25 +0000 | [diff] [blame] | 395 | if (flags & TRANSPORT_PUSH_MIRROR) |
| 396 | return error("http transport does not support mirror mode"); |
| 397 | |
Steffen Prohaska | 1b2486d | 2007-11-11 15:01:44 +0100 | [diff] [blame] | 398 | argv = xmalloc((refspec_nr + 12) * sizeof(char *)); |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 399 | argv[0] = "http-push"; |
| 400 | argc = 1; |
| 401 | if (flags & TRANSPORT_PUSH_ALL) |
| 402 | argv[argc++] = "--all"; |
| 403 | if (flags & TRANSPORT_PUSH_FORCE) |
| 404 | argv[argc++] = "--force"; |
Shawn O. Pearce | fe5d1d3 | 2007-10-16 00:35:22 -0400 | [diff] [blame] | 405 | if (flags & TRANSPORT_PUSH_DRY_RUN) |
| 406 | argv[argc++] = "--dry-run"; |
Steffen Prohaska | 1b2486d | 2007-11-11 15:01:44 +0100 | [diff] [blame] | 407 | if (flags & TRANSPORT_PUSH_VERBOSE) |
| 408 | argv[argc++] = "--verbose"; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 409 | argv[argc++] = transport->url; |
| 410 | while (refspec_nr--) |
| 411 | argv[argc++] = *refspec++; |
| 412 | argv[argc] = NULL; |
| 413 | err = run_command_v_opt(argv, RUN_GIT_CMD); |
| 414 | switch (err) { |
| 415 | case -ERR_RUN_COMMAND_FORK: |
| 416 | error("unable to fork for %s", argv[0]); |
| 417 | case -ERR_RUN_COMMAND_EXEC: |
| 418 | error("unable to exec %s", argv[0]); |
| 419 | break; |
| 420 | case -ERR_RUN_COMMAND_WAITPID: |
| 421 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: |
| 422 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: |
| 423 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: |
| 424 | error("%s died with strange error", argv[0]); |
| 425 | } |
| 426 | return !!err; |
| 427 | } |
| 428 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 429 | static struct ref *get_refs_via_curl(struct transport *transport) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 430 | { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 431 | struct strbuf buffer = STRBUF_INIT; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 432 | char *data, *start, *mid; |
| 433 | char *ref_name; |
| 434 | char *refs_url; |
| 435 | int i = 0; |
| 436 | |
| 437 | struct active_request_slot *slot; |
| 438 | struct slot_results results; |
| 439 | |
| 440 | struct ref *refs = NULL; |
| 441 | struct ref *ref = NULL; |
| 442 | struct ref *last_ref = NULL; |
| 443 | |
Mike Hommey | 9fd9279 | 2008-02-09 10:41:36 +0100 | [diff] [blame] | 444 | if (!transport->data) |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 445 | transport->data = get_http_walker(transport->url, |
| 446 | transport->remote); |
Mike Hommey | 9fd9279 | 2008-02-09 10:41:36 +0100 | [diff] [blame] | 447 | |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 448 | refs_url = xmalloc(strlen(transport->url) + 11); |
| 449 | sprintf(refs_url, "%s/info/refs", transport->url); |
| 450 | |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 451 | slot = get_active_slot(); |
| 452 | slot->results = &results; |
| 453 | curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer); |
| 454 | curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); |
| 455 | curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url); |
| 456 | curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL); |
Sam Vilain | 14c9821 | 2007-12-04 10:48:54 +1300 | [diff] [blame] | 457 | |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 458 | if (start_active_slot(slot)) { |
| 459 | run_active_slot(slot); |
| 460 | if (results.curl_result != CURLE_OK) { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 461 | strbuf_release(&buffer); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 462 | if (missing_target(&results)) { |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 463 | return NULL; |
| 464 | } else { |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 465 | error("%s", curl_errorstr); |
| 466 | return NULL; |
| 467 | } |
| 468 | } |
| 469 | } else { |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 470 | strbuf_release(&buffer); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 471 | error("Unable to start request"); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 475 | data = buffer.buf; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 476 | start = NULL; |
| 477 | mid = data; |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 478 | while (i < buffer.len) { |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 479 | if (!start) |
| 480 | start = &data[i]; |
| 481 | if (data[i] == '\t') |
| 482 | mid = &data[i]; |
| 483 | if (data[i] == '\n') { |
| 484 | data[i] = 0; |
| 485 | ref_name = mid + 1; |
| 486 | ref = xmalloc(sizeof(struct ref) + |
| 487 | strlen(ref_name) + 1); |
| 488 | memset(ref, 0, sizeof(struct ref)); |
| 489 | strcpy(ref->name, ref_name); |
| 490 | get_sha1_hex(start, ref->old_sha1); |
| 491 | if (!refs) |
| 492 | refs = ref; |
| 493 | if (last_ref) |
| 494 | last_ref->next = ref; |
| 495 | last_ref = ref; |
| 496 | start = NULL; |
| 497 | } |
| 498 | i++; |
| 499 | } |
| 500 | |
Mike Hommey | 028c297 | 2007-12-09 20:30:59 +0100 | [diff] [blame] | 501 | strbuf_release(&buffer); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 502 | |
| 503 | return refs; |
| 504 | } |
| 505 | |
Shawn O. Pearce | e5f4e21 | 2007-09-15 03:23:14 -0400 | [diff] [blame] | 506 | static int fetch_objs_via_curl(struct transport *transport, |
| 507 | int nr_objs, struct ref **to_fetch) |
| 508 | { |
| 509 | if (!transport->data) |
Mike Hommey | 9fc6440 | 2008-02-27 21:35:50 +0100 | [diff] [blame] | 510 | transport->data = get_http_walker(transport->url, |
| 511 | transport->remote); |
Shawn O. Pearce | e5f4e21 | 2007-09-15 03:23:14 -0400 | [diff] [blame] | 512 | return fetch_objs_via_walker(transport, nr_objs, to_fetch); |
| 513 | } |
| 514 | |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 515 | #endif |
| 516 | |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 517 | struct bundle_transport_data { |
| 518 | int fd; |
| 519 | struct bundle_header header; |
| 520 | }; |
| 521 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 522 | static struct ref *get_refs_from_bundle(struct transport *transport) |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 523 | { |
| 524 | struct bundle_transport_data *data = transport->data; |
| 525 | struct ref *result = NULL; |
| 526 | int i; |
| 527 | |
| 528 | if (data->fd > 0) |
| 529 | close(data->fd); |
| 530 | data->fd = read_bundle_header(transport->url, &data->header); |
| 531 | if (data->fd < 0) |
| 532 | die ("Could not read bundle '%s'.", transport->url); |
| 533 | for (i = 0; i < data->header.references.nr; i++) { |
| 534 | struct ref_list_entry *e = data->header.references.list + i; |
Junio C Hamano | 90446a0 | 2007-09-28 01:46:13 -0700 | [diff] [blame] | 535 | struct ref *ref = alloc_ref(strlen(e->name) + 1); |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 536 | hashcpy(ref->old_sha1, e->sha1); |
| 537 | strcpy(ref->name, e->name); |
| 538 | ref->next = result; |
| 539 | result = ref; |
| 540 | } |
| 541 | return result; |
| 542 | } |
| 543 | |
Shawn O. Pearce | 1788c39 | 2007-09-14 03:31:23 -0400 | [diff] [blame] | 544 | static int fetch_refs_from_bundle(struct transport *transport, |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 545 | int nr_heads, struct ref **to_fetch) |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 546 | { |
| 547 | struct bundle_transport_data *data = transport->data; |
| 548 | return unbundle(&data->header, data->fd); |
| 549 | } |
| 550 | |
| 551 | static int close_bundle(struct transport *transport) |
| 552 | { |
| 553 | struct bundle_transport_data *data = transport->data; |
| 554 | if (data->fd > 0) |
| 555 | close(data->fd); |
Shawn O. Pearce | f4e9576 | 2007-09-19 00:49:42 -0400 | [diff] [blame] | 556 | free(data); |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 557 | return 0; |
| 558 | } |
| 559 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 560 | struct git_transport_data { |
| 561 | unsigned thin : 1; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 562 | unsigned keep : 1; |
Shawn O. Pearce | 41fa7d2 | 2008-03-03 22:27:40 -0500 | [diff] [blame] | 563 | unsigned followtags : 1; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 564 | int depth; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 565 | struct child_process *conn; |
| 566 | int fd[2]; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 567 | const char *uploadpack; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 568 | const char *receivepack; |
| 569 | }; |
| 570 | |
| 571 | static int set_git_option(struct transport *connection, |
| 572 | const char *name, const char *value) |
| 573 | { |
| 574 | struct git_transport_data *data = connection->data; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 575 | if (!strcmp(name, TRANS_OPT_UPLOADPACK)) { |
| 576 | data->uploadpack = value; |
| 577 | return 0; |
| 578 | } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) { |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 579 | data->receivepack = value; |
| 580 | return 0; |
| 581 | } else if (!strcmp(name, TRANS_OPT_THIN)) { |
| 582 | data->thin = !!value; |
| 583 | return 0; |
Shawn O. Pearce | 41fa7d2 | 2008-03-03 22:27:40 -0500 | [diff] [blame] | 584 | } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) { |
| 585 | data->followtags = !!value; |
| 586 | return 0; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 587 | } else if (!strcmp(name, TRANS_OPT_KEEP)) { |
| 588 | data->keep = !!value; |
| 589 | return 0; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 590 | } else if (!strcmp(name, TRANS_OPT_DEPTH)) { |
| 591 | if (!value) |
| 592 | data->depth = 0; |
| 593 | else |
| 594 | data->depth = atoi(value); |
| 595 | return 0; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 596 | } |
| 597 | return 1; |
| 598 | } |
| 599 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 600 | static int connect_setup(struct transport *transport) |
| 601 | { |
| 602 | struct git_transport_data *data = transport->data; |
| 603 | data->conn = git_connect(data->fd, transport->url, data->uploadpack, 0); |
| 604 | return 0; |
| 605 | } |
| 606 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 607 | static struct ref *get_refs_via_connect(struct transport *transport) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 608 | { |
| 609 | struct git_transport_data *data = transport->data; |
| 610 | struct ref *refs; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 611 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 612 | connect_setup(transport); |
| 613 | get_remote_heads(data->fd[0], &refs, 0, NULL, 0); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 614 | |
| 615 | return refs; |
| 616 | } |
| 617 | |
Shawn O. Pearce | 1788c39 | 2007-09-14 03:31:23 -0400 | [diff] [blame] | 618 | static int fetch_refs_via_pack(struct transport *transport, |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 619 | int nr_heads, struct ref **to_fetch) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 620 | { |
| 621 | struct git_transport_data *data = transport->data; |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 622 | char **heads = xmalloc(nr_heads * sizeof(*heads)); |
Shawn O. Pearce | e4cd6c7 | 2007-09-14 18:57:11 -0400 | [diff] [blame] | 623 | char **origh = xmalloc(nr_heads * sizeof(*origh)); |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 624 | const struct ref *refs; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 625 | char *dest = xstrdup(transport->url); |
| 626 | struct fetch_pack_args args; |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 627 | int i; |
Daniel Barkalow | 00183cb | 2008-02-28 11:10:51 -0500 | [diff] [blame] | 628 | struct ref *refs_tmp = NULL; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 629 | |
Shawn O. Pearce | fa74052 | 2007-09-19 00:49:35 -0400 | [diff] [blame] | 630 | memset(&args, 0, sizeof(args)); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 631 | args.uploadpack = data->uploadpack; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 632 | args.keep_pack = data->keep; |
Shawn O. Pearce | fa74052 | 2007-09-19 00:49:35 -0400 | [diff] [blame] | 633 | args.lock_pack = 1; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 634 | args.use_thin_pack = data->thin; |
Shawn O. Pearce | 41fa7d2 | 2008-03-03 22:27:40 -0500 | [diff] [blame] | 635 | args.include_tag = data->followtags; |
Daniel Barkalow | 2b5a06e | 2007-10-02 22:49:15 -0400 | [diff] [blame] | 636 | args.verbose = transport->verbose > 0; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 637 | args.depth = data->depth; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 638 | |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 639 | for (i = 0; i < nr_heads; i++) |
Shawn O. Pearce | e4cd6c7 | 2007-09-14 18:57:11 -0400 | [diff] [blame] | 640 | origh[i] = heads[i] = xstrdup(to_fetch[i]->name); |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 641 | |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 642 | if (!data->conn) { |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 643 | connect_setup(transport); |
| 644 | get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0); |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 645 | } |
| 646 | |
Daniel Barkalow | 00183cb | 2008-02-28 11:10:51 -0500 | [diff] [blame] | 647 | refs = fetch_pack(&args, data->fd, data->conn, |
| 648 | refs_tmp ? refs_tmp : transport->remote_refs, |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 649 | dest, nr_heads, heads, &transport->pack_lockfile); |
| 650 | close(data->fd[0]); |
| 651 | close(data->fd[1]); |
| 652 | if (finish_connect(data->conn)) |
| 653 | refs = NULL; |
| 654 | data->conn = NULL; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 655 | |
Daniel Barkalow | 00183cb | 2008-02-28 11:10:51 -0500 | [diff] [blame] | 656 | free_refs(refs_tmp); |
| 657 | |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 658 | for (i = 0; i < nr_heads; i++) |
Shawn O. Pearce | e4cd6c7 | 2007-09-14 18:57:11 -0400 | [diff] [blame] | 659 | free(origh[i]); |
| 660 | free(origh); |
| 661 | free(heads); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 662 | free(dest); |
Junio C Hamano | c6bc400 | 2007-12-04 23:01:19 -0800 | [diff] [blame] | 663 | return (refs ? 0 : -1); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 664 | } |
| 665 | |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 666 | static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) |
| 667 | { |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 668 | struct git_transport_data *data = transport->data; |
Daniel Barkalow | 40cb4fa | 2007-10-29 22:03:42 -0400 | [diff] [blame] | 669 | struct send_pack_args args; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 670 | |
Daniel Barkalow | 40cb4fa | 2007-10-29 22:03:42 -0400 | [diff] [blame] | 671 | args.receivepack = data->receivepack; |
| 672 | args.send_all = !!(flags & TRANSPORT_PUSH_ALL); |
Andy Whitcroft | 94c89ba | 2007-11-09 23:32:25 +0000 | [diff] [blame] | 673 | args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); |
Daniel Barkalow | 40cb4fa | 2007-10-29 22:03:42 -0400 | [diff] [blame] | 674 | args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); |
| 675 | args.use_thin_pack = data->thin; |
Junio C Hamano | a108e53 | 2007-11-14 03:09:52 -0800 | [diff] [blame] | 676 | args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE); |
Daniel Barkalow | 40cb4fa | 2007-10-29 22:03:42 -0400 | [diff] [blame] | 677 | args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); |
| 678 | |
| 679 | return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec); |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 680 | } |
| 681 | |
Shawn O. Pearce | f4e9576 | 2007-09-19 00:49:42 -0400 | [diff] [blame] | 682 | static int disconnect_git(struct transport *transport) |
| 683 | { |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 684 | struct git_transport_data *data = transport->data; |
| 685 | if (data->conn) { |
| 686 | packet_flush(data->fd[1]); |
| 687 | close(data->fd[0]); |
| 688 | close(data->fd[1]); |
| 689 | finish_connect(data->conn); |
| 690 | } |
| 691 | |
| 692 | free(data); |
Shawn O. Pearce | f4e9576 | 2007-09-19 00:49:42 -0400 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 696 | static int is_local(const char *url) |
| 697 | { |
| 698 | const char *colon = strchr(url, ':'); |
| 699 | const char *slash = strchr(url, '/'); |
| 700 | return !colon || (slash && slash < colon); |
| 701 | } |
| 702 | |
| 703 | static int is_file(const char *url) |
| 704 | { |
| 705 | struct stat buf; |
| 706 | if (stat(url, &buf)) |
| 707 | return 0; |
| 708 | return S_ISREG(buf.st_mode); |
| 709 | } |
| 710 | |
Shawn O. Pearce | e5f4e21 | 2007-09-15 03:23:14 -0400 | [diff] [blame] | 711 | struct transport *transport_get(struct remote *remote, const char *url) |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 712 | { |
Shawn O. Pearce | 8eb554a | 2007-09-15 03:23:07 -0400 | [diff] [blame] | 713 | struct transport *ret = xcalloc(1, sizeof(*ret)); |
| 714 | |
| 715 | ret->remote = remote; |
| 716 | ret->url = url; |
Shawn O. Pearce | 8eb554a | 2007-09-15 03:23:07 -0400 | [diff] [blame] | 717 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 718 | if (!prefixcmp(url, "rsync://")) { |
Johannes Schindelin | cd547b4 | 2007-10-01 00:59:39 +0100 | [diff] [blame] | 719 | ret->get_refs_list = get_refs_via_rsync; |
| 720 | ret->fetch = fetch_objs_via_rsync; |
| 721 | ret->push = rsync_transport_push; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 722 | |
Shawn O. Pearce | 8eb554a | 2007-09-15 03:23:07 -0400 | [diff] [blame] | 723 | } else if (!prefixcmp(url, "http://") |
| 724 | || !prefixcmp(url, "https://") |
| 725 | || !prefixcmp(url, "ftp://")) { |
Johannes Schindelin | ccfc02a | 2007-10-15 14:52:25 +0100 | [diff] [blame] | 726 | #ifdef NO_CURL |
| 727 | error("git was compiled without libcurl support."); |
| 728 | #else |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 729 | ret->get_refs_list = get_refs_via_curl; |
| 730 | ret->fetch = fetch_objs_via_curl; |
| 731 | ret->push = curl_transport_push; |
Johannes Schindelin | ccfc02a | 2007-10-15 14:52:25 +0100 | [diff] [blame] | 732 | #endif |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 733 | ret->disconnect = disconnect_walker; |
| 734 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 735 | } else if (is_local(url) && is_file(url)) { |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 736 | struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); |
Johannes Schindelin | c7a8a16 | 2007-09-10 23:03:21 -0400 | [diff] [blame] | 737 | ret->data = data; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 738 | ret->get_refs_list = get_refs_from_bundle; |
| 739 | ret->fetch = fetch_refs_from_bundle; |
| 740 | ret->disconnect = close_bundle; |
| 741 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 742 | } else { |
| 743 | struct git_transport_data *data = xcalloc(1, sizeof(*data)); |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 744 | ret->data = data; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 745 | ret->set_option = set_git_option; |
| 746 | ret->get_refs_list = get_refs_via_connect; |
| 747 | ret->fetch = fetch_refs_via_pack; |
| 748 | ret->push = git_transport_push; |
Shawn O. Pearce | f4e9576 | 2007-09-19 00:49:42 -0400 | [diff] [blame] | 749 | ret->disconnect = disconnect_git; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 750 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 751 | data->thin = 1; |
Daniel Barkalow | ba22785 | 2008-02-04 13:26:23 -0500 | [diff] [blame] | 752 | data->conn = NULL; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 753 | data->uploadpack = "git-upload-pack"; |
| 754 | if (remote && remote->uploadpack) |
| 755 | data->uploadpack = remote->uploadpack; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 756 | data->receivepack = "git-receive-pack"; |
| 757 | if (remote && remote->receivepack) |
| 758 | data->receivepack = remote->receivepack; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 759 | } |
Shawn O. Pearce | 8eb554a | 2007-09-15 03:23:07 -0400 | [diff] [blame] | 760 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | int transport_set_option(struct transport *transport, |
| 765 | const char *name, const char *value) |
| 766 | { |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 767 | if (transport->set_option) |
| 768 | return transport->set_option(transport, name, value); |
Shawn O. Pearce | ab865e6 | 2007-09-18 04:54:57 -0400 | [diff] [blame] | 769 | return 1; |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | int transport_push(struct transport *transport, |
| 773 | int refspec_nr, const char **refspec, int flags) |
| 774 | { |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 775 | if (!transport->push) |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 776 | return 1; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 777 | return transport->push(transport, refspec_nr, refspec, flags); |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 778 | } |
| 779 | |
Daniel Barkalow | 4577370 | 2007-10-29 21:05:40 -0400 | [diff] [blame] | 780 | const struct ref *transport_get_remote_refs(struct transport *transport) |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 781 | { |
| 782 | if (!transport->remote_refs) |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 783 | transport->remote_refs = transport->get_refs_list(transport); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 784 | return transport->remote_refs; |
| 785 | } |
| 786 | |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 787 | int transport_fetch_refs(struct transport *transport, struct ref *refs) |
| 788 | { |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 789 | int rc; |
Shawn O. Pearce | 7a2bff4 | 2007-09-14 03:31:18 -0400 | [diff] [blame] | 790 | int nr_heads = 0, nr_alloc = 0; |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 791 | struct ref **heads = NULL; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 792 | struct ref *rm; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 793 | |
| 794 | for (rm = refs; rm; rm = rm->next) { |
| 795 | if (rm->peer_ref && |
| 796 | !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1)) |
| 797 | continue; |
Shawn O. Pearce | 7a2bff4 | 2007-09-14 03:31:18 -0400 | [diff] [blame] | 798 | ALLOC_GROW(heads, nr_heads + 1, nr_alloc); |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 799 | heads[nr_heads++] = rm; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 800 | } |
| 801 | |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 802 | rc = transport->fetch(transport, nr_heads, heads); |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 803 | free(heads); |
Shawn O. Pearce | 425b139 | 2007-09-14 03:31:21 -0400 | [diff] [blame] | 804 | return rc; |
Daniel Barkalow | c29727d | 2007-09-10 23:03:11 -0400 | [diff] [blame] | 805 | } |
| 806 | |
Shawn O. Pearce | 1788c39 | 2007-09-14 03:31:23 -0400 | [diff] [blame] | 807 | void transport_unlock_pack(struct transport *transport) |
| 808 | { |
| 809 | if (transport->pack_lockfile) { |
| 810 | unlink(transport->pack_lockfile); |
| 811 | free(transport->pack_lockfile); |
| 812 | transport->pack_lockfile = NULL; |
| 813 | } |
| 814 | } |
| 815 | |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 816 | int transport_disconnect(struct transport *transport) |
| 817 | { |
| 818 | int ret = 0; |
Shawn O. Pearce | 824d577 | 2007-09-19 00:49:31 -0400 | [diff] [blame] | 819 | if (transport->disconnect) |
| 820 | ret = transport->disconnect(transport); |
Daniel Barkalow | 9b28851 | 2007-09-10 23:03:04 -0400 | [diff] [blame] | 821 | free(transport); |
| 822 | return ret; |
| 823 | } |