From 7841ce79854868eaaa146c1d018b17fc4f3320be Mon Sep 17 00:00:00 2001 From: "Michael S. Tsirkin" Date: Wed, 16 May 2007 20:09:41 +0300 Subject: connect: display connection progress Make git notify the user about host resolution/connection attempts. This is useful both as a progress indicator on slow links, and helps reassure the user there are no firewall problems. Signed-off-by: Michael S. Tsirkin Acked-by: Linus Torvalds Signed-off-by: Junio C Hamano --- send-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index d5b51628d..83ee87dcf 100644 --- a/send-pack.c +++ b/send-pack.c @@ -393,7 +393,7 @@ int main(int argc, char **argv) usage(send_pack_usage); verify_remote_names(nr_heads, heads); - pid = git_connect(fd, dest, receivepack); + pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); if (pid < 0) return 1; ret = send_pack(fd[0], fd[1], nr_heads, heads); -- cgit v1.2.1 From 6b62816cb12e621c5952f088542bec6dfc7ec5d6 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Sat, 12 May 2007 11:45:59 -0400 Subject: Move refspec parser from connect.c and cache.h to remote.{c,h} Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- send-pack.c | 1 + 1 file changed, 1 insertion(+) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 83ee87dcf..3fe696cdd 100644 --- a/send-pack.c +++ b/send-pack.c @@ -4,6 +4,7 @@ #include "refs.h" #include "pkt-line.h" #include "run-command.h" +#include "remote.h" static const char send_pack_usage[] = "git-send-pack [--all] [--force] [--receive-pack=] [--verbose] [--thin] [:] [...]\n" -- cgit v1.2.1 From b516968ff62ec153e008d033c153affd7ba9ddc6 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Tue, 15 May 2007 22:50:19 -0400 Subject: Update local tracking refs when pushing This also adds a --remote option to send-pack, which specifies the configured remote being used. It is provided automatically by git-push, and must match the url (which is still needed, since there could be multiple urls). Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- send-pack.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 9 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 3fe696cdd..2c0b19ba3 100644 --- a/send-pack.c +++ b/send-pack.c @@ -177,7 +177,7 @@ static int receive_status(int in) return ret; } -static int send_pack(int in, int out, int nr_refspec, char **refspec) +static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec) { struct ref *ref; int new_refs; @@ -214,18 +214,19 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec) new_refs = 0; for (ref = remote_refs; ref; ref = ref->next) { char old_hex[60], *new_hex; - int delete_ref; + int will_delete_ref; if (!ref->peer_ref) continue; - delete_ref = is_null_sha1(ref->peer_ref->new_sha1); - if (delete_ref && !allow_deleting_refs) { + + 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"); ret = -2; continue; } - if (!delete_ref && + if (!will_delete_ref && !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { if (verbose) fprintf(stderr, "'%s': up-to-date\n", ref->name); @@ -252,7 +253,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec) */ if (!force_update && - !delete_ref && + !will_delete_ref && !is_null_sha1(ref->old_sha1) && !ref->force) { if (!has_sha1_file(ref->old_sha1) || @@ -276,7 +277,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec) } } hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); - if (!delete_ref) + if (!will_delete_ref) new_refs++; strcpy(old_hex, sha1_to_hex(ref->old_sha1)); new_hex = sha1_to_hex(ref->new_sha1); @@ -291,7 +292,7 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec) else packet_write(out, "%s %s %s", old_hex, new_hex, ref->name); - if (delete_ref) + if (will_delete_ref) fprintf(stderr, "deleting '%s'\n", ref->name); else { fprintf(stderr, "updating '%s'", ref->name); @@ -301,6 +302,28 @@ static int send_pack(int in, int out, int nr_refspec, char **refspec) fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); } + if (remote) { + struct refspec rs; + rs.src = ref->name; + remote_find_tracking(remote, &rs); + if (rs.dst) { + struct ref_lock *lock; + fprintf(stderr, " Also local %s\n", rs.dst); + if (will_delete_ref) { + if (delete_ref(rs.dst, NULL)) { + error("Failed to delete"); + } + } else { + lock = lock_any_ref_for_update(rs.dst, NULL, 0); + if (!lock) + error("Failed to lock"); + else + write_ref_sha1(lock, ref->new_sha1, + "update by push"); + } + free(rs.dst); + } + } } packet_flush(out); @@ -345,6 +368,8 @@ int main(int argc, char **argv) char **heads = NULL; int fd[2], ret; pid_t pid; + char *remote_name = NULL; + struct remote *remote = NULL; setup_git_directory(); git_config(git_default_config); @@ -362,6 +387,10 @@ int main(int argc, char **argv) receivepack = arg + 7; continue; } + if (!prefixcmp(arg, "--remote=")) { + remote_name = arg + 9; + continue; + } if (!strcmp(arg, "--all")) { send_all = 1; continue; @@ -394,10 +423,18 @@ int main(int argc, char **argv) usage(send_pack_usage); verify_remote_names(nr_heads, heads); + if (remote_name) { + remote = remote_get(remote_name); + if (!remote_has_uri(remote, dest)) { + die("Destination %s is not a uri for %s", + dest, remote_name); + } + } + pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); if (pid < 0) return 1; - ret = send_pack(fd[0], fd[1], nr_heads, heads); + ret = send_pack(fd[0], fd[1], remote, nr_heads, heads); close(fd[0]); close(fd[1]); ret |= finish_connect(pid); -- cgit v1.2.1 From 8558fd9ece4c8250a037a6d5482a8040d600ef47 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Fri, 25 May 2007 01:20:56 -0400 Subject: Move refspec pattern matching to match_refs(). This means that send-pack and http-push will support pattern refspecs, so builtin-push.c doesn't have to expand them, and also git push can just turn --tags into "refs/tags/*", further simplifying builtin-push.c check_ref_format() gets a third "conditionally okay" result for something that's valid as a pattern but not as a particular ref. Signed-off-by: Daniel Barkalow Signed-off-by: Junio C Hamano --- send-pack.c | 1 + 1 file changed, 1 insertion(+) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 2c0b19ba3..fecbda981 100644 --- a/send-pack.c +++ b/send-pack.c @@ -354,6 +354,7 @@ static void verify_remote_names(int nr_heads, char **heads) case -2: /* ok but a single level -- that is fine for * a match pattern. */ + case -3: /* ok but ends with a pattern-match character */ continue; } die("remote part of refspec is not a valid name in %s", -- cgit v1.2.1 From b42f69273bf5ae2d5bb0c7479bfa9827e7b351ac Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Tue, 10 Jul 2007 18:48:40 +0100 Subject: Add for_each_remote() function, and extend remote_find_tracking() The function for_each_remote() does exactly what the name suggests. The function remote_find_tracking() was extended to be able to search remote refs for a given local ref. The caller sets either src or dst (but not both) in the refspec parameter, and remote_find_tracking() will fill in the other and return 0. Both changes are required for the next step: simplification of git-branch's --track functionality. Signed-off-by: Johannes Schindelin Signed-off-by: Junio C Hamano --- send-pack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index fecbda981..9fc8a812f 100644 --- a/send-pack.c +++ b/send-pack.c @@ -305,8 +305,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha if (remote) { struct refspec rs; rs.src = ref->name; - remote_find_tracking(remote, &rs); - if (rs.dst) { + rs.dst = NULL; + if (!remote_find_tracking(remote, &rs)) { struct ref_lock *lock; fprintf(stderr, " Also local %s\n", rs.dst); if (will_delete_ref) { -- cgit v1.2.1 From 3d9f037c60ceae1bd60ee3c861564812a89b05b1 Mon Sep 17 00:00:00 2001 From: Carlos Rica Date: Wed, 5 Sep 2007 03:38:24 +0200 Subject: Function for updating refs. A function intended to be called from builtins updating refs by locking them before write, specially those that came from scripts using "git update-ref". [jc: with minor fixups] Signed-off-by: Carlos Rica Signed-off-by: Junio C Hamano --- send-pack.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 9fc8a812f..f74e66a8b 100644 --- a/send-pack.c +++ b/send-pack.c @@ -307,20 +307,14 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha rs.src = ref->name; rs.dst = NULL; if (!remote_find_tracking(remote, &rs)) { - struct ref_lock *lock; fprintf(stderr, " Also local %s\n", rs.dst); if (will_delete_ref) { if (delete_ref(rs.dst, NULL)) { error("Failed to delete"); } - } else { - lock = lock_any_ref_for_update(rs.dst, NULL, 0); - if (!lock) - error("Failed to lock"); - else - write_ref_sha1(lock, ref->new_sha1, - "update by push"); - } + } else + update_ref("update by push", rs.dst, + ref->new_sha1, NULL, 0, 0); free(rs.dst); } } -- cgit v1.2.1 From 28b91f8ad9e4791b5c35ca6bffbb78725b4e5bbf Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 19 Sep 2007 00:49:27 -0400 Subject: Rename remote.uri to remote.url within remote handling internals Anyplace we talk about the address of a remote repository we always refer to it as a URL, especially in the configuration file and .git/remotes where we call it "remote.$n.url" or start the first line with "URL:". Calling this value a uri within the internal C code just doesn't jive well with our commonly accepted terms. Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- send-pack.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index f74e66a8b..4533d1bf8 100644 --- a/send-pack.c +++ b/send-pack.c @@ -420,7 +420,7 @@ int main(int argc, char **argv) if (remote_name) { remote = remote_get(remote_name); - if (!remote_has_uri(remote, dest)) { + if (!remote_has_url(remote, dest)) { die("Destination %s is not a uri for %s", dest, remote_name); } -- cgit v1.2.1 From a63103ae4f02f8890d381de352dbfc6cba0b646f Mon Sep 17 00:00:00 2001 From: Brian Ewins Date: Thu, 11 Oct 2007 20:32:26 +0100 Subject: Add a --dry-run option to git-send-pack. Implement support for --dry-run, so that it can be used in calls from git-push. With this flag set, git-send-pack will not send any updates to the server. Signed-off-by: Brian Ewins Signed-off-by: Lars Hjemli Signed-off-by: Shawn O. Pearce --- send-pack.c | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index f74e66a8b..16ed51f6a 100644 --- a/send-pack.c +++ b/send-pack.c @@ -7,13 +7,14 @@ #include "remote.h" static const char send_pack_usage[] = -"git-send-pack [--all] [--force] [--receive-pack=] [--verbose] [--thin] [:] [...]\n" +"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; /* * Make a pack stream and spit it out into file descriptor fd @@ -282,16 +283,18 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha strcpy(old_hex, sha1_to_hex(ref->old_sha1)); 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, - "report-status"); - ask_for_status_report = 0; - expect_status_report = 1; + if (!dry_run) { + if (ask_for_status_report) { + packet_write(out, "%s %s %s%c%s", + old_hex, new_hex, ref->name, 0, + "report-status"); + ask_for_status_report = 0; + expect_status_report = 1; + } + else + packet_write(out, "%s %s %s", + old_hex, new_hex, ref->name); } - else - packet_write(out, "%s %s %s", - old_hex, new_hex, ref->name); if (will_delete_ref) fprintf(stderr, "deleting '%s'\n", ref->name); else { @@ -302,7 +305,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); } - if (remote) { + if (remote && !dry_run) { struct refspec rs; rs.src = ref->name; rs.dst = NULL; @@ -321,7 +324,7 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha } packet_flush(out); - if (new_refs) + if (new_refs && !dry_run) ret = pack_objects(out, remote_refs); close(out); @@ -390,6 +393,10 @@ int main(int argc, char **argv) send_all = 1; continue; } + if (!strcmp(arg, "--dry-run")) { + dry_run = 1; + continue; + } if (!strcmp(arg, "--force")) { force_update = 1; continue; -- cgit v1.2.1 From 7e23b06d5972ebf6377380b2dba97aeea9b7c02d Mon Sep 17 00:00:00 2001 From: Andrew Clausen Date: Tue, 16 Oct 2007 17:16:05 -0400 Subject: helpful error message when send-pack finds no refs in common. Signed-off-by: Andrew Clausen Signed-off-by: Shawn O. Pearce --- send-pack.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 9fc8a812f..fd985ed28 100644 --- a/send-pack.c +++ b/send-pack.c @@ -204,7 +204,8 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha return -1; if (!remote_refs) { - fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); + fprintf(stderr, "No refs in common and none specified; doing nothing.\n" + "Perhaps you should specify a branch such as 'master'.\n"); return 0; } -- cgit v1.2.1 From 334f4831e5a779d42e521b770a26eae1ecb27e86 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Thu, 18 Oct 2007 02:19:15 -0400 Subject: send-pack: don't update tracking refs on error Previously, we updated the tracking refs (which match refs we are pushing) while generating the list of refs to send. However, at that point we don't know whether the refs were accepted. Instead, we now wait until we get a response code from the server. If an error was indicated, we don't update any local tracking refs. Technically some refs could have been updated on the remote, but since the local ref update is just an optimization to avoid an extra fetch, we are better off erring on the side of correctness. The user-visible message is now generated much later in the program, and has been tweaked to make more sense. Signed-off-by: Jeff King Signed-off-by: Shawn O. Pearce --- send-pack.c | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index c1807f079..1a7397f3a 100644 --- a/send-pack.c +++ b/send-pack.c @@ -178,6 +178,35 @@ static int receive_status(int in) return ret; } +static void update_tracking_ref(struct remote *remote, struct ref *ref) +{ + struct refspec rs; + int will_delete_ref; + + 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)) { + 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"); + } else + update_ref("update by push", rs.dst, + ref->new_sha1, NULL, 0, 0); + free(rs.dst); + } +} + static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec) { struct ref *ref; @@ -306,22 +335,6 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); } - if (remote && !dry_run) { - struct refspec rs; - rs.src = ref->name; - rs.dst = NULL; - if (!remote_find_tracking(remote, &rs)) { - fprintf(stderr, " Also local %s\n", rs.dst); - if (will_delete_ref) { - if (delete_ref(rs.dst, NULL)) { - error("Failed to delete"); - } - } else - update_ref("update by push", rs.dst, - ref->new_sha1, NULL, 0, 0); - free(rs.dst); - } - } } packet_flush(out); @@ -334,6 +347,11 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha ret = -4; } + if (!dry_run && remote && ret == 0) { + for (ref = remote_refs; ref; ref = ref->next) + update_tracking_ref(remote, ref); + } + if (!new_refs && ret == 0) fprintf(stderr, "Everything up-to-date\n"); return ret; -- cgit v1.2.1 From 98158e9cfd2808613f305bf587ce697762c884bb Mon Sep 17 00:00:00 2001 From: Johannes Sixt Date: Fri, 19 Oct 2007 21:47:53 +0200 Subject: Change git_connect() to return a struct child_process instead of a pid_t. This prepares the API of git_connect() and finish_connect() to operate on a struct child_process. Currently, we just use that object as a placeholder for the pid that we used to return. A follow-up patch will change the implementation of git_connect() and finish_connect() to make full use of the object. Old code had early-return-on-error checks at the calling sites of git_connect(), but since git_connect() dies on errors anyway, these checks were removed. [sp: Corrected style nit of "conn == NULL" to "!conn"] Signed-off-by: Johannes Sixt Signed-off-by: Shawn O. Pearce --- send-pack.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 7b776243e..147f44b01 100644 --- a/send-pack.c +++ b/send-pack.c @@ -365,7 +365,7 @@ int main(int argc, char **argv) char *dest = NULL; char **heads = NULL; int fd[2], ret; - pid_t pid; + struct child_process *conn; char *remote_name = NULL; struct remote *remote = NULL; @@ -433,12 +433,10 @@ int main(int argc, char **argv) } } - pid = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); - if (pid < 0) - return 1; + conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); ret = send_pack(fd[0], fd[1], remote, nr_heads, heads); close(fd[0]); close(fd[1]); - ret |= finish_connect(pid); + ret |= finish_connect(conn); return !!ret; } -- cgit v1.2.1 From 4577370e9bfeca8652880b99b8499f76d18865ba Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 21:05:40 -0400 Subject: 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 --- send-pack.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c index 5e127a1b7..456fe4f12 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; } -- cgit v1.2.1 From 96249c04c07f00485a3ed1052ff10b27ab147fc5 Mon Sep 17 00:00:00 2001 From: Daniel Barkalow Date: Mon, 29 Oct 2007 22:03:39 -0400 Subject: 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 --- send-pack.c | 461 ------------------------------------------------------------ 1 file changed, 461 deletions(-) delete mode 100644 send-pack.c (limited to 'send-pack.c') diff --git a/send-pack.c b/send-pack.c deleted file mode 100644 index 456fe4f12..000000000 --- a/send-pack.c +++ /dev/null @@ -1,461 +0,0 @@ -#include "cache.h" -#include "commit.h" -#include "tag.h" -#include "refs.h" -#include "pkt-line.h" -#include "run-command.h" -#include "remote.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; - -/* - * Make a pack stream and spit it out into file descriptor fd - */ -static int pack_objects(int fd, struct ref *refs) -{ - /* - * The child becomes pack-objects --revs; we feed - * the revision parameters to it via its stdin and - * let its stdout go back to the other end. - */ - const char *args[] = { - "pack-objects", - "--all-progress", - "--revs", - "--stdout", - NULL, - NULL, - }; - struct child_process po; - - if (use_thin_pack) - args[4] = "--thin"; - memset(&po, 0, sizeof(po)); - po.argv = args; - po.in = -1; - po.out = fd; - po.git_cmd = 1; - if (start_command(&po)) - die("git-pack-objects failed (%s)", strerror(errno)); - - /* - * We feed the pack-objects we just spawned with revision - * parameters by writing to the pipe. - */ - while (refs) { - char buf[42]; - - if (!is_null_sha1(refs->old_sha1) && - has_sha1_file(refs->old_sha1)) { - memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40); - buf[0] = '^'; - buf[41] = '\n'; - if (!write_or_whine(po.in, buf, 42, - "send-pack: send refs")) - break; - } - if (!is_null_sha1(refs->new_sha1)) { - memcpy(buf, sha1_to_hex(refs->new_sha1), 40); - buf[40] = '\n'; - if (!write_or_whine(po.in, buf, 41, - "send-pack: send refs")) - break; - } - refs = refs->next; - } - - if (finish_command(&po)) - return error("pack-objects died with strange error"); - return 0; -} - -static void unmark_and_free(struct commit_list *list, unsigned int mark) -{ - while (list) { - struct commit_list *temp = list; - temp->item->object.flags &= ~mark; - list = temp->next; - free(temp); - } -} - -static int ref_newer(const unsigned char *new_sha1, - const unsigned char *old_sha1) -{ - struct object *o; - struct commit *old, *new; - struct commit_list *list, *used; - int found = 0; - - /* Both new and old must be commit-ish and new is descendant of - * old. Otherwise we require --force. - */ - o = deref_tag(parse_object(old_sha1), NULL, 0); - if (!o || o->type != OBJ_COMMIT) - return 0; - old = (struct commit *) o; - - o = deref_tag(parse_object(new_sha1), NULL, 0); - if (!o || o->type != OBJ_COMMIT) - return 0; - new = (struct commit *) o; - - if (parse_commit(new) < 0) - return 0; - - used = list = NULL; - commit_list_insert(new, &list); - while (list) { - new = pop_most_recent_commit(&list, 1); - commit_list_insert(new, &used); - if (new == old) { - found = 1; - break; - } - } - unmark_and_free(list, 1); - unmark_and_free(used, 1); - return found; -} - -static struct ref *local_refs, **local_tail; -static struct ref *remote_refs, **remote_tail; - -static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data) -{ - struct ref *ref; - int len = strlen(refname) + 1; - ref = xcalloc(1, sizeof(*ref) + len); - hashcpy(ref->new_sha1, sha1); - memcpy(ref->name, refname, len); - *local_tail = ref; - local_tail = &ref->next; - return 0; -} - -static void get_local_heads(void) -{ - local_tail = &local_refs; - for_each_ref(one_local_ref, NULL); -} - -static int receive_status(int in) -{ - 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; - } - if (memcmp(line, "unpack ok\n", 10)) { - fputs(line, stderr); - ret = -1; - } - while (1) { - len = packet_read_line(in, line, sizeof(line)); - if (!len) - break; - if (len < 3 || - (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) { - fprintf(stderr, "protocol error: %s\n", line); - ret = -1; - break; - } - if (!memcmp(line, "ok", 2)) - continue; - fputs(line, stderr); - ret = -1; - } - return ret; -} - -static void update_tracking_ref(struct remote *remote, struct ref *ref) -{ - struct refspec rs; - int will_delete_ref; - - 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)) { - 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"); - } else - update_ref("update by push", rs.dst, - ref->new_sha1, NULL, 0, 0); - free(rs.dst); - } -} - -static int send_pack(int in, int out, struct remote *remote, 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; - - /* No funny business with the matcher */ - remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL); - get_local_heads(); - - /* Does the other end support the reporting? */ - if (server_supports("report-status")) - ask_for_status_report = 1; - if (server_supports("delete-refs")) - allow_deleting_refs = 1; - - /* match them up */ - if (!remote_tail) - remote_tail = &remote_refs; - if (match_refs(local_refs, remote_refs, &remote_tail, - nr_refspec, refspec, send_all)) - return -1; - - if (!remote_refs) { - fprintf(stderr, "No refs in common and none specified; doing nothing.\n" - "Perhaps you should specify a branch such as 'master'.\n"); - return 0; - } - - /* - * Finally, tell the other end! - */ - new_refs = 0; - for (ref = remote_refs; ref; ref = ref->next) { - char old_hex[60], *new_hex; - int will_delete_ref; - - if (!ref->peer_ref) - continue; - - - 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"); - ret = -2; - continue; - } - if (!will_delete_ref && - !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) { - if (verbose) - fprintf(stderr, "'%s': up-to-date\n", ref->name); - continue; - } - - /* This part determines what can overwrite what. - * The rules are: - * - * (0) you can always use --force or +A:B notation to - * selectively force individual ref pairs. - * - * (1) if the old thing does not exist, it is OK. - * - * (2) if you do not have the old thing, you are not allowed - * to overwrite it; you would not know what you are losing - * otherwise. - * - * (3) if both new and old are commit-ish, and new is a - * descendant of old, it is OK. - * - * (4) regardless of all of the above, removing :B is - * always allowed. - */ - - if (!force_update && - !will_delete_ref && - !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)) { - /* 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. - */ - 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); - ret = -2; - continue; - } - } - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1); - if (!will_delete_ref) - new_refs++; - strcpy(old_hex, sha1_to_hex(ref->old_sha1)); - new_hex = sha1_to_hex(ref->new_sha1); - - if (!dry_run) { - if (ask_for_status_report) { - packet_write(out, "%s %s %s%c%s", - old_hex, new_hex, ref->name, 0, - "report-status"); - ask_for_status_report = 0; - expect_status_report = 1; - } - else - packet_write(out, "%s %s %s", - old_hex, new_hex, ref->name); - } - if (will_delete_ref) - fprintf(stderr, "deleting '%s'\n", ref->name); - 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); - } - } - - packet_flush(out); - if (new_refs && !dry_run) - ret = pack_objects(out, remote_refs); - close(out); - - if (expect_status_report) { - if (receive_status(in)) - ret = -4; - } - - if (!dry_run && remote && ret == 0) { - for (ref = remote_refs; ref; ref = ref->next) - update_tracking_ref(remote, ref); - } - - if (!new_refs && ret == 0) - fprintf(stderr, "Everything up-to-date\n"); - return ret; -} - -static void verify_remote_names(int nr_heads, const char **heads) -{ - int i; - - for (i = 0; i < nr_heads; i++) { - const char *remote = strchr(heads[i], ':'); - - remote = remote ? (remote + 1) : heads[i]; - switch (check_ref_format(remote)) { - case 0: /* ok */ - case -2: /* ok but a single level -- that is fine for - * a match pattern. - */ - case -3: /* ok but ends with a pattern-match character */ - continue; - } - die("remote part of refspec is not a valid name in %s", - heads[i]); - } -} - -int main(int argc, char **argv) -{ - int i, nr_heads = 0; - char *dest = NULL; - const char **heads = NULL; - int fd[2], ret; - struct child_process *conn; - char *remote_name = NULL; - struct remote *remote = NULL; - - setup_git_directory(); - git_config(git_default_config); - - argv++; - for (i = 1; i < argc; i++, argv++) { - char *arg = *argv; - - if (*arg == '-') { - if (!prefixcmp(arg, "--receive-pack=")) { - receivepack = arg + 15; - continue; - } - if (!prefixcmp(arg, "--exec=")) { - receivepack = arg + 7; - continue; - } - if (!prefixcmp(arg, "--remote=")) { - remote_name = arg + 9; - continue; - } - if (!strcmp(arg, "--all")) { - send_all = 1; - continue; - } - if (!strcmp(arg, "--dry-run")) { - dry_run = 1; - continue; - } - if (!strcmp(arg, "--force")) { - force_update = 1; - continue; - } - if (!strcmp(arg, "--verbose")) { - verbose = 1; - continue; - } - if (!strcmp(arg, "--thin")) { - use_thin_pack = 1; - continue; - } - usage(send_pack_usage); - } - if (!dest) { - dest = arg; - continue; - } - heads = (const char **) argv; - nr_heads = argc - i; - break; - } - if (!dest) - usage(send_pack_usage); - if (heads && send_all) - usage(send_pack_usage); - verify_remote_names(nr_heads, heads); - - if (remote_name) { - remote = remote_get(remote_name); - if (!remote_has_url(remote, dest)) { - die("Destination %s is not a uri for %s", - dest, remote_name); - } - } - - conn = git_connect(fd, dest, receivepack, verbose ? CONNECT_VERBOSE : 0); - ret = send_pack(fd[0], fd[1], remote, nr_heads, heads); - close(fd[0]); - close(fd[1]); - ret |= finish_connect(conn); - return !!ret; -} -- cgit v1.2.1