2 #include "object-store.h"
3 #include "promisor-remote.h"
9 struct promisor_remote_config
{
10 struct promisor_remote
*promisors
;
11 struct promisor_remote
**promisors_tail
;
14 static int fetch_objects(struct repository
*repo
,
15 const char *remote_name
,
16 const struct object_id
*oids
,
19 struct child_process child
= CHILD_PROCESS_INIT
;
25 if (repo
!= the_repository
)
26 prepare_other_repo_env(&child
.env
, repo
->gitdir
);
27 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
28 "fetch", remote_name
, "--no-tags",
29 "--no-write-fetch-head", "--recurse-submodules=no",
30 "--filter=blob:none", "--stdin", NULL
);
31 if (start_command(&child
))
32 die(_("promisor-remote: unable to fork off fetch subprocess"));
33 child_in
= xfdopen(child
.in
, "w");
35 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
37 for (i
= 0; i
< oid_nr
; i
++) {
38 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
39 die_errno(_("promisor-remote: could not write to fetch subprocess"));
40 if (fputc('\n', child_in
) < 0)
41 die_errno(_("promisor-remote: could not write to fetch subprocess"));
44 if (fclose(child_in
) < 0)
45 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
46 return finish_command(&child
) ? -1 : 0;
49 static struct promisor_remote
*promisor_remote_new(struct promisor_remote_config
*config
,
50 const char *remote_name
)
52 struct promisor_remote
*r
;
54 if (*remote_name
== '/') {
55 warning(_("promisor remote name cannot begin with '/': %s"),
60 FLEX_ALLOC_STR(r
, name
, remote_name
);
62 *config
->promisors_tail
= r
;
63 config
->promisors_tail
= &r
->next
;
68 static struct promisor_remote
*promisor_remote_lookup(struct promisor_remote_config
*config
,
69 const char *remote_name
,
70 struct promisor_remote
**previous
)
72 struct promisor_remote
*r
, *p
;
74 for (p
= NULL
, r
= config
->promisors
; r
; p
= r
, r
= r
->next
)
75 if (!strcmp(r
->name
, remote_name
)) {
84 static void promisor_remote_move_to_tail(struct promisor_remote_config
*config
,
85 struct promisor_remote
*r
,
86 struct promisor_remote
*previous
)
92 previous
->next
= r
->next
;
94 config
->promisors
= r
->next
? r
->next
: r
;
96 *config
->promisors_tail
= r
;
97 config
->promisors_tail
= &r
->next
;
100 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
102 struct promisor_remote_config
*config
= data
;
107 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
110 if (!strcmp(subkey
, "promisor")) {
113 if (!git_config_bool(var
, value
))
116 remote_name
= xmemdupz(name
, namelen
);
118 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
119 promisor_remote_new(config
, remote_name
);
124 if (!strcmp(subkey
, "partialclonefilter")) {
125 struct promisor_remote
*r
;
126 char *remote_name
= xmemdupz(name
, namelen
);
128 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
130 r
= promisor_remote_new(config
, remote_name
);
137 return git_config_string(&r
->partial_clone_filter
, var
, value
);
143 static void promisor_remote_init(struct repository
*r
)
145 struct promisor_remote_config
*config
;
147 if (r
->promisor_remote_config
)
149 config
= r
->promisor_remote_config
=
150 xcalloc(1, sizeof(*r
->promisor_remote_config
));
151 config
->promisors_tail
= &config
->promisors
;
153 repo_config(r
, promisor_remote_config
, config
);
155 if (r
->repository_format_partial_clone
) {
156 struct promisor_remote
*o
, *previous
;
158 o
= promisor_remote_lookup(config
,
159 r
->repository_format_partial_clone
,
162 promisor_remote_move_to_tail(config
, o
, previous
);
164 promisor_remote_new(config
, r
->repository_format_partial_clone
);
168 void promisor_remote_clear(struct promisor_remote_config
*config
)
170 while (config
->promisors
) {
171 struct promisor_remote
*r
= config
->promisors
;
172 config
->promisors
= config
->promisors
->next
;
176 config
->promisors_tail
= &config
->promisors
;
179 void repo_promisor_remote_reinit(struct repository
*r
)
181 promisor_remote_clear(r
->promisor_remote_config
);
182 FREE_AND_NULL(r
->promisor_remote_config
);
183 promisor_remote_init(r
);
186 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
187 const char *remote_name
)
189 promisor_remote_init(r
);
192 return r
->promisor_remote_config
->promisors
;
194 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
197 int repo_has_promisor_remote(struct repository
*r
)
199 return !!repo_promisor_remote_find(r
, 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 CALLOC_ARRAY(new_oids
, remaining_nr
);
221 for (i
= 0; i
< oid_nr
; i
++)
223 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
234 void 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(repo
);
249 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
250 if (fetch_objects(repo
, 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
);
263 for (i
= 0; i
< remaining_nr
; i
++) {
264 if (is_promisor_object(&remaining_oids
[i
]))
265 die(_("could not fetch %s from promisor remote"),
266 oid_to_hex(&remaining_oids
[i
]));
271 free(remaining_oids
);