1 #include "git-compat-util.h"
4 #include "object-store.h"
5 #include "promisor-remote.h"
12 struct promisor_remote_config
{
13 struct promisor_remote
*promisors
;
14 struct promisor_remote
**promisors_tail
;
17 static int fetch_objects(struct repository
*repo
,
18 const char *remote_name
,
19 const struct object_id
*oids
,
22 struct child_process child
= CHILD_PROCESS_INIT
;
28 if (repo
!= the_repository
)
29 prepare_other_repo_env(&child
.env
, repo
->gitdir
);
30 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
31 "fetch", remote_name
, "--no-tags",
32 "--no-write-fetch-head", "--recurse-submodules=no",
33 "--filter=blob:none", "--stdin", NULL
);
34 if (start_command(&child
))
35 die(_("promisor-remote: unable to fork off fetch subprocess"));
36 child_in
= xfdopen(child
.in
, "w");
38 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
40 for (i
= 0; i
< oid_nr
; i
++) {
41 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
42 die_errno(_("promisor-remote: could not write to fetch subprocess"));
43 if (fputc('\n', child_in
) < 0)
44 die_errno(_("promisor-remote: could not write to fetch subprocess"));
47 if (fclose(child_in
) < 0)
48 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
49 return finish_command(&child
) ? -1 : 0;
52 static struct promisor_remote
*promisor_remote_new(struct promisor_remote_config
*config
,
53 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
);
65 *config
->promisors_tail
= r
;
66 config
->promisors_tail
= &r
->next
;
71 static struct promisor_remote
*promisor_remote_lookup(struct promisor_remote_config
*config
,
72 const char *remote_name
,
73 struct promisor_remote
**previous
)
75 struct promisor_remote
*r
, *p
;
77 for (p
= NULL
, r
= config
->promisors
; r
; p
= r
, r
= r
->next
)
78 if (!strcmp(r
->name
, remote_name
)) {
87 static void promisor_remote_move_to_tail(struct promisor_remote_config
*config
,
88 struct promisor_remote
*r
,
89 struct promisor_remote
*previous
)
95 previous
->next
= r
->next
;
97 config
->promisors
= r
->next
? r
->next
: r
;
99 *config
->promisors_tail
= r
;
100 config
->promisors_tail
= &r
->next
;
103 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
105 struct promisor_remote_config
*config
= data
;
110 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
113 if (!strcmp(subkey
, "promisor")) {
116 if (!git_config_bool(var
, value
))
119 remote_name
= xmemdupz(name
, namelen
);
121 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
122 promisor_remote_new(config
, remote_name
);
127 if (!strcmp(subkey
, "partialclonefilter")) {
128 struct promisor_remote
*r
;
129 char *remote_name
= xmemdupz(name
, namelen
);
131 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
133 r
= promisor_remote_new(config
, remote_name
);
140 return git_config_string(&r
->partial_clone_filter
, var
, value
);
146 static void promisor_remote_init(struct repository
*r
)
148 struct promisor_remote_config
*config
;
150 if (r
->promisor_remote_config
)
152 config
= r
->promisor_remote_config
=
153 xcalloc(1, sizeof(*r
->promisor_remote_config
));
154 config
->promisors_tail
= &config
->promisors
;
156 repo_config(r
, promisor_remote_config
, config
);
158 if (r
->repository_format_partial_clone
) {
159 struct promisor_remote
*o
, *previous
;
161 o
= promisor_remote_lookup(config
,
162 r
->repository_format_partial_clone
,
165 promisor_remote_move_to_tail(config
, o
, previous
);
167 promisor_remote_new(config
, r
->repository_format_partial_clone
);
171 void promisor_remote_clear(struct promisor_remote_config
*config
)
173 while (config
->promisors
) {
174 struct promisor_remote
*r
= config
->promisors
;
175 config
->promisors
= config
->promisors
->next
;
179 config
->promisors_tail
= &config
->promisors
;
182 void repo_promisor_remote_reinit(struct repository
*r
)
184 promisor_remote_clear(r
->promisor_remote_config
);
185 FREE_AND_NULL(r
->promisor_remote_config
);
186 promisor_remote_init(r
);
189 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
190 const char *remote_name
)
192 promisor_remote_init(r
);
195 return r
->promisor_remote_config
->promisors
;
197 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
200 int repo_has_promisor_remote(struct repository
*r
)
202 return !!repo_promisor_remote_find(r
, NULL
);
205 static int remove_fetched_oids(struct repository
*repo
,
206 struct object_id
**oids
,
207 int oid_nr
, int to_free
)
209 int i
, remaining_nr
= 0;
210 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
211 struct object_id
*old_oids
= *oids
;
212 struct object_id
*new_oids
;
214 for (i
= 0; i
< oid_nr
; i
++)
215 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
216 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
223 CALLOC_ARRAY(new_oids
, remaining_nr
);
224 for (i
= 0; i
< oid_nr
; i
++)
226 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
237 void promisor_remote_get_direct(struct repository
*repo
,
238 const struct object_id
*oids
,
241 struct promisor_remote
*r
;
242 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
243 int remaining_nr
= oid_nr
;
250 promisor_remote_init(repo
);
252 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
253 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
254 if (remaining_nr
== 1)
256 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
257 remaining_nr
, to_free
);
266 for (i
= 0; i
< remaining_nr
; i
++) {
267 if (is_promisor_object(&remaining_oids
[i
]))
268 die(_("could not fetch %s from promisor remote"),
269 oid_to_hex(&remaining_oids
[i
]));
274 free(remaining_oids
);