promisor-remote: implement promisor_remote_get_direct()
authorChristian Couder <christian.couder@gmail.com>
Tue, 25 Jun 2019 13:40:28 +0000 (15:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Jun 2019 21:05:37 +0000 (14:05 -0700)
This is implemented for now by calling fetch_objects(). It fetches
from all the promisor remotes.

Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
Helped-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
promisor-remote.c
promisor-remote.h
index c249b80e02022c66544889486d64d63956583d88..b79a84ce3a5c0f46a0c1bbe82ba96514d18dbd63 100644 (file)
@@ -1,6 +1,8 @@
 #include "cache.h"
+#include "object-store.h"
 #include "promisor-remote.h"
 #include "config.h"
+#include "fetch-object.h"
 
 static struct promisor_remote *promisors;
 static struct promisor_remote **promisors_tail = &promisors;
@@ -90,3 +92,68 @@ int has_promisor_remote(void)
 {
        return !!promisor_remote_find(NULL);
 }
+
+static int remove_fetched_oids(struct repository *repo,
+                              struct object_id **oids,
+                              int oid_nr, int to_free)
+{
+       int i, remaining_nr = 0;
+       int *remaining = xcalloc(oid_nr, sizeof(*remaining));
+       struct object_id *old_oids = *oids;
+       struct object_id *new_oids;
+
+       for (i = 0; i < oid_nr; i++)
+               if (oid_object_info_extended(repo, &old_oids[i], NULL,
+                                            OBJECT_INFO_SKIP_FETCH_OBJECT)) {
+                       remaining[i] = 1;
+                       remaining_nr++;
+               }
+
+       if (remaining_nr) {
+               int j = 0;
+               new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
+               for (i = 0; i < oid_nr; i++)
+                       if (remaining[i])
+                               oidcpy(&new_oids[j++], &old_oids[i]);
+               *oids = new_oids;
+               if (to_free)
+                       free(old_oids);
+       }
+
+       free(remaining);
+
+       return remaining_nr;
+}
+
+int promisor_remote_get_direct(struct repository *repo,
+                              const struct object_id *oids,
+                              int oid_nr)
+{
+       struct promisor_remote *r;
+       struct object_id *remaining_oids = (struct object_id *)oids;
+       int remaining_nr = oid_nr;
+       int to_free = 0;
+       int res = -1;
+
+       promisor_remote_init();
+
+       for (r = promisors; r; r = r->next) {
+               if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
+                       if (remaining_nr == 1)
+                               continue;
+                       remaining_nr = remove_fetched_oids(repo, &remaining_oids,
+                                                        remaining_nr, to_free);
+                       if (remaining_nr) {
+                               to_free = 1;
+                               continue;
+                       }
+               }
+               res = 0;
+               break;
+       }
+
+       if (to_free)
+               free(remaining_oids);
+
+       return res;
+}
index 01dcdf4dc72c2084bfc3cd2744405b66a64dd864..ed4ecead36f5a37ad65918c4b3fb489eaaa44e14 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef PROMISOR_REMOTE_H
 #define PROMISOR_REMOTE_H
 
+struct object_id;
+
 /*
  * Promisor remote linked list
  * Its information come from remote.XXX config entries.
@@ -12,5 +14,8 @@ struct promisor_remote {
 
 extern struct promisor_remote *promisor_remote_find(const char *remote_name);
 extern int has_promisor_remote(void);
+extern int promisor_remote_get_direct(struct repository *repo,
+                                     const struct object_id *oids,
+                                     int oid_nr);
 
 #endif /* PROMISOR_REMOTE_H */