@matheusbsbs / matheus-bagliano / commits / 28b9d6e548
Fork 0

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 <apw@shadowen.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Andy Whitcroft committed Nov 9, 2007 at 23:32 UTC 28b9d6e548322755bbdb24c28a493862f61b1eba
5 files changed +54 -19
builtin-send-pack.c
+34 -12
@@ -8,7 +8,7 @@
8 #include "send-pack.h"
9
10 static const char send_pack_usage[] =
11 -"git-send-pack [--all] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
11 +"git-send-pack [--all | --mirror] [--dry-run] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
12 " --all and explicit <ref> specification are mutually exclusive.";
13
14 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
227 int allow_deleting_refs = 0;
228 int expect_status_report = 0;
229 int shown_dest = 0;
230 + int flags = MATCH_REFS_NONE;
231 +
232 + if (args.send_all)
233 + flags |= MATCH_REFS_ALL;
234 + if (args.send_mirror)
235 + flags |= MATCH_REFS_MIRROR;
236
237 /* No funny business with the matcher */
238 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
248 if (!remote_tail)
249 remote_tail = &remote_refs;
250 if (match_refs(local_refs, remote_refs, &remote_tail,
245 - nr_refspec, refspec, args.send_all))
251 + nr_refspec, refspec, flags))
252 return -1;
253
254 if (!remote_refs) {
@@ -259,20 +265,28 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
265 char old_hex[60], *new_hex;
266 int will_delete_ref;
267 const char *pretty_ref;
262 - const char *pretty_peer;
268 + const char *pretty_peer = NULL; /* only used when not deleting */
269 + const unsigned char *new_sha1;
270
264 - if (!ref->peer_ref)
265 - continue;
271 + if (!ref->peer_ref) {
272 + if (!args.send_mirror)
273 + continue;
274 + new_sha1 = null_sha1;
275 + }
276 + else
277 + new_sha1 = ref->peer_ref->new_sha1;
278
279 if (!shown_dest) {
280 fprintf(stderr, "To %s\n", dest);
281 shown_dest = 1;
282 }
283
284 + will_delete_ref = is_null_sha1(new_sha1);
285 +
286 pretty_ref = prettify_ref(ref->name);
273 - pretty_peer = prettify_ref(ref->peer_ref->name);
287 + if (!will_delete_ref)
288 + pretty_peer = prettify_ref(ref->peer_ref->name);
289
275 - will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
290 if (will_delete_ref && !allow_deleting_refs) {
291 fprintf(stderr, " ! %-*s %s (remote does not support deleting refs)\n",
292 SUMMARY_WIDTH, "[rejected]", pretty_ref);
@@ -280,7 +294,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
294 continue;
295 }
296 if (!will_delete_ref &&
283 - !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
297 + !hashcmp(ref->old_sha1, new_sha1)) {
298 if (args.verbose)
299 fprintf(stderr, " = %-*s %s -> %s\n",
300 SUMMARY_WIDTH, "[up to date]",
@@ -312,8 +326,7 @@ static int do_send_pack(int in, int out, struct remote *remote, const char *dest
326 !is_null_sha1(ref->old_sha1) &&
327 !ref->force) {
328 if (!has_sha1_file(ref->old_sha1) ||
315 - !ref_newer(ref->peer_ref->new_sha1,
316 - ref->old_sha1)) {
329 + !ref_newer(new_sha1, ref->old_sha1)) {
330 /* We do not have the remote ref, or
331 * we know that the remote ref is not
332 * 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
341 continue;
342 }
343 }
331 - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
344 + hashcpy(ref->new_sha1, new_sha1);
345 if (!will_delete_ref)
346 new_refs++;
347 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)
472 args.dry_run = 1;
473 continue;
474 }
475 + if (!strcmp(arg, "--mirror")) {
476 + args.send_mirror = 1;
477 + continue;
478 + }
479 if (!strcmp(arg, "--force")) {
480 args.force_update = 1;
481 continue;
@@ -483,7 +500,12 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
500 }
501 if (!dest)
502 usage(send_pack_usage);
486 - if (heads && args.send_all)
503 + /*
504 + * --all and --mirror are incompatible; neither makes sense
505 + * with any refspecs.
506 + */
507 + if ((heads && (args.send_all || args.send_mirror)) ||
508 + (args.send_all && args.send_mirror))
509 usage(send_pack_usage);
510
511 if (remote_name) {
http-push.c
+2 -2
@@ -78,7 +78,7 @@ static struct curl_slist *no_pragma_header;
78 static struct curl_slist *default_headers;
79
80 static int push_verbosely;
81 -static int push_all;
81 +static int push_all = MATCH_REFS_NONE;
82 static int force_all;
83 static int dry_run;
84
@@ -2300,7 +2300,7 @@ int main(int argc, char **argv)
2300
2301 if (*arg == '-') {
2302 if (!strcmp(arg, "--all")) {
2303 - push_all = 1;
2303 + push_all = MATCH_REFS_ALL;
2304 continue;
2305 }
2306 if (!strcmp(arg, "--force")) {
remote.c
+10 -5
@@ -722,10 +722,12 @@ static const struct refspec *check_pattern_match(const struct refspec *rs,
722 * without thinking.
723 */
724 int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
725 - int nr_refspec, const char **refspec, int all)
725 + int nr_refspec, const char **refspec, int flags)
726 {
727 struct refspec *rs =
728 parse_ref_spec(nr_refspec, (const char **) refspec);
729 + int send_all = flags & MATCH_REFS_ALL;
730 + int send_mirror = flags & MATCH_REFS_MIRROR;
731
732 if (match_explicit_refs(src, dst, dst_tail, rs, nr_refspec))
733 return -1;
@@ -742,7 +744,7 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
744 if (!pat)
745 continue;
746 }
745 - else if (prefixcmp(src->name, "refs/heads/"))
747 + else if (!send_mirror && prefixcmp(src->name, "refs/heads/"))
748 /*
749 * "matching refs"; traditionally we pushed everything
750 * including refs outside refs/heads/ hierarchy, but
@@ -763,10 +765,13 @@ int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail,
765 if (dst_peer && dst_peer->peer_ref)
766 /* We're already sending something to this ref. */
767 goto free_name;
766 - if (!dst_peer && !nr_refspec && !all)
767 - /* Remote doesn't have it, and we have no
768 +
769 + if (!dst_peer && !nr_refspec && !(send_all || send_mirror))
770 + /*
771 + * Remote doesn't have it, and we have no
772 * explicit pattern, and we don't have
769 - * --all. */
773 + * --all nor --mirror.
774 + */
775 goto free_name;
776 if (!dst_peer) {
777 /* Create a new one and link it */
remote.h
+7
@@ -102,4 +102,11 @@ struct branch *branch_get(const char *name);
102 int branch_has_merge_config(struct branch *branch);
103 int branch_merge_matches(struct branch *, int n, const char *);
104
105 +/* Flags to match_refs. */
106 +enum match_refs_flags {
107 + MATCH_REFS_NONE = 0,
108 + MATCH_REFS_ALL = (1 << 0),
109 + MATCH_REFS_MIRROR = (1 << 1),
110 +};
111 +
112 #endif
send-pack.h
+1
@@ -5,6 +5,7 @@ struct send_pack_args {
5 const char *receivepack;
6 unsigned verbose:1,
7 send_all:1,
8 + send_mirror:1,
9 force_update:1,
10 use_thin_pack:1,
11 dry_run:1;