diff options
Diffstat (limited to 'transport.c')
-rw-r--r-- | transport.c | 194 |
1 files changed, 117 insertions, 77 deletions
diff --git a/transport.c b/transport.c index b58f5c8cdc..fc802260f6 100644 --- a/transport.c +++ b/transport.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "config.h" #include "transport.h" #include "run-command.h" #include "pkt-line.h" @@ -16,6 +17,7 @@ #include "string-list.h" #include "sha1-array.h" #include "sigchain.h" +#include "transport-internal.h" static void set_upstreams(struct transport *transport, struct ref *refs, int pretend) @@ -25,7 +27,6 @@ static void set_upstreams(struct transport *transport, struct ref *refs, const char *localname; const char *tmp; const char *remotename; - unsigned char sha[20]; int flag = 0; /* * Check suitability for tracking. Must be successful / @@ -43,7 +44,7 @@ static void set_upstreams(struct transport *transport, struct ref *refs, localname = ref->peer_ref->name; remotename = ref->name; tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, - sha, &flag); + NULL, &flag); if (tmp && flag & REF_ISSYMREF && starts_with(tmp, "refs/heads/")) localname = tmp; @@ -87,7 +88,7 @@ static struct ref *get_refs_from_bundle(struct transport *transport, int for_pus for (i = 0; i < data->header.references.nr; i++) { struct ref_list_entry *e = data->header.references.list + i; struct ref *ref = alloc_ref(e->name); - hashcpy(ref->old_oid.hash, e->sha1); + oidcpy(&ref->old_oid, &e->oid); ref->next = result; result = ref; } @@ -116,8 +117,8 @@ struct git_transport_data { struct child_process *conn; int fd[2]; unsigned got_remote_heads : 1; - struct sha1_array extra_have; - struct sha1_array shallow; + struct oid_array extra_have; + struct oid_array shallow; }; static int set_git_option(struct git_transport_options *opts, @@ -303,10 +304,10 @@ void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int v if (verbose) fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); if (ref->deletion) { - delete_ref(rs.dst, NULL, 0); + delete_ref(NULL, rs.dst, NULL, 0); } else - update_ref("update by push", rs.dst, - ref->new_oid.hash, NULL, 0, 0); + update_ref("update by push", rs.dst, &ref->new_oid, + NULL, 0, 0); free(rs.dst); } } @@ -447,7 +448,7 @@ static int print_one_push_status(struct ref *ref, const char *dest, int count, static int measure_abbrev(const struct object_id *oid, int sofar) { - char hex[GIT_SHA1_HEXSZ + 1]; + char hex[GIT_MAX_HEXSZ + 1]; int w = find_unique_abbrev_r(hex, oid->hash, DEFAULT_ABBREV); return (w < sofar) ? sofar : w; @@ -471,11 +472,10 @@ void transport_print_push_status(const char *dest, struct ref *refs, { struct ref *ref; int n = 0; - unsigned char head_sha1[20]; char *head; int summary_width = transport_summary_width(refs); - head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL); + head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); if (verbose) { for (ref = refs; ref; ref = ref->next) @@ -608,6 +608,15 @@ static int disconnect_git(struct transport *transport) return 0; } +static struct transport_vtable taken_over_vtable = { + NULL, + get_refs_via_connect, + fetch_refs_via_pack, + git_transport_push, + NULL, + disconnect_git +}; + void transport_take_over(struct transport *transport, struct child_process *child) { @@ -625,12 +634,7 @@ void transport_take_over(struct transport *transport, data->got_remote_heads = 0; transport->data = data; - transport->set_option = NULL; - transport->get_refs_list = get_refs_via_connect; - transport->fetch = fetch_refs_via_pack; - transport->push = NULL; - transport->push_refs = git_transport_push; - transport->disconnect = disconnect_git; + transport->vtable = &taken_over_vtable; transport->smart_options = &(data->options); transport->cannot_reuse = 1; @@ -753,6 +757,24 @@ void transport_check_allowed(const char *type) die("transport '%s' not allowed", type); } +static struct transport_vtable bundle_vtable = { + NULL, + get_refs_from_bundle, + fetch_refs_from_bundle, + NULL, + NULL, + close_bundle +}; + +static struct transport_vtable builtin_smart_vtable = { + NULL, + get_refs_via_connect, + fetch_refs_via_pack, + git_transport_push, + connect_git, + disconnect_git +}; + struct transport *transport_get(struct remote *remote, const char *url) { const char *helper; @@ -789,9 +811,7 @@ struct transport *transport_get(struct remote *remote, const char *url) struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); transport_check_allowed("file"); ret->data = data; - ret->get_refs_list = get_refs_from_bundle; - ret->fetch = fetch_refs_from_bundle; - ret->disconnect = close_bundle; + ret->vtable = &bundle_vtable; ret->smart_options = NULL; } else if (!is_url(url) || starts_with(url, "file://") @@ -806,12 +826,7 @@ struct transport *transport_get(struct remote *remote, const char *url) */ struct git_transport_data *data = xcalloc(1, sizeof(*data)); ret->data = data; - ret->set_option = NULL; - ret->get_refs_list = get_refs_via_connect; - ret->fetch = fetch_refs_via_pack; - ret->push_refs = git_transport_push; - ret->connect = connect_git; - ret->disconnect = disconnect_git; + ret->vtable = &builtin_smart_vtable; ret->smart_options = &(data->options); data->conn = NULL; @@ -845,9 +860,9 @@ int transport_set_option(struct transport *transport, git_reports = set_git_option(transport->smart_options, name, value); - if (transport->set_option) - protocol_reports = transport->set_option(transport, name, - value); + if (transport->vtable->set_option) + protocol_reports = transport->vtable->set_option(transport, + name, value); /* If either report is 0, report 0 (success). */ if (!git_reports || !protocol_reports) @@ -970,13 +985,7 @@ int transport_push(struct transport *transport, *reject_reasons = 0; transport_verify_remote_names(refspec_nr, refspec); - if (transport->push) { - /* Maybe FIXME. But no important transport uses this case. */ - if (flags & TRANSPORT_PUSH_SET_UPSTREAM) - die("This transport does not support using --set-upstream"); - - return transport->push(transport, refspec_nr, refspec, flags); - } else if (transport->push_refs) { + if (transport->vtable->push_refs) { struct ref *remote_refs; struct ref *local_refs = get_local_heads(); int match_flags = MATCH_REFS_NONE; @@ -989,7 +998,7 @@ int transport_push(struct transport *transport, if (check_push_refs(local_refs, refspec_nr, refspec) < 0) return -1; - remote_refs = transport->get_refs_list(transport, 1); + remote_refs = transport->vtable->get_refs_list(transport, 1); if (flags & TRANSPORT_PUSH_ALL) match_flags |= MATCH_REFS_ALL; @@ -1023,19 +1032,22 @@ int transport_push(struct transport *transport, TRANSPORT_RECURSE_SUBMODULES_ONLY)) && !is_bare_repository()) { struct ref *ref = remote_refs; - struct sha1_array commits = SHA1_ARRAY_INIT; + struct oid_array commits = OID_ARRAY_INIT; for (; ref; ref = ref->next) if (!is_null_oid(&ref->new_oid)) - sha1_array_append(&commits, ref->new_oid.hash); + oid_array_append(&commits, + &ref->new_oid); if (!push_unpushed_submodules(&commits, - transport->remote->name, + transport->remote, + refspec, refspec_nr, + transport->push_options, pretend)) { - sha1_array_clear(&commits); + oid_array_clear(&commits); die("Failed to push all needed submodules!"); } - sha1_array_clear(&commits); + oid_array_clear(&commits); } if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) || @@ -1044,23 +1056,24 @@ int transport_push(struct transport *transport, !pretend)) && !is_bare_repository()) { struct ref *ref = remote_refs; struct string_list needs_pushing = STRING_LIST_INIT_DUP; - struct sha1_array commits = SHA1_ARRAY_INIT; + struct oid_array commits = OID_ARRAY_INIT; for (; ref; ref = ref->next) if (!is_null_oid(&ref->new_oid)) - sha1_array_append(&commits, ref->new_oid.hash); + oid_array_append(&commits, + &ref->new_oid); if (find_unpushed_submodules(&commits, transport->remote->name, &needs_pushing)) { - sha1_array_clear(&commits); + oid_array_clear(&commits); die_with_unpushed_submodules(&needs_pushing); } string_list_clear(&needs_pushing, 0); - sha1_array_clear(&commits); + oid_array_clear(&commits); } if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) - push_ret = transport->push_refs(transport, remote_refs, flags); + push_ret = transport->vtable->push_refs(transport, remote_refs, flags); else push_ret = 0; err = push_had_errors(remote_refs); @@ -1094,7 +1107,7 @@ int transport_push(struct transport *transport, const struct ref *transport_get_remote_refs(struct transport *transport) { if (!transport->got_remote_refs) { - transport->remote_refs = transport->get_refs_list(transport, 0); + transport->remote_refs = transport->vtable->get_refs_list(transport, 0); transport->got_remote_refs = 1; } @@ -1131,7 +1144,7 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs) heads[nr_heads++] = rm; } - rc = transport->fetch(transport, nr_heads, heads); + rc = transport->vtable->fetch(transport, nr_heads, heads); free(heads); return rc; @@ -1141,16 +1154,15 @@ void transport_unlock_pack(struct transport *transport) { if (transport->pack_lockfile) { unlink_or_warn(transport->pack_lockfile); - free(transport->pack_lockfile); - transport->pack_lockfile = NULL; + FREE_AND_NULL(transport->pack_lockfile); } } int transport_connect(struct transport *transport, const char *name, const char *exec, int fd[2]) { - if (transport->connect) - return transport->connect(transport, name, exec, fd); + if (transport->vtable->connect) + return transport->vtable->connect(transport, name, exec, fd); else die("Operation not supported by protocol"); } @@ -1158,8 +1170,8 @@ int transport_connect(struct transport *transport, const char *name, int transport_disconnect(struct transport *transport) { int ret = 0; - if (transport->disconnect) - ret = transport->disconnect(transport); + if (transport->vtable->disconnect) + ret = transport->vtable->disconnect(transport); free(transport); return ret; } @@ -1210,6 +1222,42 @@ literal_copy: return xstrdup(url); } +static void read_alternate_refs(const char *path, + alternate_ref_fn *cb, + void *data) +{ + struct child_process cmd = CHILD_PROCESS_INIT; + struct strbuf line = STRBUF_INIT; + FILE *fh; + + cmd.git_cmd = 1; + argv_array_pushf(&cmd.args, "--git-dir=%s", path); + argv_array_push(&cmd.args, "for-each-ref"); + argv_array_push(&cmd.args, "--format=%(objectname) %(refname)"); + cmd.env = local_repo_env; + cmd.out = -1; + + if (start_command(&cmd)) + return; + + fh = xfdopen(cmd.out, "r"); + while (strbuf_getline_lf(&line, fh) != EOF) { + struct object_id oid; + + if (get_oid_hex(line.buf, &oid) || + line.buf[GIT_SHA1_HEXSZ] != ' ') { + warning("invalid line while parsing alternate refs: %s", + line.buf); + break; + } + + cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data); + } + + fclose(fh); + finish_command(&cmd); +} + struct alternate_refs_data { alternate_ref_fn *fn; void *data; @@ -1218,34 +1266,26 @@ struct alternate_refs_data { static int refs_from_alternate_cb(struct alternate_object_database *e, void *data) { - char *other; - size_t len; - struct remote *remote; - struct transport *transport; - const struct ref *extra; + struct strbuf path = STRBUF_INIT; + size_t base_len; struct alternate_refs_data *cb = data; - other = real_pathdup(e->path, 1); - len = strlen(other); - - while (other[len-1] == '/') - other[--len] = '\0'; - if (len < 8 || memcmp(other + len - 8, "/objects", 8)) + if (!strbuf_realpath(&path, e->path, 0)) + goto out; + if (!strbuf_strip_suffix(&path, "/objects")) goto out; + base_len = path.len; + /* Is this a git repository with refs? */ - memcpy(other + len - 8, "/refs", 6); - if (!is_directory(other)) + strbuf_addstr(&path, "/refs"); + if (!is_directory(path.buf)) goto out; - other[len - 8] = '\0'; - remote = remote_get(other); - transport = transport_get(remote, other); - for (extra = transport_get_remote_refs(transport); - extra; - extra = extra->next) - cb->fn(extra, cb->data); - transport_disconnect(transport); + strbuf_setlen(&path, base_len); + + read_alternate_refs(path.buf, cb->fn, cb->data); + out: - free(other); + strbuf_release(&path); return 0; } |