From 4577370e9bfeca8652880b99b8499f76d18865ba Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 21:05:40 -0400 Subject: [PATCH 01/24] Miscellaneous const changes and utilities The list of remote refs in struct transport should be const, because builtin-fetch will get confused if it changes. The url in git_connect should be const (and work on a copy) instead of requiring the caller to copy it. match_refs doesn't modify the refspecs it gets. get_fetch_map and get_remote_ref don't change the list they get. Allow transport get_refs_list methods to modify the struct transport. Add a function to copy a list of refs, when a function needs a mutable copy of a const list. Add a function to check the type of a ref, as per the code in connect.c Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- builtin-fetch.c | 10 +++++----- cache.h | 2 +- connect.c | 10 +++++++++- http-push.c | 2 +- remote.c | 32 ++++++++++++++++++++++---------- remote.h | 10 +++++++--- send-pack.c | 8 ++++---- transport.c | 10 +++++----- transport.h | 6 +++--- 9 files changed, 57 insertions(+), 33 deletions(-) diff --git a/builtin-fetch.c b/builtin-fetch.c index 003ed76d16..fa0af170dd 100644 --- a/builtin-fetch.c +++ b/builtin-fetch.c @@ -29,7 +29,7 @@ static void unlock_pack_on_signal(int signo) } static void add_merge_config(struct ref **head, - struct ref *remote_refs, + const struct ref *remote_refs, struct branch *branch, struct ref ***tail) { @@ -77,7 +77,7 @@ static struct ref *get_ref_map(struct transport *transport, struct ref *ref_map = NULL; struct ref **tail = &ref_map; - struct ref *remote_refs = transport_get_remote_refs(transport); + const struct ref *remote_refs = transport_get_remote_refs(transport); if (ref_count || tags) { for (i = 0; i < ref_count; i++) { @@ -345,12 +345,12 @@ static struct ref *find_non_local_tags(struct transport *transport, struct path_list new_refs = { NULL, 0, 0, 1 }; char *ref_name; int ref_name_len; - unsigned char *ref_sha1; - struct ref *tag_ref; + const unsigned char *ref_sha1; + const struct ref *tag_ref; struct ref *rm = NULL; struct ref *ref_map = NULL; struct ref **tail = &ref_map; - struct ref *ref; + const struct ref *ref; for_each_ref(add_existing, &existing_refs); for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) { diff --git a/cache.h b/cache.h index bfffa05dff..119566bf6f 100644 --- a/cache.h +++ b/cache.h @@ -503,7 +503,7 @@ struct ref { #define REF_TAGS (1u << 2) #define CONNECT_VERBOSE (1u << 0) -extern struct child_process *git_connect(int fd[2], char *url, const char *prog, int flags); +extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags); extern int finish_connect(struct child_process *conn); extern int path_match(const char *path, int nr, char **match); extern int get_ack(int fd, unsigned char *result_sha1); diff --git a/connect.c b/connect.c index 44e423dafd..3aefd4ace5 100644 --- a/connect.c +++ b/connect.c @@ -36,6 +36,11 @@ static int check_ref(const char *name, int len, unsigned int flags) return !(flags & ~REF_NORMAL); } +int check_ref_type(const struct ref *ref, int flags) +{ + return check_ref(ref->name, strlen(ref->name), flags); +} + /* * Read all the refs from the other end */ @@ -476,9 +481,10 @@ char *get_port(char *host) * * If it returns, the connect is successful; it just dies on errors. */ -struct child_process *git_connect(int fd[2], char *url, +struct child_process *git_connect(int fd[2], const char *url_orig, const char *prog, int flags) { + char *url = xstrdup(url_orig); char *host, *path = url; char *end; int c; @@ -568,6 +574,7 @@ struct child_process *git_connect(int fd[2], char *url, prog, path, 0, target_host, 0); free(target_host); + free(url); if (free_path) free(path); return NULL; @@ -619,6 +626,7 @@ struct child_process *git_connect(int fd[2], char *url, fd[0] = conn->out; /* read from child's stdout */ fd[1] = conn->in; /* write to child's stdin */ strbuf_release(&cmd); + free(url); if (free_path) free(path); return conn; diff --git a/http-push.c b/http-push.c index c02a3af634..f461bb3248 100644 --- a/http-push.c +++ b/http-push.c @@ -2389,7 +2389,7 @@ int main(int argc, char **argv) if (!remote_tail) remote_tail = &remote_refs; if (match_refs(local_refs, remote_refs, &remote_tail, - nr_refspec, refspec, push_all)) + nr_refspec, (const char **) refspec, push_all)) return -1; if (!remote_refs) { fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); diff --git a/remote.c b/remote.c index bec2ba1adb..59defdbf14 100644 --- a/remote.c +++ b/remote.c @@ -485,7 +485,7 @@ struct ref *alloc_ref(unsigned namelen) return ret; } -static struct ref *copy_ref(struct ref *ref) +static struct ref *copy_ref(const struct ref *ref) { struct ref *ret = xmalloc(sizeof(struct ref) + strlen(ref->name) + 1); memcpy(ret, ref, sizeof(struct ref) + strlen(ref->name) + 1); @@ -493,6 +493,18 @@ static struct ref *copy_ref(struct ref *ref) return ret; } +struct ref *copy_ref_list(const struct ref *ref) +{ + struct ref *ret = NULL; + struct ref **tail = &ret; + while (ref) { + *tail = copy_ref(ref); + ref = ref->next; + tail = &((*tail)->next); + } + return ret; +} + void free_refs(struct ref *ref) { struct ref *next; @@ -710,7 +722,7 @@ static const struct refspec *check_pattern_match(const struct refspec *rs, * without thinking. */ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, - int nr_refspec, char **refspec, int all) + int nr_refspec, const char **refspec, int all) { struct refspec *rs = parse_ref_spec(nr_refspec, (const char **) refspec); @@ -810,10 +822,10 @@ int branch_merge_matches(struct branch *branch, return ref_matches_abbrev(branch->merge[i]->src, refname); } -static struct ref *get_expanded_map(struct ref *remote_refs, +static struct ref *get_expanded_map(const struct ref *remote_refs, const struct refspec *refspec) { - struct ref *ref; + const struct ref *ref; struct ref *ret = NULL; struct ref **tail = &ret; @@ -824,7 +836,7 @@ static struct ref *get_expanded_map(struct ref *remote_refs, if (strchr(ref->name, '^')) continue; /* a dereference item */ if (!prefixcmp(ref->name, refspec->src)) { - char *match; + const char *match; struct ref *cpy = copy_ref(ref); match = ref->name + remote_prefix_len; @@ -842,9 +854,9 @@ static struct ref *get_expanded_map(struct ref *remote_refs, return ret; } -static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name) +static const struct ref *find_ref_by_name_abbrev(const struct ref *refs, const char *name) { - struct ref *ref; + const struct ref *ref; for (ref = refs; ref; ref = ref->next) { if (ref_matches_abbrev(name, ref->name)) return ref; @@ -852,9 +864,9 @@ static struct ref *find_ref_by_name_abbrev(struct ref *refs, const char *name) return NULL; } -struct ref *get_remote_ref(struct ref *remote_refs, const char *name) +struct ref *get_remote_ref(const struct ref *remote_refs, const char *name) { - struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); + const struct ref *ref = find_ref_by_name_abbrev(remote_refs, name); if (!ref) return NULL; @@ -887,7 +899,7 @@ static struct ref *get_local_ref(const char *name) return ret; } -int get_fetch_map(struct ref *remote_refs, +int get_fetch_map(const struct ref *remote_refs, const struct refspec *refspec, struct ref ***tail, int missing_ok) diff --git a/remote.h b/remote.h index 878b4ecc32..6a4c7a0f37 100644 --- a/remote.h +++ b/remote.h @@ -44,6 +44,10 @@ struct refspec { struct ref *alloc_ref(unsigned namelen); +struct ref *copy_ref_list(const struct ref *ref); + +int check_ref_type(const struct ref *ref, int flags); + /* * Frees the entire list and peers of elements. */ @@ -57,7 +61,7 @@ void ref_remove_duplicates(struct ref *ref_map); struct refspec *parse_ref_spec(int nr_refspec, const char **refspec); int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, - int nr_refspec, char **refspec, int all); + int nr_refspec, const char **refspec, int all); /* * Given a list of the remote refs and the specification of things to @@ -71,10 +75,10 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, * missing_ok is usually false, but when we are adding branch.$name.merge * it is Ok if the branch is not at the remote anymore. */ -int get_fetch_map(struct ref *remote_refs, const struct refspec *refspec, +int get_fetch_map(const struct ref *remote_refs, const struct refspec *refspec, struct ref ***tail, int missing_ok); -struct ref *get_remote_ref(struct ref *remote_refs, const char *name); +struct ref *get_remote_ref(const struct ref *remote_refs, const char *name); /* * For the given remote, reads the refspec's src and sets the other fields. diff --git a/send-pack.c b/send-pack.c index 5e127a1b7b..456fe4f125 100644 --- a/send-pack.c +++ b/send-pack.c @@ -207,7 +207,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref) } } -static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec) +static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec) { struct ref *ref; int new_refs; @@ -357,7 +357,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha return ret; } -static void verify_remote_names(int nr_heads, char **heads) +static void verify_remote_names(int nr_heads, const char **heads) { int i; @@ -382,7 +382,7 @@ int main(int argc, char **argv) { int i, nr_heads = 0; char *dest = NULL; - char **heads = NULL; + const char **heads = NULL; int fd[2], ret; struct child_process *conn; char *remote_name = NULL; @@ -434,7 +434,7 @@ int main(int argc, char **argv) dest = arg; continue; } - heads = argv; + heads = (const char **) argv; nr_heads = argc - i; break; } diff --git a/transport.c b/transport.c index d44fe7cee7..ab79f0cbb0 100644 --- a/transport.c +++ b/transport.c @@ -141,7 +141,7 @@ static void insert_packed_refs(const char *packed_refs, struct ref **list) } } -static struct ref *get_refs_via_rsync(const struct transport *transport) +static struct ref *get_refs_via_rsync(struct transport *transport) { struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; struct ref dummy, *tail = &dummy; @@ -427,7 +427,7 @@ static int missing__target(int code, int result) #define missing_target(a) missing__target((a)->http_code, (a)->curl_result) -static struct ref *get_refs_via_curl(const struct transport *transport) +static struct ref *get_refs_via_curl(struct transport *transport) { struct buffer buffer; char *data, *start, *mid; @@ -524,7 +524,7 @@ struct bundle_transport_data { struct bundle_header header; }; -static struct ref *get_refs_from_bundle(const struct transport *transport) +static struct ref *get_refs_from_bundle(struct transport *transport) { struct bundle_transport_data *data = transport->data; struct ref *result = NULL; @@ -596,7 +596,7 @@ static int set_git_option(struct transport *connection, return 1; } -static struct ref *get_refs_via_connect(const struct transport *transport) +static struct ref *get_refs_via_connect(struct transport *transport) { struct git_transport_data *data = transport->data; struct ref *refs; @@ -781,7 +781,7 @@ int transport_push(struct transport *transport, return transport->push(transport, refspec_nr, refspec, flags); } -struct ref *transport_get_remote_refs(struct transport *transport) +const struct ref *transport_get_remote_refs(struct transport *transport) { if (!transport->remote_refs) transport->remote_refs = transport->get_refs_list(transport); diff --git a/transport.h b/transport.h index df12ea7424..d27f5629d2 100644 --- a/transport.h +++ b/transport.h @@ -8,7 +8,7 @@ struct transport { struct remote *remote; const char *url; void *data; - struct ref *remote_refs; + const struct ref *remote_refs; /** * Returns 0 if successful, positive if the option is not @@ -18,7 +18,7 @@ struct transport { int (*set_option)(struct transport *connection, const char *name, const char *value); - struct ref *(*get_refs_list)(const struct transport *transport); + struct ref *(*get_refs_list)(struct transport *transport); int (*fetch)(struct transport *transport, int refs_nr, struct ref **refs); int (*push)(struct transport *connection, int refspec_nr, const char **refspec, int flags); @@ -61,7 +61,7 @@ int transport_set_option(struct transport *transport, const char *name, int transport_push(struct transport *connection, int refspec_nr, const char **refspec, int flags); -struct ref *transport_get_remote_refs(struct transport *transport); +const struct ref *transport_get_remote_refs(struct transport *transport); int transport_fetch_refs(struct transport *transport, struct ref *refs); void transport_unlock_pack(struct transport *transport); From 18f7c51cf921f7db021e012499456120f3f095a9 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 21:05:43 -0400 Subject: [PATCH 02/24] Build-in peek-remote, using transport infrastructure. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- Makefile | 3 +- peek-remote.c => builtin-peek-remote.c | 55 ++++++++++++-------------- builtin.h | 1 + git.c | 1 + 4 files changed, 30 insertions(+), 30 deletions(-) rename peek-remote.c => builtin-peek-remote.c (59%) diff --git a/Makefile b/Makefile index 042f79ef8f..14f25ff8ec 100644 --- a/Makefile +++ b/Makefile @@ -239,7 +239,7 @@ PROGRAMS = \ git-fast-import$X \ git-daemon$X \ git-merge-index$X git-mktag$X git-mktree$X git-patch-id$X \ - git-peek-remote$X git-receive-pack$X \ + git-receive-pack$X \ git-send-pack$X git-shell$X \ git-show-index$X \ git-unpack-file$X \ @@ -352,6 +352,7 @@ BUILTIN_OBJS = \ builtin-mv.o \ builtin-name-rev.o \ builtin-pack-objects.o \ + builtin-peek-remote.o \ builtin-prune.o \ builtin-prune-packed.o \ builtin-push.o \ diff --git a/peek-remote.c b/builtin-peek-remote.c similarity index 59% rename from peek-remote.c rename to builtin-peek-remote.c index 8d20f7c9c6..b4106f510a 100644 --- a/peek-remote.c +++ b/builtin-peek-remote.c @@ -1,38 +1,26 @@ +#include "builtin.h" #include "cache.h" -#include "refs.h" -#include "pkt-line.h" +#include "transport.h" +#include "remote.h" static const char peek_remote_usage[] = "git-peek-remote [--upload-pack=] [:]"; -static const char *uploadpack = "git-upload-pack"; -static int peek_remote(int fd[2], unsigned flags) +int cmd_peek_remote(int argc, const char **argv, const char *prefix) { - struct ref *ref; - - get_remote_heads(fd[0], &ref, 0, NULL, flags); - packet_flush(fd[1]); - - while (ref) { - printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name); - ref = ref->next; - } - return 0; -} - -int main(int argc, char **argv) -{ - int i, ret; - char *dest = NULL; - int fd[2]; - struct child_process *conn; + int i; + const char *dest = NULL; int nongit = 0; unsigned flags = 0; + const char *uploadpack = NULL; + + struct transport *transport; + const struct ref *ref; setup_git_directory_gently(&nongit); for (i = 1; i < argc; i++) { - char *arg = argv[i]; + const char *arg = argv[i]; if (*arg == '-') { if (!prefixcmp(arg, "--upload-pack=")) { @@ -64,10 +52,19 @@ int main(int argc, char **argv) if (!dest || i != argc - 1) usage(peek_remote_usage); - conn = git_connect(fd, dest, uploadpack, 0); - ret = peek_remote(fd, flags); - close(fd[0]); - close(fd[1]); - ret |= finish_connect(conn); - return !!ret; + transport = transport_get(NULL, dest); + if (uploadpack != NULL) + transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); + + ref = transport_get_remote_refs(transport); + + if (!ref) + return 1; + + while (ref) { + if (check_ref_type(ref, flags)) + printf("%s %s\n", sha1_to_hex(ref->old_sha1), ref->name); + ref = ref->next; + } + return 0; } diff --git a/builtin.h b/builtin.h index 9a6213af12..62ee5373dc 100644 --- a/builtin.h +++ b/builtin.h @@ -55,6 +55,7 @@ extern int cmd_merge_file(int argc, const char **argv, const char *prefix); extern int cmd_mv(int argc, const char **argv, const char *prefix); extern int cmd_name_rev(int argc, const char **argv, const char *prefix); extern int cmd_pack_objects(int argc, const char **argv, const char *prefix); +extern int cmd_peek_remote(int argc, const char **argv, const char *prefix); extern int cmd_pickaxe(int argc, const char **argv, const char *prefix); extern int cmd_prune(int argc, const char **argv, const char *prefix); extern int cmd_prune_packed(int argc, const char **argv, const char *prefix); diff --git a/git.c b/git.c index 4e10581101..c55a13de17 100644 --- a/git.c +++ b/git.c @@ -333,6 +333,7 @@ static void handle_internal_command(int argc, const char **argv) { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, { "name-rev", cmd_name_rev, RUN_SETUP }, { "pack-objects", cmd_pack_objects, RUN_SETUP }, + { "peek-remote", cmd_peek_remote }, { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, { "prune-packed", cmd_prune_packed, RUN_SETUP }, From 40cb4fab720807f8525fb125e6c4ddc802ee8ed1 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 22:03:42 -0400 Subject: [PATCH 03/24] Use built-in send-pack. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- transport.c | 51 ++++++++++----------------------------------------- 1 file changed, 10 insertions(+), 41 deletions(-) diff --git a/transport.c b/transport.c index ab79f0cbb0..f4577b7fc6 100644 --- a/transport.c +++ b/transport.c @@ -6,6 +6,7 @@ #endif #include "pkt-line.h" #include "fetch-pack.h" +#include "send-pack.h" #include "walker.h" #include "bundle.h" #include "dir.h" @@ -648,48 +649,16 @@ static int fetch_refs_via_pack(struct transport *transport, static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags) { struct git_transport_data *data = transport->data; - const char **argv; - char *rem; - int argc; - int err; + struct send_pack_args args; - argv = xmalloc((refspec_nr + 11) * sizeof(char *)); - argv[0] = "send-pack"; - argc = 1; - if (flags & TRANSPORT_PUSH_ALL) - argv[argc++] = "--all"; - if (flags & TRANSPORT_PUSH_FORCE) - argv[argc++] = "--force"; - if (flags & TRANSPORT_PUSH_DRY_RUN) - argv[argc++] = "--dry-run"; - if (data->receivepack) { - char *rp = xmalloc(strlen(data->receivepack) + 16); - sprintf(rp, "--receive-pack=%s", data->receivepack); - argv[argc++] = rp; - } - if (data->thin) - argv[argc++] = "--thin"; - rem = xmalloc(strlen(transport->remote->name) + 10); - sprintf(rem, "--remote=%s", transport->remote->name); - argv[argc++] = rem; - argv[argc++] = transport->url; - while (refspec_nr--) - argv[argc++] = *refspec++; - argv[argc] = NULL; - err = run_command_v_opt(argv, RUN_GIT_CMD); - switch (err) { - case -ERR_RUN_COMMAND_FORK: - error("unable to fork for %s", argv[0]); - case -ERR_RUN_COMMAND_EXEC: - error("unable to exec %s", argv[0]); - break; - case -ERR_RUN_COMMAND_WAITPID: - case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: - case -ERR_RUN_COMMAND_WAITPID_SIGNAL: - case -ERR_RUN_COMMAND_WAITPID_NOEXIT: - error("%s died with strange error", argv[0]); - } - return !!err; + args.receivepack = data->receivepack; + args.send_all = !!(flags & TRANSPORT_PUSH_ALL); + args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); + args.use_thin_pack = data->thin; + args.verbose = transport->verbose; + args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); + + return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec); } static int disconnect_git(struct transport *transport) From 96249c04c07f00485a3ed1052ff10b27ab147fc5 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 22:03:39 -0400 Subject: [PATCH 04/24] Build-in send-pack, with an API for other programs to call. Also marks some more things as const, as needed. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- Makefile | 1 + send-pack.c => builtin-send-pack.c | 81 ++++++++++++++++-------------- builtin.h | 1 + git.c | 1 + send-pack.h | 17 +++++++ 5 files changed, 64 insertions(+), 37 deletions(-) rename send-pack.c => builtin-send-pack.c (88%) create mode 100644 send-pack.h diff --git a/Makefile b/Makefile index 14f25ff8ec..3ec1876cbb 100644 --- a/Makefile +++ b/Makefile @@ -358,6 +358,7 @@ BUILTIN_OBJS = \ builtin-push.o \ builtin-read-tree.o \ builtin-reflog.o \ + builtin-send-pack.o \ builtin-config.o \ builtin-rerere.o \ builtin-reset.o \ diff --git a/send-pack.c b/builtin-send-pack.c similarity index 88% rename from send-pack.c rename to builtin-send-pack.c index 456fe4f125..aedef09ef0 100644 --- a/send-pack.c +++ b/builtin-send-pack.c @@ -5,16 +5,15 @@ #include "pkt-line.h" #include "run-command.h" #include "remote.h" +#include "send-pack.h" static const char send_pack_usage[] = "git-send-pack [--all] [--dry-run] [--force] [--receive-pack=] [--verbose] [--thin] [:] [...]\n" " --all and explicit specification are mutually exclusive."; -static const char *receivepack = "git-receive-pack"; -static int verbose; -static int send_all; -static int force_update; -static int use_thin_pack; -static int dry_run; + +static struct send_pack_args args = { + /* .receivepack = */ "git-receive-pack", +}; /* * Make a pack stream and spit it out into file descriptor fd @@ -26,7 +25,7 @@ static int pack_objects(int fd, struct ref *refs) * the revision parameters to it via its stdin and * let its stdout go back to the other end. */ - const char *args[] = { + const char *argv[] = { "pack-objects", "--all-progress", "--revs", @@ -36,10 +35,10 @@ static int pack_objects(int fd, struct ref *refs) }; struct child_process po; - if (use_thin_pack) - args[4] = "--thin"; + if (args.use_thin_pack) + argv[4] = "--thin"; memset(&po, 0, sizeof(po)); - po.argv = args; + po.argv = argv; po.in = -1; po.out = fd; po.git_cmd = 1; @@ -207,7 +206,7 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref) } } -static int send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec) +static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec) { struct ref *ref; int new_refs; @@ -230,7 +229,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con if (!remote_tail) remote_tail = &remote_refs; if (match_refs(local_refs, remote_refs, &remote_tail, - nr_refspec, refspec, send_all)) + nr_refspec, refspec, args.send_all)) return -1; if (!remote_refs) { @@ -259,7 +258,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con } if (!will_delete_ref && !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { - if (verbose) + if (args.verbose) fprintf(stderr, "'%s': up-to-date\n", ref->name); continue; } @@ -283,7 +282,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con * always allowed. */ - if (!force_update && + if (!args.force_update && !will_delete_ref && !is_null_sha1(ref->old_sha1) && !ref->force) { @@ -313,7 +312,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con strcpy(old_hex, sha1_to_hex(ref->old_sha1)); new_hex = sha1_to_hex(ref->new_sha1); - if (!dry_run) { + if (!args.dry_run) { if (ask_for_status_report) { packet_write(out, "%s %s %s%c%s", old_hex, new_hex, ref->name, 0, @@ -338,7 +337,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con } packet_flush(out); - if (new_refs && !dry_run) + if (new_refs && !args.dry_run) ret = pack_objects(out, remote_refs); close(out); @@ -347,7 +346,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, con ret = -4; } - if (!dry_run && remote && ret == 0) { + if (!args.dry_run && remote && ret == 0) { for (ref = remote_refs; ref; ref = ref->next) update_tracking_ref(remote, ref); } @@ -378,30 +377,25 @@ static void verify_remote_names(int nr_heads, const char **heads) } } -int main(int argc, char **argv) +int cmd_send_pack(int argc, const char **argv, const char *prefix) { int i, nr_heads = 0; - char *dest = NULL; const char **heads = NULL; - int fd[2], ret; - struct child_process *conn; - char *remote_name = NULL; + const char *remote_name = NULL; struct remote *remote = NULL; - - setup_git_directory(); - git_config(git_default_config); + const char *dest = NULL; argv++; for (i = 1; i < argc; i++, argv++) { - char *arg = *argv; + const char *arg = *argv; if (*arg == '-') { if (!prefixcmp(arg, "--receive-pack=")) { - receivepack = arg + 15; + args.receivepack = arg + 15; continue; } if (!prefixcmp(arg, "--exec=")) { - receivepack = arg + 7; + args.receivepack = arg + 7; continue; } if (!prefixcmp(arg, "--remote=")) { @@ -409,23 +403,23 @@ int main(int argc, char **argv) continue; } if (!strcmp(arg, "--all")) { - send_all = 1; + args.send_all = 1; continue; } if (!strcmp(arg, "--dry-run")) { - dry_run = 1; + args.dry_run = 1; continue; } if (!strcmp(arg, "--force")) { - force_update = 1; + args.force_update = 1; continue; } if (!strcmp(arg, "--verbose")) { - verbose = 1; + args.verbose = 1; continue; } if (!strcmp(arg, "--thin")) { - use_thin_pack = 1; + args.use_thin_pack = 1; continue; } usage(send_pack_usage); @@ -440,9 +434,8 @@ int main(int argc, char **argv) } if (!dest) usage(send_pack_usage); - if (heads && send_all) + if (heads && args.send_all) usage(send_pack_usage); - verify_remote_names(nr_heads, heads); if (remote_name) { remote = remote_get(remote_name); @@ -452,8 +445,22 @@ int main(int argc, char **argv) } } - conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); - ret = send_pack(fd[0], fd[1], remote, nr_heads, heads); + return send_pack(&args, dest, remote, nr_heads, heads); +} + +int send_pack(struct send_pack_args *my_args, + const char *dest, struct remote *remote, + int nr_heads, const char **heads) +{ + int fd[2], ret; + struct child_process *conn; + + memcpy(&args, my_args, sizeof(args)); + + verify_remote_names(nr_heads, heads); + + conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0); + ret = do_send_pack(fd[0], fd[1], remote, nr_heads, heads); close(fd[0]); close(fd[1]); ret |= finish_connect(conn); diff --git a/builtin.h b/builtin.h index 62ee5373dc..2335c01b02 100644 --- a/builtin.h +++ b/builtin.h @@ -70,6 +70,7 @@ extern int cmd_rev_parse(int argc, const char **argv, const char *prefix); extern int cmd_revert(int argc, const char **argv, const char *prefix); extern int cmd_rm(int argc, const char **argv, const char *prefix); extern int cmd_runstatus(int argc, const char **argv, const char *prefix); +extern int cmd_send_pack(int argc, const char **argv, const char *prefix); extern int cmd_shortlog(int argc, const char **argv, const char *prefix); extern int cmd_show(int argc, const char **argv, const char *prefix); extern int cmd_show_branch(int argc, const char **argv, const char *prefix); diff --git a/git.c b/git.c index c55a13de17..19a2172a10 100644 --- a/git.c +++ b/git.c @@ -348,6 +348,7 @@ static void handle_internal_command(int argc, const char **argv) { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, { "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE }, { "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE }, + { "send-pack", cmd_send_pack, RUN_SETUP }, { "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER }, { "show-branch", cmd_show_branch, RUN_SETUP }, { "show", cmd_show, RUN_SETUP | USE_PAGER }, diff --git a/send-pack.h b/send-pack.h new file mode 100644 index 0000000000..7a24f71c77 --- /dev/null +++ b/send-pack.h @@ -0,0 +1,17 @@ +#ifndef SEND_PACK_H +#define SEND_PACK_H + +struct send_pack_args { + const char *receivepack; + unsigned verbose:1, + send_all:1, + force_update:1, + use_thin_pack:1, + dry_run:1; +}; + +int send_pack(struct send_pack_args *args, + const char *dest, struct remote *remote, + int nr_heads, const char **heads); + +#endif From 8951d7c1f1ae38f34617b6c2490bf65e73e371f7 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sun, 4 Nov 2007 15:51:17 -0500 Subject: [PATCH 05/24] Build in ls-remote This actually replaces peek-remote with ls-remote, since peek-remote now handles everything. peek-remote remains an a second name for ls-remote, although its help message now gives the "ls-remote" name. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- Makefile | 3 +-- builtin-peek-remote.c => builtin-ls-remote.c | 10 +++++----- builtin.h | 2 +- git-ls-remote.sh => contrib/examples/git-ls-remote.sh | 0 git.c | 3 ++- 5 files changed, 9 insertions(+), 9 deletions(-) rename builtin-peek-remote.c => builtin-ls-remote.c (83%) rename git-ls-remote.sh => contrib/examples/git-ls-remote.sh (100%) diff --git a/Makefile b/Makefile index 3ec1876cbb..470e54a60d 100644 --- a/Makefile +++ b/Makefile @@ -210,7 +210,6 @@ BASIC_LDFLAGS = SCRIPT_SH = \ git-bisect.sh git-checkout.sh \ git-clean.sh git-clone.sh git-commit.sh \ - git-ls-remote.sh \ git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \ git-pull.sh git-rebase.sh git-rebase--interactive.sh \ git-repack.sh git-request-pull.sh \ @@ -345,6 +344,7 @@ BUILTIN_OBJS = \ builtin-log.o \ builtin-ls-files.o \ builtin-ls-tree.o \ + builtin-ls-remote.o \ builtin-mailinfo.o \ builtin-mailsplit.o \ builtin-merge-base.o \ @@ -352,7 +352,6 @@ BUILTIN_OBJS = \ builtin-mv.o \ builtin-name-rev.o \ builtin-pack-objects.o \ - builtin-peek-remote.o \ builtin-prune.o \ builtin-prune-packed.o \ builtin-push.o \ diff --git a/builtin-peek-remote.c b/builtin-ls-remote.c similarity index 83% rename from builtin-peek-remote.c rename to builtin-ls-remote.c index b4106f510a..003580c26a 100644 --- a/builtin-peek-remote.c +++ b/builtin-ls-remote.c @@ -3,10 +3,10 @@ #include "transport.h" #include "remote.h" -static const char peek_remote_usage[] = -"git-peek-remote [--upload-pack=] [:]"; +static const char ls_remote_usage[] = +"git-ls-remote [--upload-pack=] [:]"; -int cmd_peek_remote(int argc, const char **argv, const char *prefix) +int cmd_ls_remote(int argc, const char **argv, const char *prefix) { int i; const char *dest = NULL; @@ -43,14 +43,14 @@ int cmd_peek_remote(int argc, const char **argv, const char *prefix) flags |= REF_NORMAL; continue; } - usage(peek_remote_usage); + usage(ls_remote_usage); } dest = arg; break; } if (!dest || i != argc - 1) - usage(peek_remote_usage); + usage(ls_remote_usage); transport = transport_get(NULL, dest); if (uploadpack != NULL) diff --git a/builtin.h b/builtin.h index 2335c01b02..525107f385 100644 --- a/builtin.h +++ b/builtin.h @@ -48,6 +48,7 @@ extern int cmd_log(int argc, const char **argv, const char *prefix); extern int cmd_log_reflog(int argc, const char **argv, const char *prefix); extern int cmd_ls_files(int argc, const char **argv, const char *prefix); extern int cmd_ls_tree(int argc, const char **argv, const char *prefix); +extern int cmd_ls_remote(int argc, const char **argv, const char *prefix); extern int cmd_mailinfo(int argc, const char **argv, const char *prefix); extern int cmd_mailsplit(int argc, const char **argv, const char *prefix); extern int cmd_merge_base(int argc, const char **argv, const char *prefix); @@ -55,7 +56,6 @@ extern int cmd_merge_file(int argc, const char **argv, const char *prefix); extern int cmd_mv(int argc, const char **argv, const char *prefix); extern int cmd_name_rev(int argc, const char **argv, const char *prefix); extern int cmd_pack_objects(int argc, const char **argv, const char *prefix); -extern int cmd_peek_remote(int argc, const char **argv, const char *prefix); extern int cmd_pickaxe(int argc, const char **argv, const char *prefix); extern int cmd_prune(int argc, const char **argv, const char *prefix); extern int cmd_prune_packed(int argc, const char **argv, const char *prefix); diff --git a/git-ls-remote.sh b/contrib/examples/git-ls-remote.sh similarity index 100% rename from git-ls-remote.sh rename to contrib/examples/git-ls-remote.sh diff --git a/git.c b/git.c index 19a2172a10..b173f227f0 100644 --- a/git.c +++ b/git.c @@ -326,6 +326,7 @@ static void handle_internal_command(int argc, const char **argv) { "log", cmd_log, RUN_SETUP | USE_PAGER }, { "ls-files", cmd_ls_files, RUN_SETUP }, { "ls-tree", cmd_ls_tree, RUN_SETUP }, + { "ls-remote", cmd_ls_remote }, { "mailinfo", cmd_mailinfo }, { "mailsplit", cmd_mailsplit }, { "merge-base", cmd_merge_base, RUN_SETUP }, @@ -333,7 +334,7 @@ static void handle_internal_command(int argc, const char **argv) { "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE }, { "name-rev", cmd_name_rev, RUN_SETUP }, { "pack-objects", cmd_pack_objects, RUN_SETUP }, - { "peek-remote", cmd_peek_remote }, + { "peek-remote", cmd_ls_remote }, { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, { "prune-packed", cmd_prune_packed, RUN_SETUP }, From f76734902bba47afff622068524a0c38f642d769 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 5 Nov 2007 00:11:15 -0500 Subject: [PATCH 06/24] more terse push output This changes the output of send-pack to match the new, more terse fetch output. It looks like this: To git://host.tld/path/to/repo + f3325dc...3b91d1c hasforce -> mirror/hasforce (forced update) f3325dc..bb022dc master -> mirror/master ! [rejected] needsforce -> mirror/needsforce (non-fast forward) * [new branch] newbranch -> mirror/newbranch * [new tag] v1.0 -> v1.0 instead of: updating 'refs/heads/mirror/hasforce' using 'refs/heads/hasforce' from f3325dca9c4a34d74012c0e159254f454930cec7 to 3b91d1c310ca9d7b547b85466dd876e143498304 updating 'refs/heads/mirror/master' using 'refs/heads/master' from f3325dca9c4a34d74012c0e159254f454930cec7 to bb022dc363d5c2aa9aa3026beb9706d44fbe1328 error: remote 'refs/heads/mirror/needsforce' is not an ancestor of local 'refs/heads/needsforce'. Maybe you are not up-to-date and need to pull first? updating 'refs/heads/mirror/newbranch' using 'refs/heads/newbranch' from 0000000000000000000000000000000000000000 to 3b91d1c310ca9d7b547b85466dd876e143498304 updating 'refs/tags/v1.0' from 0000000000000000000000000000000000000000 to bb022dc363d5c2aa9aa3026beb9706d44fbe1328 Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 81 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 64 insertions(+), 17 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index aedef09ef0..d74cc3c4a6 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -206,7 +206,18 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref) } } -static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, const char **refspec) +static const char *prettify_ref(const char *name) +{ + return name + ( + !prefixcmp(name, "refs/heads/") ? 11 : + !prefixcmp(name, "refs/tags/") ? 10 : + !prefixcmp(name, "refs/remotes/") ? 13 : + 0); +} + +#define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3) + +static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec) { struct ref *ref; int new_refs; @@ -214,6 +225,7 @@ static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, int ask_for_status_report = 0; int allow_deleting_refs = 0; int expect_status_report = 0; + int shown_dest = 0; /* No funny business with the matcher */ remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); @@ -245,21 +257,33 @@ static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, for (ref = remote_refs; ref; ref = ref->next) { char old_hex[60], *new_hex; int will_delete_ref; + const char *pretty_ref; + const char *pretty_peer; if (!ref->peer_ref) continue; + if (!shown_dest) { + fprintf(stderr, "To %s\n", dest); + shown_dest = 1; + } + + pretty_ref = prettify_ref(ref->name); + pretty_peer = prettify_ref(ref->peer_ref->name); will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1); if (will_delete_ref && !allow_deleting_refs) { - error("remote does not support deleting refs"); + fprintf(stderr, " ! %-*s %s (remote does not support deleting refs)\n", + SUMMARY_WIDTH, "[rejected]", pretty_ref); ret = -2; continue; } if (!will_delete_ref && !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { if (args.verbose) - fprintf(stderr, "'%s': up-to-date\n", ref->name); + fprintf(stderr, " = %-*s %s -> %s\n", + SUMMARY_WIDTH, "[up to date]", + pretty_peer, pretty_ref); continue; } @@ -296,12 +320,9 @@ static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, * commits at the remote end and likely * we were not up to date to begin with. */ - error("remote '%s' is not a strict " - "subset of local ref '%s'. " - "maybe you are not up-to-date and " - "need to pull first?", - ref->name, - ref->peer_ref->name); + fprintf(stderr, " ! %-*s %s -> %s (non-fast forward)\n", + SUMMARY_WIDTH, "[rejected]", + pretty_peer, pretty_ref); ret = -2; continue; } @@ -325,14 +346,40 @@ static int do_send_pack(int in, int out, struct remote *remote, int nr_refspec, old_hex, new_hex, ref->name); } if (will_delete_ref) - fprintf(stderr, "deleting '%s'\n", ref->name); + fprintf(stderr, " - %-*s %s\n", + SUMMARY_WIDTH, "[deleting]", + pretty_ref); + else if (is_null_sha1(ref->old_sha1)) { + const char *msg; + + if (!prefixcmp(ref->name, "refs/tags/")) + msg = "[new tag]"; + else + msg = "[new branch]"; + fprintf(stderr, " * %-*s %s -> %s\n", + SUMMARY_WIDTH, msg, + pretty_peer, pretty_ref); + } else { - fprintf(stderr, "updating '%s'", ref->name); - if (strcmp(ref->name, ref->peer_ref->name)) - fprintf(stderr, " using '%s'", - ref->peer_ref->name); - fprintf(stderr, "\n from %s\n to %s\n", - old_hex, new_hex); + char quickref[83]; + char type = ' '; + const char *msg = ""; + + strcpy(quickref, find_unique_abbrev(ref->old_sha1, DEFAULT_ABBREV)); + if (ref_newer(ref->peer_ref->new_sha1, ref->old_sha1)) + strcat(quickref, ".."); + else { + strcat(quickref, "..."); + type = '+'; + msg = " (forced update)"; + } + strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); + + fprintf(stderr, " %c %-*s %s -> %s%s\n", + type, + SUMMARY_WIDTH, quickref, + pretty_peer, pretty_ref, + msg); } } @@ -460,7 +507,7 @@ int send_pack(struct send_pack_args *my_args, verify_remote_names(nr_heads, heads); conn = git_connect(fd, dest, args.receivepack, args.verbose ? CONNECT_VERBOSE : 0); - ret = do_send_pack(fd[0], fd[1], remote, nr_heads, heads); + ret = do_send_pack(fd[0], fd[1], remote, dest, nr_heads, heads); close(fd[0]); close(fd[1]); ret |= finish_connect(conn); From 3b70da2b17dc5b7df644701a96a141d8f7c5ea15 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 5 Nov 2007 00:11:41 -0500 Subject: [PATCH 07/24] receive-pack: don't mention successful updates The proposed updates are already shown to the user by send-pack, so there's no point. We continue to show errors, since they are unexpected. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- receive-pack.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/receive-pack.c b/receive-pack.c index 38e35c06b9..ed44b897f6 100644 --- a/receive-pack.c +++ b/receive-pack.c @@ -204,8 +204,6 @@ static const char *update(struct command *cmd) error("failed to delete %s", name); return "failed to delete"; } - fprintf(stderr, "%s: %s -> deleted\n", name, - sha1_to_hex(old_sha1)); return NULL; /* good */ } else { @@ -217,8 +215,6 @@ static const char *update(struct command *cmd) if (write_ref_sha1(lock, new_sha1, "push")) { return "failed to write"; /* error() already called */ } - fprintf(stderr, "%s: %s -> %s\n", name, - sha1_to_hex(old_sha1), sha1_to_hex(new_sha1)); return NULL; /* good */ } } From b50fa2bd061c3bb21f2918849ece43ac9ca2c504 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 5 Nov 2007 00:12:18 -0500 Subject: [PATCH 08/24] send-pack: require --verbose to show update of tracking refs This is really an uninteresting detail, and it just takes attention away from the actual push updates and posssible errors. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index d74cc3c4a6..c1fd3f5fbb 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -195,7 +195,8 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref) return; if (!remote_find_tracking(remote, &rs)) { - fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); + if (args.verbose) + fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); if (is_null_sha1(ref->peer_ref->new_sha1)) { if (delete_ref(rs.dst, NULL)) error("Failed to delete"); From 7c2c6ee7e0259d591acb3d9841cf5417e6b7a8eb Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Tue, 6 Nov 2007 20:29:20 -0500 Subject: [PATCH 09/24] Reteach builtin-ls-remote to understand remotes Prior to being made a builtin git-ls-remote understood that when it was given a remote name we wanted it to resolve that to the pre-configured URL and connect to that location. That changed when it was converted to a builtin and many of my automation tools broke. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- builtin-ls-remote.c | 6 ++++- t/t5512-ls-remote.sh | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100755 t/t5512-ls-remote.sh diff --git a/builtin-ls-remote.c b/builtin-ls-remote.c index 003580c26a..56f3f88023 100644 --- a/builtin-ls-remote.c +++ b/builtin-ls-remote.c @@ -14,6 +14,7 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) unsigned flags = 0; const char *uploadpack = NULL; + struct remote *remote; struct transport *transport; const struct ref *ref; @@ -52,7 +53,10 @@ int cmd_ls_remote(int argc, const char **argv, const char *prefix) if (!dest || i != argc - 1) usage(ls_remote_usage); - transport = transport_get(NULL, dest); + remote = nongit ? NULL : remote_get(dest); + if (remote && !remote->url_nr) + die("remote %s has no configured URL", dest); + transport = transport_get(remote, remote ? remote->url[0] : dest); if (uploadpack != NULL) transport_set_option(transport, TRANS_OPT_UPLOADPACK, uploadpack); diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh new file mode 100755 index 0000000000..6ec5f7c48b --- /dev/null +++ b/t/t5512-ls-remote.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +test_description='git ls-remote' + +. ./test-lib.sh + +test_expect_success setup ' + + >file && + git add file && + test_tick && + git commit -m initial && + git tag mark && + git show-ref --tags -d | sed -e "s/ / /" >expected.tag && + ( + echo "$(git rev-parse HEAD) HEAD" + git show-ref -d | sed -e "s/ / /" + ) >expected.all && + + git remote add self $(pwd)/.git + +' + +test_expect_success 'ls-remote --tags .git' ' + + git ls-remote --tags .git >actual && + diff -u expected.tag actual + +' + +test_expect_success 'ls-remote .git' ' + + git ls-remote .git >actual && + diff -u expected.all actual + +' + +test_expect_success 'ls-remote --tags self' ' + + git ls-remote --tags self >actual && + diff -u expected.tag actual + +' + +test_expect_success 'ls-remote self' ' + + git ls-remote self >actual && + diff -u expected.all actual + +' + +test_done From 6738c8194286fa0017f72cb57628dcae9ec07b27 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Thu, 8 Nov 2007 01:38:12 -0800 Subject: [PATCH 10/24] send-pack: segfault fix on forced push When pushing to overwrite a ref that points at a commit we do not even have, the recent "terse push" patch tried to get a unique abbreviation for the non-existent (from our point of view) object, which resulted in strcpy(buf, NULL) and segfaulted. Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 5 +++-- t/t5405-send-pack-rewind.sh | 42 +++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100755 t/t5405-send-pack-rewind.sh diff --git a/builtin-send-pack.c b/builtin-send-pack.c index c1fd3f5fbb..5a0f5c681c 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -365,8 +365,9 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest char quickref[83]; char type = ' '; const char *msg = ""; - - strcpy(quickref, find_unique_abbrev(ref->old_sha1, DEFAULT_ABBREV)); + const char *old_abb; + old_abb = find_unique_abbrev(ref->old_sha1, DEFAULT_ABBREV); + strcpy(quickref, old_abb ? old_abb : old_hex); if (ref_newer(ref->peer_ref->new_sha1, ref->old_sha1)) strcat(quickref, ".."); else { diff --git a/t/t5405-send-pack-rewind.sh b/t/t5405-send-pack-rewind.sh new file mode 100755 index 0000000000..86abc62271 --- /dev/null +++ b/t/t5405-send-pack-rewind.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +test_description='forced push to replace commit we do not have' + +. ./test-lib.sh + +test_expect_success setup ' + + >file1 && git add file1 && test_tick && + git commit -m Initial && + + mkdir another && ( + cd another && + git init && + git fetch .. master:master + ) && + + >file2 && git add file2 && test_tick && + git commit -m Second + +' + +test_expect_success 'non forced push should die not segfault' ' + + ( + cd another && + git push .. master:master + test $? = 1 + ) + +' + +test_expect_success 'forced push should succeed' ' + + ( + cd another && + git push .. +master:master + ) + +' + +test_done From 28b9d6e548322755bbdb24c28a493862f61b1eba Mon Sep 17 00:00:00 2001 From: Andy Whitcroft Date: Fri, 9 Nov 2007 23:32:10 +0000 Subject: [PATCH 11/24] Teach send-pack a mirror mode Existing "git push --all" is almost perfect for backing up to another repository, except that "--all" only means "all branches" in modern git, and it does not delete old branches and tags that exist at the back-up repository that you have removed from your local repository. This teaches "git-send-pack" a new "--mirror" option. The difference from the "--all" option are that (1) it sends all refs, not just branches, and (2) it deletes old refs you no longer have on the local side from the remote side. Original patch by Junio C Hamano. Signed-off-by: Andy Whitcroft Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 48 +++++++++++++++++++++++++++++++++------------ http-push.c | 4 ++-- remote.c | 15 +++++++++----- remote.h | 7 +++++++ send-pack.h | 1 + 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 5a0f5c681c..d42164ec08 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -8,7 +8,7 @@ #include "send-pack.h" static const char send_pack_usage[] = -"git-send-pack [--all] [--dry-run] [--force] [--receive-pack=] [--verbose] [--thin] [:] [...]\n" +"git-send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=] [--verbose] [--thin] [:] [...]\n" " --all and explicit specification are mutually exclusive."; static struct send_pack_args args = { @@ -227,6 +227,12 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest int allow_deleting_refs = 0; int expect_status_report = 0; int shown_dest = 0; + int flags = MATCH_REFS_NONE; + + if (args.send_all) + flags |= MATCH_REFS_ALL; + if (args.send_mirror) + flags |= MATCH_REFS_MIRROR; /* No funny business with the matcher */ remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); @@ -242,7 +248,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest if (!remote_tail) remote_tail = &remote_refs; if (match_refs(local_refs, remote_refs, &remote_tail, - nr_refspec, refspec, args.send_all)) + nr_refspec, refspec, flags)) return -1; if (!remote_refs) { @@ -259,20 +265,28 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest char old_hex[60], *new_hex; int will_delete_ref; const char *pretty_ref; - const char *pretty_peer; + const char *pretty_peer = NULL; /* only used when not deleting */ + const unsigned char *new_sha1; - if (!ref->peer_ref) - continue; + if (!ref->peer_ref) { + if (!args.send_mirror) + continue; + new_sha1 = null_sha1; + } + else + new_sha1 = ref->peer_ref->new_sha1; if (!shown_dest) { fprintf(stderr, "To %s\n", dest); shown_dest = 1; } - pretty_ref = prettify_ref(ref->name); - pretty_peer = prettify_ref(ref->peer_ref->name); + will_delete_ref = is_null_sha1(new_sha1); + + pretty_ref = prettify_ref(ref->name); + if (!will_delete_ref) + pretty_peer = prettify_ref(ref->peer_ref->name); - will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1); if (will_delete_ref && !allow_deleting_refs) { fprintf(stderr, " ! %-*s %s (remote does not support deleting refs)\n", SUMMARY_WIDTH, "[rejected]", pretty_ref); @@ -280,7 +294,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest continue; } if (!will_delete_ref && - !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { + !hashcmp(ref->old_sha1, new_sha1)) { if (args.verbose) fprintf(stderr, " = %-*s %s -> %s\n", SUMMARY_WIDTH, "[up to date]", @@ -312,8 +326,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest !is_null_sha1(ref->old_sha1) && !ref->force) { if (!has_sha1_file(ref->old_sha1) || - !ref_newer(ref->peer_ref->new_sha1, - ref->old_sha1)) { + !ref_newer(new_sha1, ref->old_sha1)) { /* We do not have the remote ref, or * we know that the remote ref is not * an ancestor of what we are trying to @@ -328,7 +341,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest continue; } } - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); + hashcpy(ref->new_sha1, new_sha1); if (!will_delete_ref) new_refs++; strcpy(old_hex, sha1_to_hex(ref->old_sha1)); @@ -459,6 +472,10 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix) args.dry_run = 1; continue; } + if (!strcmp(arg, "--mirror")) { + args.send_mirror = 1; + continue; + } if (!strcmp(arg, "--force")) { args.force_update = 1; continue; @@ -483,7 +500,12 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix) } if (!dest) usage(send_pack_usage); - if (heads && args.send_all) + /* + * --all and --mirror are incompatible; neither makes sense + * with any refspecs. + */ + if ((heads && (args.send_all || args.send_mirror)) || + (args.send_all && args.send_mirror)) usage(send_pack_usage); if (remote_name) { diff --git a/http-push.c b/http-push.c index 99328f5909..66b81f1726 100644 --- a/http-push.c +++ b/http-push.c @@ -78,7 +78,7 @@ static struct curl_slist *no_pragma_header; static struct curl_slist *default_headers; static int push_verbosely; -static int push_all; +static int push_all = MATCH_REFS_NONE; static int force_all; static int dry_run; @@ -2300,7 +2300,7 @@ int main(int argc, char **argv) if (*arg == '-') { if (!strcmp(arg, "--all")) { - push_all = 1; + push_all = MATCH_REFS_ALL; continue; } if (!strcmp(arg, "--force")) { diff --git a/remote.c b/remote.c index 59defdbf14..09b7aad525 100644 --- a/remote.c +++ b/remote.c @@ -722,10 +722,12 @@ static const struct refspec *check_pattern_match(const struct refspec *rs, * without thinking. */ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, - int nr_refspec, const char **refspec, int all) + int nr_refspec, const char **refspec, int flags) { struct refspec *rs = parse_ref_spec(nr_refspec, (const char **) refspec); + int send_all = flags & MATCH_REFS_ALL; + int send_mirror = flags & MATCH_REFS_MIRROR; if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec)) return -1; @@ -742,7 +744,7 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, if (!pat) continue; } - else if (prefixcmp(src->name, "refs/heads/")) + else if (!send_mirror && prefixcmp(src->name, "refs/heads/")) /* * "matching refs"; traditionally we pushed everything * including refs outside refs/heads/ hierarchy, but @@ -763,10 +765,13 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, if (dst_peer && dst_peer->peer_ref) /* We're already sending something to this ref. */ goto free_name; - if (!dst_peer && !nr_refspec && !all) - /* Remote doesn't have it, and we have no + + if (!dst_peer && !nr_refspec && !(send_all || send_mirror)) + /* + * Remote doesn't have it, and we have no * explicit pattern, and we don't have - * --all. */ + * --all nor --mirror. + */ goto free_name; if (!dst_peer) { /* Create a new one and link it */ diff --git a/remote.h b/remote.h index 6a4c7a0f37..b10036cae6 100644 --- a/remote.h +++ b/remote.h @@ -102,4 +102,11 @@ struct branch *branch_get(const char *name); int branch_has_merge_config(struct branch *branch); int branch_merge_matches(struct branch *, int n, const char *); +/* Flags to match_refs. */ +enum match_refs_flags { + MATCH_REFS_NONE = 0, + MATCH_REFS_ALL = (1 << 0), + MATCH_REFS_MIRROR = (1 << 1), +}; + #endif diff --git a/send-pack.h b/send-pack.h index 7a24f71c77..8ff1dc3539 100644 --- a/send-pack.h +++ b/send-pack.h @@ -5,6 +5,7 @@ struct send_pack_args { const char *receivepack; unsigned verbose:1, send_all:1, + send_mirror:1, force_update:1, use_thin_pack:1, dry_run:1; From 94c89ba662e964c544fdb171dc8dd33f95b97942 Mon Sep 17 00:00:00 2001 From: Andy Whitcroft Date: Fri, 9 Nov 2007 23:32:25 +0000 Subject: [PATCH 12/24] git-push: plumb in --mirror mode Plumb in the --mirror mode for git-push. Signed-off-by: Andy Whitcroft Signed-off-by: Junio C Hamano --- builtin-push.c | 14 ++++++++++++-- transport.c | 7 +++++++ transport.h | 1 + 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/builtin-push.c b/builtin-push.c index 2c561953fc..d49157c926 100644 --- a/builtin-push.c +++ b/builtin-push.c @@ -10,7 +10,7 @@ #include "parse-options.h" static const char * const push_usage[] = { - "git-push [--all] [--dry-run] [--tags] [--receive-pack=] [--repo=all] [-f | --force] [-v] [ ...]", + "git-push [--all | --mirror] [--dry-run] [--tags] [--receive-pack=] [--repo=all] [-f | --force] [-v] [ ...]", NULL, }; @@ -91,6 +91,7 @@ int cmd_push(int argc, const char **argv, const char *prefix) { int flags = 0; int all = 0; + int mirror = 0; int dry_run = 0; int force = 0; int tags = 0; @@ -100,6 +101,7 @@ int cmd_push(int argc, const char **argv, const char *prefix) OPT__VERBOSE(&verbose), OPT_STRING( 0 , "repo", &repo, "repository", "repository"), OPT_BOOLEAN( 0 , "all", &all, "push all refs"), + OPT_BOOLEAN( 0 , "mirror", &mirror, "mirror all refs"), OPT_BOOLEAN( 0 , "tags", &tags, "push tags"), OPT_BOOLEAN( 0 , "dry-run", &dry_run, "dry run"), OPT_BOOLEAN('f', "force", &force, "force updates"), @@ -119,13 +121,21 @@ int cmd_push(int argc, const char **argv, const char *prefix) add_refspec("refs/tags/*"); if (all) flags |= TRANSPORT_PUSH_ALL; + if (mirror) + flags |= (TRANSPORT_PUSH_MIRROR|TRANSPORT_PUSH_FORCE); if (argc > 0) { repo = argv[0]; set_refspecs(argv + 1, argc - 1); } - if ((flags & TRANSPORT_PUSH_ALL) && refspec) + if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) && refspec) usage_with_options(push_usage, options); + if ((flags & (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) == + (TRANSPORT_PUSH_ALL|TRANSPORT_PUSH_MIRROR)) { + error("--all and --mirror are incompatible"); + usage_with_options(push_usage, options); + } + return do_push(repo, flags); } diff --git a/transport.c b/transport.c index 83677fca40..fad97d7cb2 100644 --- a/transport.c +++ b/transport.c @@ -284,6 +284,9 @@ static int rsync_transport_push(struct transport *transport, struct child_process rsync; const char *args[10]; + if (flags & TRANSPORT_PUSH_MIRROR) + return error("rsync transport does not support mirror mode"); + /* first push the objects */ strbuf_addstr(&buf, transport->url); @@ -387,6 +390,9 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons int argc; int err; + if (flags & TRANSPORT_PUSH_MIRROR) + return error("http transport does not support mirror mode"); + argv = xmalloc((refspec_nr + 11) * sizeof(char *)); argv[0] = "http-push"; argc = 1; @@ -655,6 +661,7 @@ static int git_transport_push(struct transport *transport, int refspec_nr, const args.receivepack = data->receivepack; args.send_all = !!(flags & TRANSPORT_PUSH_ALL); + args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); args.use_thin_pack = data->thin; args.verbose = transport->verbose; diff --git a/transport.h b/transport.h index d27f5629d2..7f337d2f0f 100644 --- a/transport.h +++ b/transport.h @@ -30,6 +30,7 @@ struct transport { #define TRANSPORT_PUSH_ALL 1 #define TRANSPORT_PUSH_FORCE 2 #define TRANSPORT_PUSH_DRY_RUN 4 +#define TRANSPORT_PUSH_MIRROR 8 /* Returns a transport suitable for the url */ struct transport *transport_get(struct remote *, const char *); From 6fa92bf3cdbfdd8c9c56ef40a5170a859442b166 Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 Nov 2007 22:38:23 +0100 Subject: [PATCH 13/24] Add a test checking if send-pack updated local tracking branches correctly Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- t/t5404-tracking-branches.sh | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100755 t/t5404-tracking-branches.sh diff --git a/t/t5404-tracking-branches.sh b/t/t5404-tracking-branches.sh new file mode 100755 index 0000000000..20718d4679 --- /dev/null +++ b/t/t5404-tracking-branches.sh @@ -0,0 +1,40 @@ +#!/bin/sh + +test_description='tracking branch update checks for git push' + +. ./test-lib.sh + +test_expect_success 'setup' ' + echo 1 >file && + git add file && + git commit -m 1 && + git branch b1 && + git branch b2 && + git clone . aa && + git checkout b1 && + echo b1 >>file && + git commit -a -m b1 && + git checkout b2 && + echo b2 >>file && + git commit -a -m b2 +' + +test_expect_success 'check tracking branches updated correctly after push' ' + cd aa && + b1=$(git rev-parse origin/b1) && + b2=$(git rev-parse origin/b2) && + git checkout -b b1 origin/b1 && + echo aa-b1 >>file && + git commit -a -m aa-b1 && + git checkout -b b2 origin/b2 && + echo aa-b2 >>file && + git commit -a -m aa-b2 && + git checkout master && + echo aa-master >>file && + git commit -a -m aa-master && + git push && + test "$(git rev-parse origin/b1)" = "$b1" && + test "$(git rev-parse origin/b2)" = "$b2" +' + +test_done From ed31df312a30d91c288a4b6e3d031de15284405a Mon Sep 17 00:00:00 2001 From: Alex Riesen Date: Mon, 12 Nov 2007 22:39:38 +0100 Subject: [PATCH 14/24] Update the tracking references only if they were succesfully updated on remote It fixes the bug where local tracking branches were filled with zeroed SHA-1 if the remote branch was not updated because, for instance, it was not an ancestor of the local (i.e. had other changes). Signed-off-by: Alex Riesen Signed-off-by: Junio C Hamano --- send-pack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/send-pack.c b/send-pack.c index b74fd454f2..d56d980af7 100644 --- a/send-pack.c +++ b/send-pack.c @@ -349,7 +349,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha if (!dry_run && remote && ret == 0) { for (ref = remote_refs; ref; ref = ref->next) - update_tracking_ref(remote, ref); + if (!is_null_sha1(ref->new_sha1)) + update_tracking_ref(remote, ref); } if (!new_refs && ret == 0) From 8e806adb65dc4a0df91a0ebd54b76bb24ff1263e Mon Sep 17 00:00:00 2001 From: Andy Whitcroft Date: Fri, 9 Nov 2007 23:32:41 +0000 Subject: [PATCH 15/24] Add tests for git push'es mirror mode Add some tests for git push --mirror mode. Signed-off-by: Andy Whitcroft Signed-off-by: Junio C Hamano --- t/t5517-push-mirror.sh | 228 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 228 insertions(+) create mode 100755 t/t5517-push-mirror.sh diff --git a/t/t5517-push-mirror.sh b/t/t5517-push-mirror.sh new file mode 100755 index 0000000000..ed3fec192a --- /dev/null +++ b/t/t5517-push-mirror.sh @@ -0,0 +1,228 @@ +#!/bin/sh + +test_description='pushing to a mirror repository' + +. ./test-lib.sh + +D=`pwd` + +invert () { + if "$@"; then + return 1 + else + return 0 + fi +} + +mk_repo_pair () { + rm -rf master mirror && + mkdir mirror && + ( + cd mirror && + git init + ) && + mkdir master && + ( + cd master && + git init && + git config remote.up.url ../mirror + ) +} + + +# BRANCH tests +test_expect_success 'push mirror creates new branches' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/heads/master) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/heads/master) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror updates existing branches' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git push --mirror up && + echo two >foo && git add foo && git commit -m two && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/heads/master) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/heads/master) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror force updates existing branches' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git push --mirror up && + echo two >foo && git add foo && git commit -m two && + git push --mirror up && + git reset --hard HEAD^ + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/heads/master) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/heads/master) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror removes branches' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git branch remove master && + git push --mirror up && + git branch -D remove + git push --mirror up + ) && + ( + cd mirror && + invert git show-ref -s --verify refs/heads/remove + ) + +' + +test_expect_success 'push mirror adds, updates and removes branches together' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git branch remove master && + git push --mirror up && + git branch -D remove && + git branch add master && + echo two >foo && git add foo && git commit -m two && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/heads/master) && + master_add=$(cd master && git show-ref -s --verify refs/heads/add) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/heads/master) && + mirror_add=$(cd mirror && git show-ref -s --verify refs/heads/add) && + test "$master_master" = "$mirror_master" && + test "$master_add" = "$mirror_add" && + ( + cd mirror && + invert git show-ref -s --verify refs/heads/remove + ) + +' + + +# TAG tests +test_expect_success 'push mirror creates new tags' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git tag -f tmaster master && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/tags/tmaster) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/tags/tmaster) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror updates existing tags' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git tag -f tmaster master && + git push --mirror up && + echo two >foo && git add foo && git commit -m two && + git tag -f tmaster master && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/tags/tmaster) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/tags/tmaster) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror force updates existing tags' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git tag -f tmaster master && + git push --mirror up && + echo two >foo && git add foo && git commit -m two && + git tag -f tmaster master && + git push --mirror up && + git reset --hard HEAD^ + git tag -f tmaster master && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/tags/tmaster) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/tags/tmaster) && + test "$master_master" = "$mirror_master" + +' + +test_expect_success 'push mirror removes tags' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git tag -f tremove master && + git push --mirror up && + git tag -d tremove + git push --mirror up + ) && + ( + cd mirror && + invert git show-ref -s --verify refs/tags/tremove + ) + +' + +test_expect_success 'push mirror adds, updates and removes tags together' ' + + mk_repo_pair && + ( + cd master && + echo one >foo && git add foo && git commit -m one && + git tag -f tmaster master && + git tag -f tremove master && + git push --mirror up && + git tag -d tremove && + git tag tadd master && + echo two >foo && git add foo && git commit -m two && + git tag -f tmaster master && + git push --mirror up + ) && + master_master=$(cd master && git show-ref -s --verify refs/tags/tmaster) && + master_add=$(cd master && git show-ref -s --verify refs/tags/tadd) && + mirror_master=$(cd mirror && git show-ref -s --verify refs/tags/tmaster) && + mirror_add=$(cd mirror && git show-ref -s --verify refs/tags/tadd) && + test "$master_master" = "$mirror_master" && + test "$master_add" = "$mirror_add" && + ( + cd mirror && + invert git show-ref -s --verify refs/tags/tremove + ) + +' + +test_done From ff206748158aa54196bde1462ceaf550a5c2440e Mon Sep 17 00:00:00 2001 From: Andy Whitcroft Date: Fri, 9 Nov 2007 23:32:57 +0000 Subject: [PATCH 16/24] git-push: add documentation for the newly added --mirror mode Add some basic documentation on the --mirror mode for git-push. Signed-off-by: Andy Whitcroft Signed-off-by: Junio C Hamano --- Documentation/git-push.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Documentation/git-push.txt b/Documentation/git-push.txt index e5dd4c1066..3fa5992313 100644 --- a/Documentation/git-push.txt +++ b/Documentation/git-push.txt @@ -63,6 +63,14 @@ the remote repository. Instead of naming each ref to push, specifies that all refs under `$GIT_DIR/refs/heads/` be pushed. +\--mirror:: + Instead of naming each ref to push, specifies that all + refs under `$GIT_DIR/refs/heads/` and `$GIT_DIR/refs/tags/` + be mirrored to the remote repository. Newly created local + refs will be pushed to the remote end, locally updated refs + will be force updated on the remote end, and deleted refs + will be removed from the remote end. + \--dry-run:: Do everything except actually send the updates. From 8736a8489080509516f5f4cc1cc74de33150f397 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 17 Nov 2007 07:54:27 -0500 Subject: [PATCH 17/24] send-pack: track errors for each ref Instead of keeping the 'ret' variable, we instead have a status flag for each ref that tracks what happened to it. We then print the ref status after all of the refs have been examined. This paves the way for three improvements: - updating tracking refs only for non-error refs - incorporating remote rejection into the printed status - printing errors in a different order than we processed (e.g., consolidating non-ff errors near the end with a special message) Signed-off-by: Jeff King Acked-by: Alex Riesen Acked-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 224 +++++++++++++++++++++-------------- cache.h | 13 +- t/t5404-tracking-branches.sh | 14 ++- 3 files changed, 156 insertions(+), 95 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 418925eb01..dafc02bcb0 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -207,8 +207,9 @@ static void update_tracking_ref(struct remote *remote, struct ref *ref) } } -static const char *prettify_ref(const char *name) +static const char *prettify_ref(const struct ref *ref) { + const char *name = ref->name; return name + ( !prefixcmp(name, "refs/heads/") ? 11 : !prefixcmp(name, "refs/tags/") ? 10 : @@ -218,15 +219,104 @@ static const char *prettify_ref(const char *name) #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3) +static void print_ref_status(char flag, const char *summary, struct ref *to, struct ref *from, const char *msg) +{ + fprintf(stderr, " %c %-*s ", flag, SUMMARY_WIDTH, summary); + if (from) + fprintf(stderr, "%s -> %s", prettify_ref(from), prettify_ref(to)); + else + fputs(prettify_ref(to), stderr); + if (msg) { + fputs(" (", stderr); + fputs(msg, stderr); + fputc(')', stderr); + } + fputc('\n', stderr); +} + +static const char *status_abbrev(unsigned char sha1[20]) +{ + const char *abbrev; + abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV); + return abbrev ? abbrev : sha1_to_hex(sha1); +} + +static void print_ok_ref_status(struct ref *ref) +{ + if (ref->deletion) + print_ref_status('-', "[deleted]", ref, NULL, NULL); + else if (is_null_sha1(ref->old_sha1)) + print_ref_status('*', + (!prefixcmp(ref->name, "refs/tags/") ? "[new tag]" : + "[new branch]"), + ref, ref->peer_ref, NULL); + else { + char quickref[84]; + char type; + const char *msg; + + strcpy(quickref, status_abbrev(ref->old_sha1)); + if (ref->nonfastforward) { + strcat(quickref, "..."); + type = '+'; + msg = "forced update"; + } else { + strcat(quickref, ".."); + type = ' '; + msg = NULL; + } + strcat(quickref, status_abbrev(ref->new_sha1)); + + print_ref_status(type, quickref, ref, ref->peer_ref, msg); + } +} + +static void print_push_status(const char *dest, struct ref *refs) +{ + struct ref *ref; + int shown_dest = 0; + + for (ref = refs; ref; ref = ref->next) { + if (!ref->status) + continue; + if (ref->status == REF_STATUS_UPTODATE && !args.verbose) + continue; + + if (!shown_dest) { + fprintf(stderr, "To %s\n", dest); + shown_dest = 1; + } + + switch(ref->status) { + case REF_STATUS_NONE: + print_ref_status('X', "[no match]", ref, NULL, NULL); + break; + case REF_STATUS_REJECT_NODELETE: + print_ref_status('!', "[rejected]", ref, NULL, + "remote does not support deleting refs"); + break; + case REF_STATUS_UPTODATE: + print_ref_status('=', "[up to date]", ref, + ref->peer_ref, NULL); + break; + case REF_STATUS_REJECT_NONFASTFORWARD: + print_ref_status('!', "[rejected]", ref, ref->peer_ref, + "non-fast forward"); + break; + case REF_STATUS_OK: + print_ok_ref_status(ref); + break; + } + } +} + static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec) { struct ref *ref; int new_refs; - int ret = 0; int ask_for_status_report = 0; int allow_deleting_refs = 0; int expect_status_report = 0; - int shown_dest = 0; int flags = MATCH_REFS_NONE; if (args.send_all) @@ -262,10 +352,6 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest */ new_refs = 0; for (ref = remote_refs; ref; ref = ref->next) { - char old_hex[60], *new_hex; - int will_delete_ref; - const char *pretty_ref; - const char *pretty_peer = NULL; /* only used when not deleting */ const unsigned char *new_sha1; if (!ref->peer_ref) { @@ -276,29 +362,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest else new_sha1 = ref->peer_ref->new_sha1; - if (!shown_dest) { - fprintf(stderr, "To %s\n", dest); - shown_dest = 1; - } - will_delete_ref = is_null_sha1(new_sha1); - - pretty_ref = prettify_ref(ref->name); - if (!will_delete_ref) - pretty_peer = prettify_ref(ref->peer_ref->name); - - if (will_delete_ref && !allow_deleting_refs) { - fprintf(stderr, " ! %-*s %s (remote does not support deleting refs)\n", - SUMMARY_WIDTH, "[rejected]", pretty_ref); - ret = -2; + ref->deletion = is_null_sha1(new_sha1); + if (ref->deletion && !allow_deleting_refs) { + ref->status = REF_STATUS_REJECT_NODELETE; continue; } - if (!will_delete_ref && + if (!ref->deletion && !hashcmp(ref->old_sha1, new_sha1)) { - if (args.verbose) - fprintf(stderr, " = %-*s %s -> %s\n", - SUMMARY_WIDTH, "[up to date]", - pretty_peer, pretty_ref); + ref->status = REF_STATUS_UPTODATE; continue; } @@ -321,33 +393,26 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest * always allowed. */ - if (!args.force_update && - !will_delete_ref && + ref->nonfastforward = + !ref->deletion && !is_null_sha1(ref->old_sha1) && - !ref->force) { - if (!has_sha1_file(ref->old_sha1) || - !ref_newer(new_sha1, ref->old_sha1)) { - /* We do not have the remote ref, or - * we know that the remote ref is not - * an ancestor of what we are trying to - * push. Either way this can be losing - * commits at the remote end and likely - * we were not up to date to begin with. - */ - fprintf(stderr, " ! %-*s %s -> %s (non-fast forward)\n", - SUMMARY_WIDTH, "[rejected]", - pretty_peer, pretty_ref); - ret = -2; - continue; - } + (!has_sha1_file(ref->old_sha1) + || !ref_newer(new_sha1, ref->old_sha1)); + + if (ref->nonfastforward && !ref->force && !args.force_update) { + ref->status = REF_STATUS_REJECT_NONFASTFORWARD; + continue; } + hashcpy(ref->new_sha1, new_sha1); - if (!will_delete_ref) + if (!ref->deletion) new_refs++; - strcpy(old_hex, sha1_to_hex(ref->old_sha1)); - new_hex = sha1_to_hex(ref->new_sha1); + ref->status = REF_STATUS_OK; if (!args.dry_run) { + char *old_hex = sha1_to_hex(ref->old_sha1); + char *new_hex = sha1_to_hex(ref->new_sha1); + if (ask_for_status_report) { packet_write(out, "%s %s %s%c%s", old_hex, new_hex, ref->name, 0, @@ -359,64 +424,43 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest packet_write(out, "%s %s %s", old_hex, new_hex, ref->name); } - if (will_delete_ref) - fprintf(stderr, " - %-*s %s\n", - SUMMARY_WIDTH, "[deleting]", - pretty_ref); - else if (is_null_sha1(ref->old_sha1)) { - const char *msg; - - if (!prefixcmp(ref->name, "refs/tags/")) - msg = "[new tag]"; - else - msg = "[new branch]"; - fprintf(stderr, " * %-*s %s -> %s\n", - SUMMARY_WIDTH, msg, - pretty_peer, pretty_ref); - } - else { - char quickref[83]; - char type = ' '; - const char *msg = ""; - const char *old_abb; - old_abb = find_unique_abbrev(ref->old_sha1, DEFAULT_ABBREV); - strcpy(quickref, old_abb ? old_abb : old_hex); - if (ref_newer(ref->peer_ref->new_sha1, ref->old_sha1)) - strcat(quickref, ".."); - else { - strcat(quickref, "..."); - type = '+'; - msg = " (forced update)"; - } - strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV)); - - fprintf(stderr, " %c %-*s %s -> %s%s\n", - type, - SUMMARY_WIDTH, quickref, - pretty_peer, pretty_ref, - msg); - } } packet_flush(out); - if (new_refs && !args.dry_run) - ret = pack_objects(out, remote_refs); + if (new_refs && !args.dry_run) { + if (pack_objects(out, remote_refs) < 0) { + close(out); + return -1; + } + } close(out); + print_push_status(dest, remote_refs); + if (expect_status_report) { if (receive_status(in)) - ret = -4; + return -1; } - if (!args.dry_run && remote && ret == 0) { + if (!args.dry_run && remote) { for (ref = remote_refs; ref; ref = ref->next) if (!is_null_sha1(ref->new_sha1)) update_tracking_ref(remote, ref); } - if (!new_refs && ret == 0) + if (!new_refs) fprintf(stderr, "Everything up-to-date\n"); - return ret; + for (ref = remote_refs; ref; ref = ref->next) { + switch (ref->status) { + case REF_STATUS_NONE: + case REF_STATUS_UPTODATE: + case REF_STATUS_OK: + break; + default: + return -1; + } + } + return 0; } static void verify_remote_names(int nr_heads, const char **heads) diff --git a/cache.h b/cache.h index e5ea637c29..6663ec52d1 100644 --- a/cache.h +++ b/cache.h @@ -493,8 +493,17 @@ struct ref { struct ref *next; unsigned char old_sha1[20]; unsigned char new_sha1[20]; - unsigned char force; - unsigned char merge; + unsigned char force : 1; + unsigned char merge : 1; + unsigned char nonfastforward : 1; + unsigned char deletion : 1; + enum { + REF_STATUS_NONE = 0, + REF_STATUS_OK, + REF_STATUS_REJECT_NONFASTFORWARD, + REF_STATUS_REJECT_NODELETE, + REF_STATUS_UPTODATE, + } status; struct ref *peer_ref; /* when renaming */ char name[FLEX_ARRAY]; /* more */ }; diff --git a/t/t5404-tracking-branches.sh b/t/t5404-tracking-branches.sh index 20718d4679..799e47e5ba 100755 --- a/t/t5404-tracking-branches.sh +++ b/t/t5404-tracking-branches.sh @@ -19,7 +19,7 @@ test_expect_success 'setup' ' git commit -a -m b2 ' -test_expect_success 'check tracking branches updated correctly after push' ' +test_expect_success 'prepare pushable branches' ' cd aa && b1=$(git rev-parse origin/b1) && b2=$(git rev-parse origin/b2) && @@ -31,8 +31,16 @@ test_expect_success 'check tracking branches updated correctly after push' ' git commit -a -m aa-b2 && git checkout master && echo aa-master >>file && - git commit -a -m aa-master && - git push && + git commit -a -m aa-master +' + +test_expect_success 'mixed-success push returns error' '! git push' + +test_expect_success 'check tracking branches updated correctly after push' ' + test "$(git rev-parse origin/master)" = "$(git rev-parse master)" +' + +test_expect_success 'check tracking branches not updated for failed refs' ' test "$(git rev-parse origin/b1)" = "$b1" && test "$(git rev-parse origin/b2)" = "$b2" ' From 1f0e2a1a65477c2b8eb8812e5bf0ad07bf03738e Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 17 Nov 2007 07:55:15 -0500 Subject: [PATCH 18/24] send-pack: check ref->status before updating tracking refs Previously, we manually checked the 'NONE' and 'UPTODATE' conditions. Now that we have ref->status, we can easily say "only update if we pushed successfully". This adds a test for and fixes a regression introduced in ed31df31 where deleted refs did not have their tracking branches removed. This was due to a bogus per-ref error test that is superseded by the more accurate ref->status flag. Signed-off-by: Jeff King Completely-Acked-By: Alex "Sleepy" Riesen Acked-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 18 +++++------------- t/t5404-tracking-branches.sh | 5 +++++ 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index dafc02bcb0..3f86acb315 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -180,24 +180,17 @@ static int receive_status(int in) static void update_tracking_ref(struct remote *remote, struct ref *ref) { struct refspec rs; - int will_delete_ref; + + if (ref->status != REF_STATUS_OK) + return; rs.src = ref->name; rs.dst = NULL; - if (!ref->peer_ref) - return; - - will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1); - - if (!will_delete_ref && - !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) - return; - if (!remote_find_tracking(remote, &rs)) { if (args.verbose) fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); - if (is_null_sha1(ref->peer_ref->new_sha1)) { + if (ref->deletion) { if (delete_ref(rs.dst, NULL)) error("Failed to delete"); } else @@ -444,8 +437,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest if (!args.dry_run && remote) { for (ref = remote_refs; ref; ref = ref->next) - if (!is_null_sha1(ref->new_sha1)) - update_tracking_ref(remote, ref); + update_tracking_ref(remote, ref); } if (!new_refs) diff --git a/t/t5404-tracking-branches.sh b/t/t5404-tracking-branches.sh index 799e47e5ba..1493a92c06 100755 --- a/t/t5404-tracking-branches.sh +++ b/t/t5404-tracking-branches.sh @@ -45,4 +45,9 @@ test_expect_success 'check tracking branches not updated for failed refs' ' test "$(git rev-parse origin/b2)" = "$b2" ' +test_expect_success 'deleted branches have their tracking branches removed' ' + git push origin :b1 && + test "$(git rev-parse origin/b1)" = "origin/b1" +' + test_done From ca74c458a3908314cf29b96f1c43fe2b2597de76 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sat, 17 Nov 2007 07:56:03 -0500 Subject: [PATCH 19/24] send-pack: assign remote errors to each ref This lets us show remote errors (e.g., a denied hook) along with the usual push output. There is a slightly clever optimization in receive_status that bears explanation. We need to correlate the returned status and our ref objects, which naively could be an O(m*n) operation. However, since the current implementation of receive-pack returns the errors to us in the same order that we sent them, we optimistically look for the next ref to be looked up to come after the last one we have found. So it should be an O(m+n) merge if the receive-pack behavior holds, but we fall back to a correct but slower behavior if it should change. Signed-off-by: Jeff King Acked-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 51 ++++++++++++++++++++++++++++++++++----- cache.h | 2 ++ t/t5406-remote-rejects.sh | 24 ++++++++++++++++++ 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100755 t/t5406-remote-rejects.sh diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 3f86acb315..5fadd0bc95 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -146,19 +146,43 @@ static void get_local_heads(void) for_each_ref(one_local_ref, NULL); } -static int receive_status(int in) +static struct ref *set_ref_error(struct ref *refs, const char *line) { + struct ref *ref; + + for (ref = refs; ref; ref = ref->next) { + const char *msg; + if (prefixcmp(line, ref->name)) + continue; + msg = line + strlen(ref->name); + if (*msg++ != ' ') + continue; + ref->status = REF_STATUS_REMOTE_REJECT; + ref->error = xstrdup(msg); + ref->error[strlen(ref->error)-1] = '\0'; + return ref; + } + return NULL; +} + +/* a return value of -1 indicates that an error occurred, + * but we were able to set individual ref errors. A return + * value of -2 means we couldn't even get that far. */ +static int receive_status(int in, struct ref *refs) +{ + struct ref *hint; char line[1000]; int ret = 0; int len = packet_read_line(in, line, sizeof(line)); if (len < 10 || memcmp(line, "unpack ", 7)) { fprintf(stderr, "did not receive status back\n"); - return -1; + return -2; } if (memcmp(line, "unpack ok\n", 10)) { fputs(line, stderr); ret = -1; } + hint = NULL; while (1) { len = packet_read_line(in, line, sizeof(line)); if (!len) @@ -171,7 +195,10 @@ static int receive_status(int in) } if (!memcmp(line, "ok", 2)) continue; - fputs(line, stderr); + if (hint) + hint = set_ref_error(hint, line + 3); + if (!hint) + hint = set_ref_error(refs, line + 3); ret = -1; } return ret; @@ -296,6 +323,12 @@ static void print_push_status(const char *dest, struct ref *refs) print_ref_status('!', "[rejected]", ref, ref->peer_ref, "non-fast forward"); break; + case REF_STATUS_REMOTE_REJECT: + if (ref->deletion) + print_ref_status('!', "[remote rejected]", ref, NULL, ref->error); + else + print_ref_status('!', "[remote rejected]", ref, ref->peer_ref, ref->error); + break; case REF_STATUS_OK: print_ok_ref_status(ref); break; @@ -311,6 +344,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest int allow_deleting_refs = 0; int expect_status_report = 0; int flags = MATCH_REFS_NONE; + int ret; if (args.send_all) flags |= MATCH_REFS_ALL; @@ -428,12 +462,15 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest } close(out); - print_push_status(dest, remote_refs); - if (expect_status_report) { - if (receive_status(in)) + ret = receive_status(in, remote_refs); + if (ret == -2) return -1; } + else + ret = 0; + + print_push_status(dest, remote_refs); if (!args.dry_run && remote) { for (ref = remote_refs; ref; ref = ref->next) @@ -442,6 +479,8 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest if (!new_refs) fprintf(stderr, "Everything up-to-date\n"); + if (ret < 0) + return ret; for (ref = remote_refs; ref; ref = ref->next) { switch (ref->status) { case REF_STATUS_NONE: diff --git a/cache.h b/cache.h index 6663ec52d1..81e8c88e46 100644 --- a/cache.h +++ b/cache.h @@ -503,7 +503,9 @@ struct ref { REF_STATUS_REJECT_NONFASTFORWARD, REF_STATUS_REJECT_NODELETE, REF_STATUS_UPTODATE, + REF_STATUS_REMOTE_REJECT, } status; + char *error; struct ref *peer_ref; /* when renaming */ char name[FLEX_ARRAY]; /* more */ }; diff --git a/t/t5406-remote-rejects.sh b/t/t5406-remote-rejects.sh new file mode 100755 index 0000000000..46b2cb4e46 --- /dev/null +++ b/t/t5406-remote-rejects.sh @@ -0,0 +1,24 @@ +#!/bin/sh + +test_description='remote push rejects are reported by client' + +. ./test-lib.sh + +test_expect_success 'setup' ' + mkdir .git/hooks && + (echo "#!/bin/sh" ; echo "exit 1") >.git/hooks/update && + chmod +x .git/hooks/update && + echo 1 >file && + git add file && + git commit -m 1 && + git clone . child && + cd child && + echo 2 >file && + git commit -a -m 2 +' + +test_expect_success 'push reports error' '! git push 2>stderr' + +test_expect_success 'individual ref reports error' 'grep rejected stderr' + +test_done From 9f8a15c73437abc634f2c43501105b108c51eae8 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sun, 18 Nov 2007 04:31:37 -0500 Subject: [PATCH 20/24] Fix warning about bitfield in struct ref cache.h:503: warning: type of bit-field 'force' is a GCC extension cache.h:504: warning: type of bit-field 'merge' is a GCC extension cache.h:505: warning: type of bit-field 'nonfastforward' is a GCC extension cache.h:506: warning: type of bit-field 'deletion' is a GCC extension So we change it to an 'unsigned int' which is not a GCC extension. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- cache.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cache.h b/cache.h index 81e8c88e46..6ccb764229 100644 --- a/cache.h +++ b/cache.h @@ -493,10 +493,10 @@ struct ref { struct ref *next; unsigned char old_sha1[20]; unsigned char new_sha1[20]; - unsigned char force : 1; - unsigned char merge : 1; - unsigned char nonfastforward : 1; - unsigned char deletion : 1; + unsigned int force:1, + merge:1, + nonfastforward:1, + deletion:1; enum { REF_STATUS_NONE = 0, REF_STATUS_OK, From cda69f481db510e2a3f0ca8a0f4b54123c799416 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 18 Nov 2007 02:13:10 -0500 Subject: [PATCH 21/24] make "find_ref_by_name" a public function This was a static in remote.c, but is generally useful. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- cache.h | 2 ++ refs.c | 8 ++++++++ remote.c | 8 -------- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cache.h b/cache.h index 6ccb764229..8d601dd6f7 100644 --- a/cache.h +++ b/cache.h @@ -514,6 +514,8 @@ struct ref { #define REF_HEADS (1u << 1) #define REF_TAGS (1u << 2) +extern struct ref *find_ref_by_name(struct ref *list, const char *name); + #define CONNECT_VERBOSE (1u << 0) extern struct child_process *git_connect(int fd[2], const char *url, const char *prog, int flags); extern int finish_connect(struct child_process *conn); diff --git a/refs.c b/refs.c index aff02cd09d..387c588c74 100644 --- a/refs.c +++ b/refs.c @@ -1445,3 +1445,11 @@ int update_ref(const char *action, const char *refname, } return 0; } + +struct ref *find_ref_by_name(struct ref *list, const char *name) +{ + for ( ; list; list = list->next) + if (!strcmp(list->name, name)) + return list; + return NULL; +} diff --git a/remote.c b/remote.c index 09b7aad525..bb01059083 100644 --- a/remote.c +++ b/remote.c @@ -696,14 +696,6 @@ static int match_explicit_refs(struct ref *src, struct ref *dst, return -errs; } -static struct ref *find_ref_by_name(struct ref *list, const char *name) -{ - for ( ; list; list = list->next) - if (!strcmp(list->name, name)) - return list; - return NULL; -} - static const struct refspec *check_pattern_match(const struct refspec *rs, int rs_nr, const struct ref *src) From 2a0fe89a976331cb2163d4f299e38e2cb5010632 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 18 Nov 2007 02:16:52 -0500 Subject: [PATCH 22/24] send-pack: tighten remote error reporting Previously, we set all ref pushes to 'OK', and then marked them as errors if the remote reported so. This has the problem that if the remote dies or fails to report a ref, we just assume it was OK. Instead, we use a new non-OK state to indicate that we are expecting status (if the remote doesn't support the report-status feature, we fall back on the old behavior). Thus we can flag refs for which we expected a status, but got none (conversely, we now also print a warning for refs for which we get a status, but weren't expecting one). This also allows us to simplify the receive_status exit code, since each ref is individually marked with failure until we get a success response. We can just print the usual status table, so the user still gets a sense of what we were trying to do when the failure happened. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 94 +++++++++++++++++++++++++-------------------- cache.h | 3 +- 2 files changed, 54 insertions(+), 43 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 5fadd0bc95..14447bb7cd 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -146,60 +146,67 @@ static void get_local_heads(void) for_each_ref(one_local_ref, NULL); } -static struct ref *set_ref_error(struct ref *refs, const char *line) -{ - struct ref *ref; - - for (ref = refs; ref; ref = ref->next) { - const char *msg; - if (prefixcmp(line, ref->name)) - continue; - msg = line + strlen(ref->name); - if (*msg++ != ' ') - continue; - ref->status = REF_STATUS_REMOTE_REJECT; - ref->error = xstrdup(msg); - ref->error[strlen(ref->error)-1] = '\0'; - return ref; - } - return NULL; -} - -/* a return value of -1 indicates that an error occurred, - * but we were able to set individual ref errors. A return - * value of -2 means we couldn't even get that far. */ static int receive_status(int in, struct ref *refs) { struct ref *hint; char line[1000]; int ret = 0; int len = packet_read_line(in, line, sizeof(line)); - if (len < 10 || memcmp(line, "unpack ", 7)) { - fprintf(stderr, "did not receive status back\n"); - return -2; - } + if (len < 10 || memcmp(line, "unpack ", 7)) + return error("did not receive remote status"); if (memcmp(line, "unpack ok\n", 10)) { - fputs(line, stderr); + char *p = line + strlen(line) - 1; + if (*p == '\n') + *p = '\0'; + error("unpack failed: %s", line + 7); ret = -1; } hint = NULL; while (1) { + char *refname; + char *msg; len = packet_read_line(in, line, sizeof(line)); if (!len) break; if (len < 3 || - (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) { + (memcmp(line, "ok ", 3) && memcmp(line, "ng ", 3))) { fprintf(stderr, "protocol error: %s\n", line); ret = -1; break; } - if (!memcmp(line, "ok", 2)) - continue; + + line[strlen(line)-1] = '\0'; + refname = line + 3; + msg = strchr(refname, ' '); + if (msg) + *msg++ = '\0'; + + /* first try searching at our hint, falling back to all refs */ if (hint) - hint = set_ref_error(hint, line + 3); + hint = find_ref_by_name(hint, refname); if (!hint) - hint = set_ref_error(refs, line + 3); - ret = -1; + hint = find_ref_by_name(refs, refname); + if (!hint) { + warning("remote reported status on unknown ref: %s", + refname); + continue; + } + if (hint->status != REF_STATUS_EXPECTING_REPORT) { + warning("remote reported status on unexpected ref: %s", + refname); + continue; + } + + if (line[0] == 'o' && line[1] == 'k') + hint->status = REF_STATUS_OK; + else { + hint->status = REF_STATUS_REMOTE_REJECT; + ret = -1; + } + if (msg) + hint->remote_status = xstrdup(msg); + /* start our next search from the next ref */ + hint = hint->next; } return ret; } @@ -324,10 +331,14 @@ static void print_push_status(const char *dest, struct ref *refs) "non-fast forward"); break; case REF_STATUS_REMOTE_REJECT: - if (ref->deletion) - print_ref_status('!', "[remote rejected]", ref, NULL, ref->error); - else - print_ref_status('!', "[remote rejected]", ref, ref->peer_ref, ref->error); + print_ref_status('!', "[remote rejected]", ref, + ref->deletion ? NULL : ref->peer_ref, + ref->remote_status); + break; + case REF_STATUS_EXPECTING_REPORT: + print_ref_status('!', "[remote failure]", ref, + ref->deletion ? NULL : ref->peer_ref, + "remote failed to report status"); break; case REF_STATUS_OK: print_ok_ref_status(ref); @@ -434,7 +445,6 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest hashcpy(ref->new_sha1, new_sha1); if (!ref->deletion) new_refs++; - ref->status = REF_STATUS_OK; if (!args.dry_run) { char *old_hex = sha1_to_hex(ref->old_sha1); @@ -451,6 +461,9 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest packet_write(out, "%s %s %s", old_hex, new_hex, ref->name); } + ref->status = expect_status_report ? + REF_STATUS_EXPECTING_REPORT : + REF_STATUS_OK; } packet_flush(out); @@ -462,11 +475,8 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest } close(out); - if (expect_status_report) { + if (expect_status_report) ret = receive_status(in, remote_refs); - if (ret == -2) - return -1; - } else ret = 0; diff --git a/cache.h b/cache.h index 8d601dd6f7..e0c1cc3fe8 100644 --- a/cache.h +++ b/cache.h @@ -504,8 +504,9 @@ struct ref { REF_STATUS_REJECT_NODELETE, REF_STATUS_UPTODATE, REF_STATUS_REMOTE_REJECT, + REF_STATUS_EXPECTING_REPORT, } status; - char *error; + char *remote_status; struct ref *peer_ref; /* when renaming */ char name[FLEX_ARRAY]; /* more */ }; From 73fa0b44ac1ed3b0b34ea6bcdca7d9b76886b903 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Sun, 18 Nov 2007 03:08:22 -0500 Subject: [PATCH 23/24] send-pack: fix "everything up-to-date" message This has always been slightly inaccurate, since it used the new_refs counter, which really meant "did we send any objects," so deletions were not counted. It has gotten even worse with recent patches, since we no longer look at the 'ret' value, meaning we would say "up to date" if non-ff pushes were rejected. Instead, we now claim up to date iff every ref is either unmatched or up to date. Any other case should already have generated a status line. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 14447bb7cd..3aab89ca1b 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -347,6 +347,20 @@ static void print_push_status(const char *dest, struct ref *refs) } } +static int refs_pushed(struct ref *ref) +{ + for (; ref; ref = ref->next) { + switch(ref->status) { + case REF_STATUS_NONE: + case REF_STATUS_UPTODATE: + break; + default: + return 1; + } + } + return 0; +} + static int do_send_pack(int in, int out, struct remote *remote, const char *dest, int nr_refspec, const char **refspec) { struct ref *ref; @@ -487,7 +501,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest update_tracking_ref(remote, ref); } - if (!new_refs) + if (!refs_pushed(remote_refs)) fprintf(stderr, "Everything up-to-date\n"); if (ret < 0) return ret; From 07f507155d01af0fdc8aa179b6a779301168b1eb Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 20 Nov 2007 06:18:01 -0500 Subject: [PATCH 24/24] send-pack: cluster ref status reporting Instead of intermingling success and failure, we now print: 1. all uptodate refs (if args.verbose is enabled) 2. successfully pushed refs 3. failed refs with the assumption that the user is most likely to see the ones at the end, and therefore we order them from "least interesting" to "most interesting." Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- builtin-send-pack.c | 95 ++++++++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 41 deletions(-) diff --git a/builtin-send-pack.c b/builtin-send-pack.c index 3aab89ca1b..25ae1fe860 100644 --- a/builtin-send-pack.c +++ b/builtin-send-pack.c @@ -298,52 +298,65 @@ static void print_ok_ref_status(struct ref *ref) } } +static int print_one_push_status(struct ref *ref, const char *dest, int count) +{ + if (!count) + fprintf(stderr, "To %s\n", dest); + + switch(ref->status) { + case REF_STATUS_NONE: + print_ref_status('X', "[no match]", ref, NULL, NULL); + break; + case REF_STATUS_REJECT_NODELETE: + print_ref_status('!', "[rejected]", ref, NULL, + "remote does not support deleting refs"); + break; + case REF_STATUS_UPTODATE: + print_ref_status('=', "[up to date]", ref, + ref->peer_ref, NULL); + break; + case REF_STATUS_REJECT_NONFASTFORWARD: + print_ref_status('!', "[rejected]", ref, ref->peer_ref, + "non-fast forward"); + break; + case REF_STATUS_REMOTE_REJECT: + print_ref_status('!', "[remote rejected]", ref, + ref->deletion ? NULL : ref->peer_ref, + ref->remote_status); + break; + case REF_STATUS_EXPECTING_REPORT: + print_ref_status('!', "[remote failure]", ref, + ref->deletion ? NULL : ref->peer_ref, + "remote failed to report status"); + break; + case REF_STATUS_OK: + print_ok_ref_status(ref); + break; + } + + return 1; +} + static void print_push_status(const char *dest, struct ref *refs) { struct ref *ref; - int shown_dest = 0; + int n = 0; + + if (args.verbose) { + for (ref = refs; ref; ref = ref->next) + if (ref->status == REF_STATUS_UPTODATE) + n += print_one_push_status(ref, dest, n); + } + + for (ref = refs; ref; ref = ref->next) + if (ref->status == REF_STATUS_OK) + n += print_one_push_status(ref, dest, n); for (ref = refs; ref; ref = ref->next) { - if (!ref->status) - continue; - if (ref->status == REF_STATUS_UPTODATE && !args.verbose) - continue; - - if (!shown_dest) { - fprintf(stderr, "To %s\n", dest); - shown_dest = 1; - } - - switch(ref->status) { - case REF_STATUS_NONE: - print_ref_status('X', "[no match]", ref, NULL, NULL); - break; - case REF_STATUS_REJECT_NODELETE: - print_ref_status('!', "[rejected]", ref, NULL, - "remote does not support deleting refs"); - break; - case REF_STATUS_UPTODATE: - print_ref_status('=', "[up to date]", ref, - ref->peer_ref, NULL); - break; - case REF_STATUS_REJECT_NONFASTFORWARD: - print_ref_status('!', "[rejected]", ref, ref->peer_ref, - "non-fast forward"); - break; - case REF_STATUS_REMOTE_REJECT: - print_ref_status('!', "[remote rejected]", ref, - ref->deletion ? NULL : ref->peer_ref, - ref->remote_status); - break; - case REF_STATUS_EXPECTING_REPORT: - print_ref_status('!', "[remote failure]", ref, - ref->deletion ? NULL : ref->peer_ref, - "remote failed to report status"); - break; - case REF_STATUS_OK: - print_ok_ref_status(ref); - break; - } + if (ref->status != REF_STATUS_NONE && + ref->status != REF_STATUS_UPTODATE && + ref->status != REF_STATUS_OK) + n += print_one_push_status(ref, dest, n); } }