diff options
Diffstat (limited to 'transport.c')
-rw-r--r-- | transport.c | 187 |
1 files changed, 103 insertions, 84 deletions
diff --git a/transport.c b/transport.c index de255880a4..40692f8ae8 100644 --- a/transport.c +++ b/transport.c @@ -3,6 +3,8 @@ #include "run-command.h" #include "pkt-line.h" #include "fetch-pack.h" +#include "remote.h" +#include "connect.h" #include "send-pack.h" #include "walker.h" #include "bundle.h" @@ -12,6 +14,7 @@ #include "url.h" #include "submodule.h" #include "string-list.h" +#include "sha1-array.h" /* rsync support */ @@ -114,7 +117,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list) return; } - if (hexval(buffer[0]) > 0xf) + if (!isxdigit(buffer[0])) continue; len = strlen(buffer); if (len && buffer[len - 1] == '\n') @@ -165,15 +168,16 @@ static void set_upstreams(struct transport *transport, struct ref *refs, /* Follow symbolic refs (mainly for HEAD). */ localname = ref->peer_ref->name; remotename = ref->name; - tmp = resolve_ref_unsafe(localname, sha, 1, &flag); + tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, + sha, &flag); if (tmp && flag & REF_ISSYMREF && - !prefixcmp(tmp, "refs/heads/")) + starts_with(tmp, "refs/heads/")) localname = tmp; /* Both source and destination must be local branches. */ - if (!localname || prefixcmp(localname, "refs/heads/")) + if (!localname || !starts_with(localname, "refs/heads/")) continue; - if (!remotename || prefixcmp(remotename, "refs/heads/")) + if (!remotename || !starts_with(remotename, "refs/heads/")) continue; if (!pretend) @@ -189,14 +193,16 @@ static void set_upstreams(struct transport *transport, struct ref *refs, static const char *rsync_url(const char *url) { - return prefixcmp(url, "rsync://") ? skip_prefix(url, "rsync:") : url; + if (!starts_with(url, "rsync://")) + skip_prefix(url, "rsync:", &url); + return url; } static struct ref *get_refs_via_rsync(struct transport *transport, int for_push) { struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; struct ref dummy = {NULL}, *tail = &dummy; - struct child_process rsync; + struct child_process rsync = CHILD_PROCESS_INIT; const char *args[5]; int temp_dir_len; @@ -213,7 +219,6 @@ static struct ref *get_refs_via_rsync(struct transport *transport, int for_push) strbuf_addstr(&buf, rsync_url(transport->url)); strbuf_addstr(&buf, "/refs"); - memset(&rsync, 0, sizeof(rsync)); rsync.argv = args; rsync.stdout_to_stderr = 1; args[0] = "rsync"; @@ -258,69 +263,54 @@ static struct ref *get_refs_via_rsync(struct transport *transport, int for_push) static int fetch_objs_via_rsync(struct transport *transport, int nr_objs, struct ref **to_fetch) { - struct strbuf buf = STRBUF_INIT; - struct child_process rsync; - const char *args[8]; - int result; + struct child_process rsync = CHILD_PROCESS_INIT; - strbuf_addstr(&buf, rsync_url(transport->url)); - strbuf_addstr(&buf, "/objects/"); - - memset(&rsync, 0, sizeof(rsync)); - rsync.argv = args; rsync.stdout_to_stderr = 1; - args[0] = "rsync"; - args[1] = (transport->verbose > 1) ? "-rv" : "-r"; - args[2] = "--ignore-existing"; - args[3] = "--exclude"; - args[4] = "info"; - args[5] = buf.buf; - args[6] = get_object_directory(); - args[7] = NULL; + argv_array_push(&rsync.args, "rsync"); + argv_array_push(&rsync.args, (transport->verbose > 1) ? "-rv" : "-r"); + argv_array_push(&rsync.args, "--ignore-existing"); + argv_array_push(&rsync.args, "--exclude"); + argv_array_push(&rsync.args, "info"); + argv_array_pushf(&rsync.args, "%s/objects/", rsync_url(transport->url)); + argv_array_push(&rsync.args, get_object_directory()); /* NEEDSWORK: handle one level of alternates */ - result = run_command(&rsync); - - strbuf_release(&buf); - - return result; + return run_command(&rsync); } -static int write_one_ref(const char *name, const unsigned char *sha1, - int flags, void *data) +static int write_one_ref(const char *name, const struct object_id *oid, + int flags, void *data) { struct strbuf *buf = data; int len = buf->len; - FILE *f; /* when called via for_each_ref(), flags is non-zero */ - if (flags && prefixcmp(name, "refs/heads/") && - prefixcmp(name, "refs/tags/")) + if (flags && !starts_with(name, "refs/heads/") && + !starts_with(name, "refs/tags/")) return 0; strbuf_addstr(buf, name); if (safe_create_leading_directories(buf->buf) || - !(f = fopen(buf->buf, "w")) || - fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 || - fclose(f)) - return error("problems writing temporary file %s", buf->buf); + write_file(buf->buf, 0, "%s\n", oid_to_hex(oid))) + return error("problems writing temporary file %s: %s", + buf->buf, strerror(errno)); strbuf_setlen(buf, len); return 0; } static int write_refs_to_temp_dir(struct strbuf *temp_dir, - int refspec_nr, const char **refspec) + int refspec_nr, const char **refspec) { int i; for (i = 0; i < refspec_nr; i++) { - unsigned char sha1[20]; + struct object_id oid; char *ref; - if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1) + if (dwim_ref(refspec[i], strlen(refspec[i]), oid.hash, &ref) != 1) return error("Could not get ref %s", refspec[i]); - if (write_one_ref(ref, sha1, 0, temp_dir)) { + if (write_one_ref(ref, &oid, 0, temp_dir)) { free(ref); return -1; } @@ -334,7 +324,7 @@ static int rsync_transport_push(struct transport *transport, { struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; int result = 0, i; - struct child_process rsync; + struct child_process rsync = CHILD_PROCESS_INIT; const char *args[10]; if (flags & TRANSPORT_PUSH_MIRROR) @@ -345,7 +335,6 @@ static int rsync_transport_push(struct transport *transport, strbuf_addstr(&buf, rsync_url(transport->url)); strbuf_addch(&buf, '/'); - memset(&rsync, 0, sizeof(rsync)); rsync.argv = args; rsync.stdout_to_stderr = 1; i = 0; @@ -452,7 +441,8 @@ struct git_transport_data { struct child_process *conn; int fd[2]; unsigned got_remote_heads : 1; - struct extra_have_objects extra_have; + struct sha1_array extra_have; + struct sha1_array shallow; }; static int set_git_option(struct git_transport_options *opts, @@ -473,6 +463,9 @@ static int set_git_option(struct git_transport_options *opts, } else if (!strcmp(name, TRANS_OPT_KEEP)) { opts->keep = !!value; return 0; + } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) { + opts->update_shallow = !!value; + return 0; } else if (!strcmp(name, TRANS_OPT_DEPTH)) { if (!value) opts->depth = 0; @@ -483,6 +476,9 @@ static int set_git_option(struct git_transport_options *opts, die("transport: invalid depth option '%s'", value); } return 0; + } else if (!strcmp(name, TRANS_OPT_PUSH_CERT)) { + opts->push_cert = !!value; + return 0; } return 1; } @@ -509,7 +505,9 @@ static struct ref *get_refs_via_connect(struct transport *transport, int for_pus connect_setup(transport, for_push, 0); get_remote_heads(data->fd[0], NULL, 0, &refs, - for_push ? REF_NORMAL : 0, &data->extra_have); + for_push ? REF_NORMAL : 0, + &data->extra_have, + &data->shallow); data->got_remote_heads = 1; return refs; @@ -519,7 +517,7 @@ static int fetch_refs_via_pack(struct transport *transport, int nr_heads, struct ref **to_fetch) { struct git_transport_data *data = transport->data; - const struct ref *refs; + struct ref *refs; char *dest = xstrdup(transport->url); struct fetch_pack_args args; struct ref *refs_tmp = NULL; @@ -536,28 +534,33 @@ static int fetch_refs_via_pack(struct transport *transport, args.depth = data->options.depth; args.check_self_contained_and_connected = data->options.check_self_contained_and_connected; + args.cloning = transport->cloning; + args.update_shallow = data->options.update_shallow; if (!data->got_remote_heads) { connect_setup(transport, 0, 0); - get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0, NULL); + get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0, + NULL, &data->shallow); data->got_remote_heads = 1; } refs = fetch_pack(&args, data->fd, data->conn, refs_tmp ? refs_tmp : transport->remote_refs, - dest, to_fetch, nr_heads, + dest, to_fetch, nr_heads, &data->shallow, &transport->pack_lockfile); close(data->fd[0]); close(data->fd[1]); - if (finish_connect(data->conn)) + if (finish_connect(data->conn)) { + free_refs(refs); refs = NULL; + } data->conn = NULL; data->got_remote_heads = 0; data->options.self_contained_and_connected = args.self_contained_and_connected; free_refs(refs_tmp); - + free_refs(refs); free(dest); return (refs ? 0 : -1); } @@ -650,7 +653,7 @@ static void print_ok_ref_status(struct ref *ref, int porcelain) print_ref_status('-', "[deleted]", ref, NULL, NULL, porcelain); else if (is_null_sha1(ref->old_sha1)) print_ref_status('*', - (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" : + (starts_with(ref->name, "refs/tags/") ? "[new tag]" : "[new branch]"), ref, ref->peer_ref, NULL, porcelain); else { @@ -707,6 +710,14 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i print_ref_status('!', "[rejected]", ref, ref->peer_ref, "needs force", porcelain); break; + case REF_STATUS_REJECT_STALE: + print_ref_status('!', "[rejected]", ref, ref->peer_ref, + "stale info", porcelain); + break; + case REF_STATUS_REJECT_SHALLOW: + print_ref_status('!', "[rejected]", ref, ref->peer_ref, + "new shallow roots not allowed", porcelain); + break; case REF_STATUS_REMOTE_REJECT: print_ref_status('!', "[remote rejected]", ref, ref->deletion ? NULL : ref->peer_ref, @@ -717,6 +728,10 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, i ref->deletion ? NULL : ref->peer_ref, "remote failed to report status", porcelain); break; + case REF_STATUS_ATOMIC_PUSH_FAILED: + print_ref_status('!', "[rejected]", ref, ref->peer_ref, + "atomic push failed", porcelain); + break; case REF_STATUS_OK: print_ok_ref_status(ref, porcelain); break; @@ -733,7 +748,7 @@ void transport_print_push_status(const char *dest, struct ref *refs, unsigned char head_sha1[20]; char *head; - head = resolve_refdup("HEAD", head_sha1, 1, NULL); + head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL); if (verbose) { for (ref = refs; ref; ref = ref->next) @@ -764,6 +779,7 @@ void transport_print_push_status(const char *dest, struct ref *refs, *reject_reasons |= REJECT_NEEDS_FORCE; } } + free(head); } void transport_verify_remote_names(int nr_heads, const char **heads) @@ -799,7 +815,8 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re struct ref *tmp_refs; connect_setup(transport, 1, 0); - get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL, NULL); + get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL, + NULL, &data->shallow); data->got_remote_heads = 1; } @@ -812,6 +829,9 @@ static int git_transport_push(struct transport *transport, struct ref *remote_re args.progress = transport->progress; args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN); + args.push_cert = !!(flags & TRANSPORT_PUSH_CERT); + args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC); + args.url = transport->url; ret = send_pack(&args, data->fd, data->conn, remote_refs, &data->extra_have); @@ -879,14 +899,6 @@ void transport_take_over(struct transport *transport, transport->cannot_reuse = 1; } -static int is_local(const char *url) -{ - const char *colon = strchr(url, ':'); - const char *slash = strchr(url, '/'); - return !colon || (slash && slash < colon) || - has_dos_drive_prefix(url); -} - static int is_file(const char *url) { struct stat buf; @@ -924,18 +936,18 @@ struct transport *transport_get(struct remote *remote, const char *url) while (is_urlschemechar(p == url, *p)) p++; - if (!prefixcmp(p, "::")) + if (starts_with(p, "::")) helper = xstrndup(url, p - url); } if (helper) { transport_helper_init(ret, helper); - } else if (!prefixcmp(url, "rsync:")) { + } else if (starts_with(url, "rsync:")) { ret->get_refs_list = get_refs_via_rsync; ret->fetch = fetch_objs_via_rsync; ret->push = rsync_transport_push; ret->smart_options = NULL; - } else if (is_local(url) && is_file(url) && is_bundle(url, 1)) { + } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) { struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); ret->data = data; ret->get_refs_list = get_refs_from_bundle; @@ -943,11 +955,11 @@ struct transport *transport_get(struct remote *remote, const char *url) ret->disconnect = close_bundle; ret->smart_options = NULL; } else if (!is_url(url) - || !prefixcmp(url, "file://") - || !prefixcmp(url, "git://") - || !prefixcmp(url, "ssh://") - || !prefixcmp(url, "git+ssh://") - || !prefixcmp(url, "ssh+git://")) { + || starts_with(url, "file://") + || starts_with(url, "git://") + || starts_with(url, "ssh://") + || starts_with(url, "git+ssh://") + || starts_with(url, "ssh+git://")) { /* These are builtin smart transports. */ struct git_transport_data *data = xcalloc(1, sizeof(*data)); ret->data = data; @@ -964,9 +976,7 @@ struct transport *transport_get(struct remote *remote, const char *url) } else { /* Unknown protocol in URL. Pass to external handler. */ int len = external_specification_len(url); - char *handler = xmalloc(len + 1); - handler[len] = 0; - strncpy(handler, url, len); + char *handler = xmemdupz(url, len); transport_helper_init(ret, handler); } @@ -1053,7 +1063,7 @@ static int run_pre_push_hook(struct transport *transport, { int ret = 0, x; struct ref *r; - struct child_process proc; + struct child_process proc = CHILD_PROCESS_INIT; struct strbuf buf; const char *argv[4]; @@ -1064,7 +1074,6 @@ static int run_pre_push_hook(struct transport *transport, argv[2] = transport->url; argv[3] = NULL; - memset(&proc, 0, sizeof(proc)); proc.argv = argv; proc.in = -1; @@ -1078,6 +1087,7 @@ static int run_pre_push_hook(struct transport *transport, for (r = remote_refs; r; r = r->next) { if (!r->peer_ref) continue; if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue; + if (r->status == REF_STATUS_REJECT_STALE) continue; if (r->status == REF_STATUS_UPTODATE) continue; strbuf_reset(&buf); @@ -1118,8 +1128,7 @@ int transport_push(struct transport *transport, return transport->push(transport, refspec_nr, refspec, flags); } else if (transport->push_refs) { - struct ref *remote_refs = - transport->get_refs_list(transport, 1); + struct ref *remote_refs; struct ref *local_refs = get_local_heads(); int match_flags = MATCH_REFS_NONE; int verbose = (transport->verbose > 0); @@ -1128,6 +1137,11 @@ int transport_push(struct transport *transport, int pretend = flags & TRANSPORT_PUSH_DRY_RUN; int push_ret, ret, err; + if (check_push_refs(local_refs, refspec_nr, refspec) < 0) + return -1; + + remote_refs = transport->get_refs_list(transport, 1); + if (flags & TRANSPORT_PUSH_ALL) match_flags |= MATCH_REFS_ALL; if (flags & TRANSPORT_PUSH_MIRROR) @@ -1142,6 +1156,12 @@ int transport_push(struct transport *transport, return -1; } + if (transport->smart_options && + transport->smart_options->cas && + !is_empty_cas(transport->smart_options->cas)) + apply_push_cas(transport->smart_options->cas, + transport->remote, remote_refs); + set_ref_status_for_push(remote_refs, flags & TRANSPORT_PUSH_MIRROR, flags & TRANSPORT_PUSH_FORCE); @@ -1162,10 +1182,8 @@ int transport_push(struct transport *transport, if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) { struct ref *ref = remote_refs; - struct string_list needs_pushing; + struct string_list needs_pushing = STRING_LIST_INIT_DUP; - memset(&needs_pushing, 0, sizeof(struct string_list)); - needs_pushing.strdup_strings = 1; for (; ref; ref = ref->next) if (!is_null_sha1(ref->new_sha1) && find_unpushed_submodules(ref->new_sha1, @@ -1284,7 +1302,7 @@ char *transport_anonymize_url(const char *url) size_t anon_len, prefix_len = 0; anon_part = strchr(url, '@'); - if (is_local(url) || !anon_part) + if (url_is_local_not_ssh(url) || !anon_part) goto literal_copy; anon_len = strlen(++anon_part); @@ -1345,11 +1363,11 @@ static int refs_from_alternate_cb(struct alternate_object_database *e, while (other[len-1] == '/') other[--len] = '\0'; if (len < 8 || memcmp(other + len - 8, "/objects", 8)) - return 0; + goto out; /* Is this a git repository with refs? */ memcpy(other + len - 8, "/refs", 6); if (!is_directory(other)) - return 0; + goto out; other[len - 8] = '\0'; remote = remote_get(other); transport = transport_get(remote, other); @@ -1358,6 +1376,7 @@ static int refs_from_alternate_cb(struct alternate_object_database *e, extra = extra->next) cb->fn(extra, cb->data); transport_disconnect(transport); +out: free(other); return 0; } |