2 #include "object-store.h"
3 #include "promisor-remote.h"
7 static char *repository_format_partial_clone
;
8 static const char *core_partial_clone_filter_default
;
10 void set_repository_format_partial_clone(char *partial_clone
)
12 repository_format_partial_clone
= xstrdup_or_null(partial_clone
);
15 static int fetch_refs(const char *remote_name
, struct ref
*ref
)
17 struct remote
*remote
;
18 struct transport
*transport
;
21 remote
= remote_get(remote_name
);
23 die(_("Remote with no URL"));
24 transport
= transport_get(remote
, remote
->url
[0]);
26 transport_set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
27 transport_set_option(transport
, TRANS_OPT_NO_DEPENDENTS
, "1");
28 res
= transport_fetch_refs(transport
, ref
);
33 static int fetch_objects(const char *remote_name
,
34 const struct object_id
*oids
,
37 struct ref
*ref
= NULL
;
40 for (i
= 0; i
< oid_nr
; i
++) {
41 struct ref
*new_ref
= alloc_ref(oid_to_hex(&oids
[i
]));
42 oidcpy(&new_ref
->old_oid
, &oids
[i
]);
43 new_ref
->exact_oid
= 1;
47 return fetch_refs(remote_name
, ref
);
50 static struct promisor_remote
*promisors
;
51 static struct promisor_remote
**promisors_tail
= &promisors
;
53 static struct promisor_remote
*promisor_remote_new(const char *remote_name
)
55 struct promisor_remote
*r
;
57 if (*remote_name
== '/') {
58 warning(_("promisor remote name cannot begin with '/': %s"),
63 FLEX_ALLOC_STR(r
, name
, remote_name
);
66 promisors_tail
= &r
->next
;
71 static struct promisor_remote
*promisor_remote_lookup(const char *remote_name
,
72 struct promisor_remote
**previous
)
74 struct promisor_remote
*r
, *p
;
76 for (p
= NULL
, r
= promisors
; r
; p
= r
, r
= r
->next
)
77 if (!strcmp(r
->name
, remote_name
)) {
86 static void promisor_remote_move_to_tail(struct promisor_remote
*r
,
87 struct promisor_remote
*previous
)
93 previous
->next
= r
->next
;
95 promisors
= r
->next
? r
->next
: r
;
98 promisors_tail
= &r
->next
;
101 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
107 if (!strcmp(var
, "core.partialclonefilter"))
108 return git_config_string(&core_partial_clone_filter_default
,
111 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
114 if (!strcmp(subkey
, "promisor")) {
117 if (!git_config_bool(var
, value
))
120 remote_name
= xmemdupz(name
, namelen
);
122 if (!promisor_remote_lookup(remote_name
, NULL
))
123 promisor_remote_new(remote_name
);
128 if (!strcmp(subkey
, "partialclonefilter")) {
129 struct promisor_remote
*r
;
130 char *remote_name
= xmemdupz(name
, namelen
);
132 r
= promisor_remote_lookup(remote_name
, NULL
);
134 r
= promisor_remote_new(remote_name
);
141 return git_config_string(&r
->partial_clone_filter
, var
, value
);
147 static int initialized
;
149 static void promisor_remote_init(void)
155 git_config(promisor_remote_config
, NULL
);
157 if (repository_format_partial_clone
) {
158 struct promisor_remote
*o
, *previous
;
160 o
= promisor_remote_lookup(repository_format_partial_clone
,
163 promisor_remote_move_to_tail(o
, previous
);
165 promisor_remote_new(repository_format_partial_clone
);
169 static void promisor_remote_clear(void)
172 struct promisor_remote
*r
= promisors
;
173 promisors
= promisors
->next
;
177 promisors_tail
= &promisors
;
180 void promisor_remote_reinit(void)
183 promisor_remote_clear();
184 promisor_remote_init();
187 struct promisor_remote
*promisor_remote_find(const char *remote_name
)
189 promisor_remote_init();
194 return promisor_remote_lookup(remote_name
, NULL
);
197 int has_promisor_remote(void)
199 return !!promisor_remote_find(NULL
);
202 static int remove_fetched_oids(struct repository
*repo
,
203 struct object_id
**oids
,
204 int oid_nr
, int to_free
)
206 int i
, remaining_nr
= 0;
207 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
208 struct object_id
*old_oids
= *oids
;
209 struct object_id
*new_oids
;
211 for (i
= 0; i
< oid_nr
; i
++)
212 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
213 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
220 new_oids
= xcalloc(remaining_nr
, sizeof(*new_oids
));
221 for (i
= 0; i
< oid_nr
; i
++)
223 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
234 int promisor_remote_get_direct(struct repository
*repo
,
235 const struct object_id
*oids
,
238 struct promisor_remote
*r
;
239 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
240 int remaining_nr
= oid_nr
;
247 promisor_remote_init();
249 for (r
= promisors
; r
; r
= r
->next
) {
250 if (fetch_objects(r
->name
, remaining_oids
, remaining_nr
) < 0) {
251 if (remaining_nr
== 1)
253 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
254 remaining_nr
, to_free
);
265 free(remaining_oids
);