@matheusbsbs / matheus-bagliano / commits / d79796bcf0
Fork 0

push: receiver end advertises refs from alternate repositories

Earlier, when pushing into a repository that borrows from alternate object stores, we followed the longstanding design decision not to trust refs in the alternate repository that houses the object store we are borrowing from. If your public repository is borrowing from Linus's public repository, you pushed into it long time ago, and now when you try to push your updated history that is in sync with more recent history from Linus, you will end up sending not just your own development, but also the changes you acquired through Linus's tree, even though the objects needed for the latter already exists at the receiving end. This is because the receiving end does not advertise that the objects only reachable from the borrowed repository (i.e. Linus's) are already available there. This solves the issue by making the receiving end advertise refs from borrowed repositories. They are not sent with their true names but with a phoney name ".have" to make sure that the old senders will safely ignore them (otherwise, the old senders will misbehave, trying to push matching refs, and mirror push that deletes refs that only exist at the receiving end). Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Sep 9, 2008 at 01:27 UTC d79796bcf05b89774671a75b3285000c43129823
3 files changed +50
builtin-receive-pack.c
+38
@@ -6,6 +6,8 @@
6 #include "exec_cmd.h"
7 #include "commit.h"
8 #include "object.h"
9 +#include "remote.h"
10 +#include "transport.h"
11
12 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
13
@@ -462,6 +464,40 @@ static int delete_only(struct command *cmd)
464 return 1;
465 }
466
467 +static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
468 +{
469 + char *other = xstrdup(make_absolute_path(e->base));
470 + size_t len = strlen(other);
471 + struct remote *remote;
472 + struct transport *transport;
473 + const struct ref *extra;
474 +
475 + while (other[len-1] == '/')
476 + other[--len] = '\0';
477 + if (len < 8 || memcmp(other + len - 8, "/objects", 8))
478 + return 0;
479 + /* Is this a git repository with refs? */
480 + memcpy(other + len - 8, "/refs", 6);
481 + if (!is_directory(other))
482 + return 0;
483 + other[len - 8] = '\0';
484 + remote = remote_get(other);
485 + transport = transport_get(remote, other);
486 + for (extra = transport_get_remote_refs(transport);
487 + extra;
488 + extra = extra->next) {
489 + add_extra_ref(".have", extra->old_sha1, 0);
490 + }
491 + transport_disconnect(transport);
492 + free(other);
493 + return 0;
494 +}
495 +
496 +static void add_alternate_refs(void)
497 +{
498 + foreach_alt_odb(add_refs_from_alternate, NULL);
499 +}
500 +
501 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
502 {
503 int i;
@@ -497,7 +533,9 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix)
533 else if (0 <= receive_unpack_limit)
534 unpack_limit = receive_unpack_limit;
535
536 + add_alternate_refs();
537 write_head_info();
538 + clear_extra_refs();
539
540 /* EOF */
541 packet_flush(1);
cache.h
+2
@@ -641,6 +641,8 @@ extern struct alternate_object_database {
641 } *alt_odb_list;
642 extern void prepare_alt_odb(void);
643 extern void add_to_alternates_file(const char *reference);
644 +typedef int alt_odb_fn(struct alternate_object_database *, void *);
645 +extern void foreach_alt_odb(alt_odb_fn, void*);
646
647 struct pack_window {
648 struct pack_window *next;
sha1_file.c
+10
@@ -393,6 +393,16 @@ void add_to_alternates_file(const char *reference)
393 link_alt_odb_entries(alt, alt + strlen(alt), '\n', NULL, 0);
394 }
395
396 +void foreach_alt_odb(alt_odb_fn fn, void *cb)
397 +{
398 + struct alternate_object_database *ent;
399 +
400 + prepare_alt_odb();
401 + for (ent = alt_odb_list; ent; ent = ent->next)
402 + if (fn(ent, cb))
403 + return;
404 +}
405 +
406 void prepare_alt_odb(void)
407 {
408 const char *alt;