2 #include "object-store.h"
3 #include "promisor-remote.h"
5 #include "fetch-object.h"
7 static struct promisor_remote
*promisors
;
8 static struct promisor_remote
**promisors_tail
= &promisors
;
10 static struct promisor_remote
*promisor_remote_new(const char *remote_name
)
12 struct promisor_remote
*r
;
14 if (*remote_name
== '/') {
15 warning(_("promisor remote name cannot begin with '/': %s"),
20 FLEX_ALLOC_STR(r
, name
, remote_name
);
23 promisors_tail
= &r
->next
;
28 static struct promisor_remote
*promisor_remote_lookup(const char *remote_name
,
29 struct promisor_remote
**previous
)
31 struct promisor_remote
*r
, *p
;
33 for (p
= NULL
, r
= promisors
; r
; p
= r
, r
= r
->next
)
34 if (!strcmp(r
->name
, remote_name
)) {
43 static void promisor_remote_move_to_tail(struct promisor_remote
*r
,
44 struct promisor_remote
*previous
)
47 previous
->next
= r
->next
;
49 promisors
= r
->next
? r
->next
: r
;
52 promisors_tail
= &r
->next
;
55 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
61 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
64 if (!strcmp(subkey
, "promisor")) {
67 if (!git_config_bool(var
, value
))
70 remote_name
= xmemdupz(name
, namelen
);
72 if (!promisor_remote_lookup(remote_name
, NULL
))
73 promisor_remote_new(remote_name
);
78 if (!strcmp(subkey
, "partialclonefilter")) {
79 struct promisor_remote
*r
;
80 char *remote_name
= xmemdupz(name
, namelen
);
82 r
= promisor_remote_lookup(remote_name
, NULL
);
84 r
= promisor_remote_new(remote_name
);
91 return git_config_string(&r
->partial_clone_filter
, var
, value
);
97 static int initialized
;
99 static void promisor_remote_init(void)
105 git_config(promisor_remote_config
, NULL
);
107 if (repository_format_partial_clone
) {
108 struct promisor_remote
*o
, *previous
;
110 o
= promisor_remote_lookup(repository_format_partial_clone
,
113 promisor_remote_move_to_tail(o
, previous
);
115 promisor_remote_new(repository_format_partial_clone
);
119 static void promisor_remote_clear(void)
122 struct promisor_remote
*r
= promisors
;
123 promisors
= promisors
->next
;
127 promisors_tail
= &promisors
;
130 void promisor_remote_reinit(void)
133 promisor_remote_clear();
134 promisor_remote_init();
137 struct promisor_remote
*promisor_remote_find(const char *remote_name
)
139 promisor_remote_init();
144 return promisor_remote_lookup(remote_name
, NULL
);
147 int has_promisor_remote(void)
149 return !!promisor_remote_find(NULL
);
152 static int remove_fetched_oids(struct repository
*repo
,
153 struct object_id
**oids
,
154 int oid_nr
, int to_free
)
156 int i
, remaining_nr
= 0;
157 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
158 struct object_id
*old_oids
= *oids
;
159 struct object_id
*new_oids
;
161 for (i
= 0; i
< oid_nr
; i
++)
162 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
163 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
170 new_oids
= xcalloc(remaining_nr
, sizeof(*new_oids
));
171 for (i
= 0; i
< oid_nr
; i
++)
173 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
184 int promisor_remote_get_direct(struct repository
*repo
,
185 const struct object_id
*oids
,
188 struct promisor_remote
*r
;
189 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
190 int remaining_nr
= oid_nr
;
194 promisor_remote_init();
196 for (r
= promisors
; r
; r
= r
->next
) {
197 if (fetch_objects(r
->name
, remaining_oids
, remaining_nr
) < 0) {
198 if (remaining_nr
== 1)
200 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
201 remaining_nr
, to_free
);
212 free(remaining_oids
);