diff options
Diffstat (limited to 'upload-pack.c')
-rw-r--r-- | upload-pack.c | 277 |
1 files changed, 142 insertions, 135 deletions
diff --git a/upload-pack.c b/upload-pack.c index 04a8707bb5..89e832b64a 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -10,13 +10,14 @@ #include "revision.h" #include "list-objects.h" #include "run-command.h" +#include "connect.h" #include "sigchain.h" #include "version.h" #include "string-list.h" static const char upload_pack_usage[] = "git upload-pack [--strict] [--timeout=<n>] <dir>"; -/* bits #0..7 in revision.h, #8..10 in commit.c */ +/* Remember to update object flag allocation in object.h */ #define THEY_HAVE (1u << 11) #define OUR_REF (1u << 12) #define WANTED (1u << 13) @@ -34,7 +35,11 @@ static int multi_ack; static int no_done; static int use_thin_pack, use_ofs_delta, use_include_tag; static int no_progress, daemon_mode; -static int allow_tip_sha1_in_want; +/* Allow specifying sha1 if it is a ref tip. */ +#define ALLOW_TIP_SHA1 01 +/* Allow request of a sha1 if it is reachable from a ref (possibly hidden ref). */ +#define ALLOW_REACHABLE_SHA1 02 +static unsigned int allow_unadvertised_object_request; static int shallow_nr; static struct object_array have_obj; static struct object_array want_obj; @@ -69,89 +74,38 @@ static ssize_t send_client_data(int fd, const char *data, ssize_t sz) return sz; } -static FILE *pack_pipe = NULL; -static void show_commit(struct commit *commit, void *data) +static int write_one_shallow(const struct commit_graft *graft, void *cb_data) { - if (commit->object.flags & BOUNDARY) - fputc('-', pack_pipe); - if (fputs(sha1_to_hex(commit->object.sha1), pack_pipe) < 0) - die("broken output pipe"); - fputc('\n', pack_pipe); - fflush(pack_pipe); - free(commit->buffer); - commit->buffer = NULL; -} - -static void show_object(struct object *obj, - const struct name_path *path, const char *component, - void *cb_data) -{ - show_object_with_name(pack_pipe, obj, path, component); -} - -static void show_edge(struct commit *commit) -{ - fprintf(pack_pipe, "-%s\n", sha1_to_hex(commit->object.sha1)); -} - -static int do_rev_list(int in, int out, void *user_data) -{ - int i; - struct rev_info revs; - - pack_pipe = xfdopen(out, "w"); - init_revisions(&revs, NULL); - revs.tag_objects = 1; - revs.tree_objects = 1; - revs.blob_objects = 1; - if (use_thin_pack) - revs.edge_hint = 1; - - for (i = 0; i < want_obj.nr; i++) { - struct object *o = want_obj.objects[i].item; - /* why??? */ - o->flags &= ~UNINTERESTING; - add_pending_object(&revs, o, NULL); - } - for (i = 0; i < have_obj.nr; i++) { - struct object *o = have_obj.objects[i].item; - o->flags |= UNINTERESTING; - add_pending_object(&revs, o, NULL); - } - setup_revisions(0, NULL, &revs, NULL); - if (prepare_revision_walk(&revs)) - die("revision walk setup failed"); - mark_edges_uninteresting(revs.commits, &revs, show_edge); - if (use_thin_pack) - for (i = 0; i < extra_edge_obj.nr; i++) - fprintf(pack_pipe, "-%s\n", sha1_to_hex( - extra_edge_obj.objects[i].item->sha1)); - traverse_commit_list(&revs, show_commit, show_object, NULL); - fflush(pack_pipe); - fclose(pack_pipe); + FILE *fp = cb_data; + if (graft->nr_parent == -1) + fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid)); return 0; } static void create_pack_file(void) { - struct async rev_list; - struct child_process pack_objects; + struct child_process pack_objects = CHILD_PROCESS_INIT; char data[8193], progress[128]; char abort_msg[] = "aborting due to possible repository " "corruption on the remote side."; int buffered = -1; ssize_t sz; - const char *argv[10]; - int arg = 0; + const char *argv[13]; + int i, arg = 0; + FILE *pipe_fd; - argv[arg++] = "pack-objects"; - if (!shallow_nr) { - argv[arg++] = "--revs"; - if (use_thin_pack) - argv[arg++] = "--thin"; + if (shallow_nr) { + argv[arg++] = "--shallow-file"; + argv[arg++] = ""; } + argv[arg++] = "pack-objects"; + argv[arg++] = "--revs"; + if (use_thin_pack) + argv[arg++] = "--thin"; argv[arg++] = "--stdout"; + if (shallow_nr) + argv[arg++] = "--shallow"; if (!no_progress) argv[arg++] = "--progress"; if (use_ofs_delta) @@ -160,7 +114,6 @@ static void create_pack_file(void) argv[arg++] = "--include-tag"; argv[arg++] = NULL; - memset(&pack_objects, 0, sizeof(pack_objects)); pack_objects.in = -1; pack_objects.out = -1; pack_objects.err = -1; @@ -170,29 +123,24 @@ static void create_pack_file(void) if (start_command(&pack_objects)) die("git upload-pack: unable to fork git-pack-objects"); - if (shallow_nr) { - memset(&rev_list, 0, sizeof(rev_list)); - rev_list.proc = do_rev_list; - rev_list.out = pack_objects.in; - if (start_async(&rev_list)) - die("git upload-pack: unable to fork git-rev-list"); - } - else { - FILE *pipe_fd = xfdopen(pack_objects.in, "w"); - int i; - - for (i = 0; i < want_obj.nr; i++) - fprintf(pipe_fd, "%s\n", - sha1_to_hex(want_obj.objects[i].item->sha1)); - fprintf(pipe_fd, "--not\n"); - for (i = 0; i < have_obj.nr; i++) - fprintf(pipe_fd, "%s\n", - sha1_to_hex(have_obj.objects[i].item->sha1)); - fprintf(pipe_fd, "\n"); - fflush(pipe_fd); - fclose(pipe_fd); - } - + pipe_fd = xfdopen(pack_objects.in, "w"); + + if (shallow_nr) + for_each_commit_graft(write_one_shallow, pipe_fd); + + for (i = 0; i < want_obj.nr; i++) + fprintf(pipe_fd, "%s\n", + sha1_to_hex(want_obj.objects[i].item->sha1)); + fprintf(pipe_fd, "--not\n"); + for (i = 0; i < have_obj.nr; i++) + fprintf(pipe_fd, "%s\n", + sha1_to_hex(have_obj.objects[i].item->sha1)); + for (i = 0; i < extra_edge_obj.nr; i++) + fprintf(pipe_fd, "%s\n", + sha1_to_hex(extra_edge_obj.objects[i].item->sha1)); + fprintf(pipe_fd, "\n"); + fflush(pipe_fd); + fclose(pipe_fd); /* We read from pack_objects.err to capture stderr output for * progress bar, and pack_objects.out to capture the pack data. @@ -224,7 +172,9 @@ static void create_pack_file(void) if (!pollsize) break; - ret = poll(pfd, pollsize, 1000 * keepalive); + ret = poll(pfd, pollsize, + keepalive < 0 ? -1 : 1000 * keepalive); + if (ret < 0) { if (errno != EINTR) { error("poll failed, resuming: %s", @@ -308,8 +258,6 @@ static void create_pack_file(void) error("git upload-pack: git-pack-objects died with error."); goto fail; } - if (shallow_nr && finish_async(&rev_list)) - goto fail; /* error was already reported */ /* flush the data */ if (0 <= buffered) { @@ -457,7 +405,7 @@ static int get_common_commits(void) got_other = 0; continue; } - if (!prefixcmp(line, "have ")) { + if (starts_with(line, "have ")) { switch (got_sha1(line+5, sha1)) { case -1: /* they have what we do not */ got_other = 1; @@ -498,8 +446,9 @@ static int get_common_commits(void) static int is_our_ref(struct object *o) { - return o->flags & - ((allow_tip_sha1_in_want ? HIDDEN_REF : 0) | OUR_REF); + int allow_hidden_ref = (allow_unadvertised_object_request & + (ALLOW_TIP_SHA1 | ALLOW_REACHABLE_SHA1)); + return o->flags & ((allow_hidden_ref ? HIDDEN_REF : 0) | OUR_REF); } static void check_non_tip(void) @@ -507,13 +456,17 @@ static void check_non_tip(void) static const char *argv[] = { "rev-list", "--stdin", NULL, }; - static struct child_process cmd; + static struct child_process cmd = CHILD_PROCESS_INIT; 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) + /* + * In the normal in-process case without + * uploadpack.allowReachableSHA1InWant, + * non-tip requests can never happen. + */ + if (!stateless_rpc && !(allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1)) goto error; cmd.argv = argv; @@ -603,7 +556,7 @@ static void receive_needs(void) if (!line) break; - if (!prefixcmp(line, "shallow ")) { + if (starts_with(line, "shallow ")) { unsigned char sha1[20]; struct object *object; if (get_sha1_hex(line + 8, sha1)) @@ -619,14 +572,14 @@ static void receive_needs(void) } continue; } - if (!prefixcmp(line, "deepen ")) { + if (starts_with(line, "deepen ")) { char *end; depth = strtol(line + 7, &end, 0); if (end == line + 7 || depth <= 0) die("Invalid deepen: %s", line); continue; } - if (prefixcmp(line, "want ") || + if (!starts_with(line, "want ") || get_sha1_hex(line+5, sha1_buf)) die("git upload-pack: protocol error, " "expected to get sha, not '%s'", line); @@ -682,7 +635,7 @@ static void receive_needs(void) if (depth > 0) { struct commit_list *result = NULL, *backup = NULL; int i; - if (depth == INFINITE_DEPTH) + if (depth == INFINITE_DEPTH && !is_repository_shallow()) for (i = 0; i < shallows.nr; i++) { struct object *object = shallows.objects[i].item; object->flags |= NOT_SHALLOW; @@ -712,8 +665,7 @@ static void receive_needs(void) /* make sure the real parents are parsed */ unregister_shallow(object->sha1); object->parsed = 0; - if (parse_commit((struct commit *)object)) - die("invalid commit"); + parse_commit_or_die((struct commit *)object); parents = ((struct commit *)object)->parents; while (parents) { add_object_array(&parents->item->object, @@ -738,57 +690,105 @@ static void receive_needs(void) } /* return non-zero if the ref is hidden, otherwise 0 */ -static int mark_our_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +static int mark_our_ref(const char *refname, const struct object_id *oid) { - struct object *o = lookup_unknown_object(sha1); + struct object *o = lookup_unknown_object(oid->hash); if (ref_is_hidden(refname)) { o->flags |= HIDDEN_REF; return 1; } - if (!o) - die("git upload-pack: cannot find object %s:", sha1_to_hex(sha1)); o->flags |= OUR_REF; return 0; } -static int send_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) +static int check_ref(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + mark_our_ref(refname, oid); + return 0; +} + +static void format_symref_info(struct strbuf *buf, struct string_list *symref) +{ + struct string_list_item *item; + + if (!symref->nr) + return; + for_each_string_list_item(item, symref) + strbuf_addf(buf, " symref=%s:%s", item->string, (char *)item->util); +} + +static int send_ref(const char *refname, const struct object_id *oid, + int flag, void *cb_data) { static const char *capabilities = "multi_ack thin-pack side-band" " side-band-64k ofs-delta shallow no-progress" " include-tag multi_ack_detailed"; const char *refname_nons = strip_namespace(refname); - unsigned char peeled[20]; + struct object_id peeled; - if (mark_our_ref(refname, sha1, flag, cb_data)) + if (mark_our_ref(refname, oid)) return 0; - if (capabilities) - packet_write(1, "%s %s%c%s%s%s agent=%s\n", - sha1_to_hex(sha1), refname_nons, + if (capabilities) { + struct strbuf symref_info = STRBUF_INIT; + + format_symref_info(&symref_info, cb_data); + packet_write(1, "%s %s%c%s%s%s%s%s agent=%s\n", + oid_to_hex(oid), refname_nons, 0, capabilities, - allow_tip_sha1_in_want ? " allow-tip-sha1-in-want" : "", + (allow_unadvertised_object_request & ALLOW_TIP_SHA1) ? + " allow-tip-sha1-in-want" : "", + (allow_unadvertised_object_request & ALLOW_REACHABLE_SHA1) ? + " allow-reachable-sha1-in-want" : "", stateless_rpc ? " no-done" : "", + symref_info.buf, git_user_agent_sanitized()); - else - packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname_nons); + strbuf_release(&symref_info); + } else { + packet_write(1, "%s %s\n", oid_to_hex(oid), refname_nons); + } capabilities = NULL; - if (!peel_ref(refname, peeled)) - packet_write(1, "%s %s^{}\n", sha1_to_hex(peeled), refname_nons); + if (!peel_ref(refname, peeled.hash)) + packet_write(1, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); + return 0; +} + +static int find_symref(const char *refname, const struct object_id *oid, + int flag, void *cb_data) +{ + const char *symref_target; + struct string_list_item *item; + struct object_id unused; + + if ((flag & REF_ISSYMREF) == 0) + return 0; + symref_target = resolve_ref_unsafe(refname, 0, unused.hash, &flag); + if (!symref_target || (flag & REF_ISSYMREF) == 0) + die("'%s' is a symref but it is not?", refname); + item = string_list_append(cb_data, refname); + item->util = xstrdup(symref_target); return 0; } static void upload_pack(void) { + struct string_list symref = STRING_LIST_INIT_DUP; + + head_ref_namespaced(find_symref, &symref); + if (advertise_refs || !stateless_rpc) { reset_timeout(); - head_ref_namespaced(send_ref, NULL); - for_each_namespaced_ref(send_ref, NULL); + head_ref_namespaced(send_ref, &symref); + for_each_namespaced_ref(send_ref, &symref); + advertise_shallow_grafts(1); packet_flush(1); } else { - head_ref_namespaced(mark_our_ref, NULL); - for_each_namespaced_ref(mark_our_ref, NULL); + head_ref_namespaced(check_ref, NULL); + for_each_namespaced_ref(check_ref, NULL); } + string_list_clear(&symref, 1); if (advertise_refs) return; @@ -801,9 +801,17 @@ static void upload_pack(void) static int upload_pack_config(const char *var, const char *value, void *unused) { - if (!strcmp("uploadpack.allowtipsha1inwant", var)) - allow_tip_sha1_in_want = git_config_bool(var, value); - else if (!strcmp("uploadpack.keepalive", var)) { + if (!strcmp("uploadpack.allowtipsha1inwant", var)) { + if (git_config_bool(var, value)) + allow_unadvertised_object_request |= ALLOW_TIP_SHA1; + else + allow_unadvertised_object_request &= ~ALLOW_TIP_SHA1; + } else if (!strcmp("uploadpack.allowreachablesha1inwant", var)) { + if (git_config_bool(var, value)) + allow_unadvertised_object_request |= ALLOW_REACHABLE_SHA1; + else + allow_unadvertised_object_request &= ~ALLOW_REACHABLE_SHA1; + } else if (!strcmp("uploadpack.keepalive", var)) { keepalive = git_config_int(var, value); if (!keepalive) keepalive = -1; @@ -821,7 +829,7 @@ int main(int argc, char **argv) packet_trace_identity("upload-pack"); git_extract_argv0_path(argv[0]); - read_replace_refs = 0; + check_replace_refs = 0; for (i = 1; i < argc; i++) { char *arg = argv[i]; @@ -840,7 +848,7 @@ int main(int argc, char **argv) strict = 1; continue; } - if (!prefixcmp(arg, "--timeout=")) { + if (starts_with(arg, "--timeout=")) { timeout = atoi(arg+10); daemon_mode = 1; continue; @@ -860,8 +868,7 @@ int main(int argc, char **argv) if (!enter_repo(dir, strict)) die("'%s' does not appear to be a git repository", dir); - if (is_repository_shallow()) - die("attempt to fetch/clone from a shallow repository"); + git_config(upload_pack_config, NULL); upload_pack(); return 0; |