2 #include "object-store.h"
3 #include "promisor-remote.h"
7 static int fetch_refs(const char *remote_name
, struct ref
*ref
)
10 struct transport
*transport
;
11 int original_fetch_if_missing
= fetch_if_missing
;
15 remote
= remote_get(remote_name
);
17 die(_("Remote with no URL"));
18 transport
= transport_get(remote
, remote
->url
[0]);
20 transport_set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
21 transport_set_option(transport
, TRANS_OPT_NO_DEPENDENTS
, "1");
22 res
= transport_fetch_refs(transport
, ref
);
23 fetch_if_missing
= original_fetch_if_missing
;
28 static int fetch_objects(const char *remote_name
,
29 const struct object_id
*oids
,
32 struct ref
*ref
= NULL
;
35 for (i
= 0; i
< oid_nr
; i
++) {
36 struct ref
*new_ref
= alloc_ref(oid_to_hex(&oids
[i
]));
37 oidcpy(&new_ref
->old_oid
, &oids
[i
]);
38 new_ref
->exact_oid
= 1;
42 return fetch_refs(remote_name
, ref
);
45 static struct promisor_remote
*promisors
;
46 static struct promisor_remote
**promisors_tail
= &promisors
;
48 static struct promisor_remote
*promisor_remote_new(const char *remote_name
)
50 struct promisor_remote
*r
;
52 if (*remote_name
== '/') {
53 warning(_("promisor remote name cannot begin with '/': %s"),
58 FLEX_ALLOC_STR(r
, name
, remote_name
);
61 promisors_tail
= &r
->next
;
66 static struct promisor_remote
*promisor_remote_lookup(const char *remote_name
,
67 struct promisor_remote
**previous
)
69 struct promisor_remote
*r
, *p
;
71 for (p
= NULL
, r
= promisors
; r
; p
= r
, r
= r
->next
)
72 if (!strcmp(r
->name
, remote_name
)) {
81 static void promisor_remote_move_to_tail(struct promisor_remote
*r
,
82 struct promisor_remote
*previous
)
85 previous
->next
= r
->next
;
87 promisors
= r
->next
? r
->next
: r
;
90 promisors_tail
= &r
->next
;
93 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
99 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
102 if (!strcmp(subkey
, "promisor")) {
105 if (!git_config_bool(var
, value
))
108 remote_name
= xmemdupz(name
, namelen
);
110 if (!promisor_remote_lookup(remote_name
, NULL
))
111 promisor_remote_new(remote_name
);
116 if (!strcmp(subkey
, "partialclonefilter")) {
117 struct promisor_remote
*r
;
118 char *remote_name
= xmemdupz(name
, namelen
);
120 r
= promisor_remote_lookup(remote_name
, NULL
);
122 r
= promisor_remote_new(remote_name
);
129 return git_config_string(&r
->partial_clone_filter
, var
, value
);
135 static int initialized
;
137 static void promisor_remote_init(void)
143 git_config(promisor_remote_config
, NULL
);
145 if (repository_format_partial_clone
) {
146 struct promisor_remote
*o
, *previous
;
148 o
= promisor_remote_lookup(repository_format_partial_clone
,
151 promisor_remote_move_to_tail(o
, previous
);
153 promisor_remote_new(repository_format_partial_clone
);
157 static void promisor_remote_clear(void)
160 struct promisor_remote
*r
= promisors
;
161 promisors
= promisors
->next
;
165 promisors_tail
= &promisors
;
168 void promisor_remote_reinit(void)
171 promisor_remote_clear();
172 promisor_remote_init();
175 struct promisor_remote
*promisor_remote_find(const char *remote_name
)
177 promisor_remote_init();
182 return promisor_remote_lookup(remote_name
, NULL
);
185 int has_promisor_remote(void)
187 return !!promisor_remote_find(NULL
);
190 static int remove_fetched_oids(struct repository
*repo
,
191 struct object_id
**oids
,
192 int oid_nr
, int to_free
)
194 int i
, remaining_nr
= 0;
195 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
196 struct object_id
*old_oids
= *oids
;
197 struct object_id
*new_oids
;
199 for (i
= 0; i
< oid_nr
; i
++)
200 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
201 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
208 new_oids
= xcalloc(remaining_nr
, sizeof(*new_oids
));
209 for (i
= 0; i
< oid_nr
; i
++)
211 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
222 int promisor_remote_get_direct(struct repository
*repo
,
223 const struct object_id
*oids
,
226 struct promisor_remote
*r
;
227 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
228 int remaining_nr
= oid_nr
;
232 promisor_remote_init();
234 for (r
= promisors
; r
; r
= r
->next
) {
235 if (fetch_objects(r
->name
, remaining_oids
, remaining_nr
) < 0) {
236 if (remaining_nr
== 1)
238 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
239 remaining_nr
, to_free
);
250 free(remaining_oids
);