diff options
Diffstat (limited to 'upload-pack.c')
-rw-r--r-- | upload-pack.c | 183 |
1 files changed, 134 insertions, 49 deletions
diff --git a/upload-pack.c b/upload-pack.c index ce5cbbea6b..6142421ea1 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -10,6 +10,8 @@ #include "revision.h" #include "list-objects.h" #include "run-command.h" +#include "sigchain.h" +#include "version.h" static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; @@ -83,22 +85,11 @@ static void show_commit(struct commit *commit, void *data) commit->buffer = NULL; } -static void show_object(struct object *obj, const struct name_path *path, const char *component) +static void show_object(struct object *obj, + const struct name_path *path, const char *component, + void *cb_data) { - /* An object with name "foo\n0000000..." can be used to - * confuse downstream git-pack-objects very badly. - */ - const char *name = path_name(path, component); - const char *ep = strchr(name, '\n'); - if (ep) { - fprintf(pack_pipe, "%s %.*s\n", sha1_to_hex(obj->sha1), - (int) (ep - name), - name); - } - else - fprintf(pack_pipe, "%s %s\n", - sha1_to_hex(obj->sha1), name); - free((char *)name); + show_object_with_name(pack_pipe, obj, path, component); } static void show_edge(struct commit *commit) @@ -498,17 +489,104 @@ static int get_common_commits(void) } } +static void check_non_tip(void) +{ + static const char *argv[] = { + "rev-list", "--stdin", NULL, + }; + static struct child_process cmd; + struct object *o; + char namebuf[42]; /* ^ + SHA-1 + LF */ + int i; + + /* In the normal in-process case non-tip request can never happen */ + if (!stateless_rpc) + goto error; + + cmd.argv = argv; + cmd.git_cmd = 1; + cmd.no_stderr = 1; + cmd.in = -1; + cmd.out = -1; + + if (start_command(&cmd)) + goto error; + + /* + * If rev-list --stdin encounters an unknown commit, it + * terminates, which will cause SIGPIPE in the write loop + * below. + */ + sigchain_push(SIGPIPE, SIG_IGN); + + namebuf[0] = '^'; + namebuf[41] = '\n'; + for (i = get_max_object_index(); 0 < i; ) { + o = get_indexed_object(--i); + if (!o) + continue; + if (!(o->flags & OUR_REF)) + continue; + memcpy(namebuf + 1, sha1_to_hex(o->sha1), 40); + if (write_in_full(cmd.in, namebuf, 42) < 0) + goto error; + } + namebuf[40] = '\n'; + for (i = 0; i < want_obj.nr; i++) { + o = want_obj.objects[i].item; + if (o->flags & OUR_REF) + continue; + memcpy(namebuf, sha1_to_hex(o->sha1), 40); + if (write_in_full(cmd.in, namebuf, 41) < 0) + goto error; + } + close(cmd.in); + + sigchain_pop(SIGPIPE); + + /* + * The commits out of the rev-list are not ancestors of + * our ref. + */ + i = read_in_full(cmd.out, namebuf, 1); + if (i) + goto error; + close(cmd.out); + + /* + * rev-list may have died by encountering a bad commit + * in the history, in which case we do want to bail out + * even when it showed no commit. + */ + if (finish_command(&cmd)) + goto error; + + /* All the non-tip ones are ancestors of what we advertised */ + return; + +error: + /* Pick one of them (we know there at least is one) */ + for (i = 0; i < want_obj.nr; i++) { + o = want_obj.objects[i].item; + if (!(o->flags & OUR_REF)) + die("git upload-pack: not our ref %s", + sha1_to_hex(o->sha1)); + } +} + static void receive_needs(void) { struct object_array shallows = OBJECT_ARRAY_INIT; static char line[1000]; int len, depth = 0; + int has_non_tip = 0; shallow_nr = 0; if (debug_fd) write_str_in_full(debug_fd, "#S\n"); for (;;) { struct object *o; + const char *features; unsigned char sha1_buf[20]; len = packet_read_line(0, line, sizeof(line)); reset_timeout(); @@ -540,45 +618,52 @@ static void receive_needs(void) get_sha1_hex(line+5, sha1_buf)) die("git upload-pack: protocol error, " "expected to get sha, not '%s'", line); - if (strstr(line+45, "multi_ack_detailed")) + + features = line + 45; + + if (parse_feature_request(features, "multi_ack_detailed")) multi_ack = 2; - else if (strstr(line+45, "multi_ack")) + else if (parse_feature_request(features, "multi_ack")) multi_ack = 1; - if (strstr(line+45, "no-done")) + if (parse_feature_request(features, "no-done")) no_done = 1; - if (strstr(line+45, "thin-pack")) + if (parse_feature_request(features, "thin-pack")) use_thin_pack = 1; - if (strstr(line+45, "ofs-delta")) + if (parse_feature_request(features, "ofs-delta")) use_ofs_delta = 1; - if (strstr(line+45, "side-band-64k")) + if (parse_feature_request(features, "side-band-64k")) use_sideband = LARGE_PACKET_MAX; - else if (strstr(line+45, "side-band")) + else if (parse_feature_request(features, "side-band")) use_sideband = DEFAULT_PACKET_MAX; - if (strstr(line+45, "no-progress")) + if (parse_feature_request(features, "no-progress")) no_progress = 1; - if (strstr(line+45, "include-tag")) + if (parse_feature_request(features, "include-tag")) use_include_tag = 1; - /* We have sent all our refs already, and the other end - * should have chosen out of them; otherwise they are - * asking for nonsense. - * - * Hmph. We may later want to allow "want" line that - * asks for something like "master~10" (symbolic)... - * would it make sense? I don't know. - */ o = lookup_object(sha1_buf); - if (!o || !(o->flags & OUR_REF)) + if (!o) die("git upload-pack: not our ref %s", sha1_to_hex(sha1_buf)); if (!(o->flags & WANTED)) { o->flags |= WANTED; + if (!(o->flags & OUR_REF)) + has_non_tip = 1; add_object_array(o, NULL, &want_obj); } } if (debug_fd) write_str_in_full(debug_fd, "#E\n"); + /* + * We have sent all our refs already, and the other end + * should have chosen out of them. When we are operating + * in the stateless RPC mode, however, their choice may + * have been based on the set of older refs advertised + * by another process that handled the initial request. + */ + if (has_non_tip) + check_non_tip(); + if (!use_sideband && daemon_mode) no_progress = 1; @@ -640,27 +725,25 @@ static int send_ref(const char *refname, const unsigned char *sha1, int flag, vo static const char *capabilities = "multi_ack thin-pack side-band" " side-band-64k ofs-delta shallow no-progress" " include-tag multi_ack_detailed"; - struct object *o = parse_object(sha1); - - if (!o) - die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); + struct object *o = lookup_unknown_object(sha1); + const char *refname_nons = strip_namespace(refname); + unsigned char peeled[20]; if (capabilities) - packet_write(1, "%s %s%c%s%s\n", sha1_to_hex(sha1), refname, + packet_write(1, "%s %s%c%s%s agent=%s\n", + sha1_to_hex(sha1), refname_nons, 0, capabilities, - stateless_rpc ? " no-done" : ""); + stateless_rpc ? " no-done" : "", + git_user_agent_sanitized()); else - packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname); + packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); capabilities = NULL; if (!(o->flags & OUR_REF)) { o->flags |= OUR_REF; nr_our_refs++; } - if (o->type == OBJ_TAG) { - o = deref_tag(o, refname, 0); - if (o) - packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname); - } + if (!peel_ref(refname, peeled)) + packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); return 0; } @@ -680,12 +763,12 @@ static void upload_pack(void) { if (advertise_refs || !stateless_rpc) { reset_timeout(); - head_ref(send_ref, NULL); - for_each_ref(send_ref, NULL); + head_ref_namespaced(send_ref, NULL); + for_each_namespaced_ref(send_ref, NULL); packet_flush(1); } else { - head_ref(mark_our_ref, NULL); - for_each_ref(mark_our_ref, NULL); + head_ref_namespaced(mark_our_ref, NULL); + for_each_namespaced_ref(mark_our_ref, NULL); } if (advertise_refs) return; @@ -703,6 +786,8 @@ int main(int argc, char **argv) int i; int strict = 0; + git_setup_gettext(); + packet_trace_identity("upload-pack"); git_extract_argv0_path(argv[0]); read_replace_refs = 0; |