1 #include "git-compat-util.h"
4 #include "object-store-ll.h"
5 #include "promisor-remote.h"
11 #include "environment.h"
13 struct promisor_remote_config
{
14 struct promisor_remote
*promisors
;
15 struct promisor_remote
**promisors_tail
;
18 static int fetch_objects(struct repository
*repo
,
19 const char *remote_name
,
20 const struct object_id
*oids
,
23 struct child_process child
= CHILD_PROCESS_INIT
;
28 if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT
, 0)) {
29 static int warning_shown
;
32 warning(_("lazy fetching disabled; some objects may not be available"));
39 if (repo
!= the_repository
)
40 prepare_other_repo_env(&child
.env
, repo
->gitdir
);
41 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
42 "fetch", remote_name
, "--no-tags",
43 "--no-write-fetch-head", "--recurse-submodules=no",
44 "--filter=blob:none", "--stdin", NULL
);
45 if (!git_config_get_bool("promisor.quiet", &quiet
) && quiet
)
46 strvec_push(&child
.args
, "--quiet");
47 if (start_command(&child
))
48 die(_("promisor-remote: unable to fork off fetch subprocess"));
49 child_in
= xfdopen(child
.in
, "w");
51 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
53 for (i
= 0; i
< oid_nr
; i
++) {
54 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
55 die_errno(_("promisor-remote: could not write to fetch subprocess"));
56 if (fputc('\n', child_in
) < 0)
57 die_errno(_("promisor-remote: could not write to fetch subprocess"));
60 if (fclose(child_in
) < 0)
61 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
62 return finish_command(&child
) ? -1 : 0;
65 static struct promisor_remote
*promisor_remote_new(struct promisor_remote_config
*config
,
66 const char *remote_name
)
68 struct promisor_remote
*r
;
70 if (*remote_name
== '/') {
71 warning(_("promisor remote name cannot begin with '/': %s"),
76 FLEX_ALLOC_STR(r
, name
, remote_name
);
78 *config
->promisors_tail
= r
;
79 config
->promisors_tail
= &r
->next
;
84 static struct promisor_remote
*promisor_remote_lookup(struct promisor_remote_config
*config
,
85 const char *remote_name
,
86 struct promisor_remote
**previous
)
88 struct promisor_remote
*r
, *p
;
90 for (p
= NULL
, r
= config
->promisors
; r
; p
= r
, r
= r
->next
)
91 if (!strcmp(r
->name
, remote_name
)) {
100 static void promisor_remote_move_to_tail(struct promisor_remote_config
*config
,
101 struct promisor_remote
*r
,
102 struct promisor_remote
*previous
)
108 previous
->next
= r
->next
;
110 config
->promisors
= r
->next
? r
->next
: r
;
112 *config
->promisors_tail
= r
;
113 config
->promisors_tail
= &r
->next
;
116 static int promisor_remote_config(const char *var
, const char *value
,
117 const struct config_context
*ctx UNUSED
,
120 struct promisor_remote_config
*config
= data
;
125 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
128 if (!strcmp(subkey
, "promisor")) {
131 if (!git_config_bool(var
, value
))
134 remote_name
= xmemdupz(name
, namelen
);
136 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
137 promisor_remote_new(config
, remote_name
);
142 if (!strcmp(subkey
, "partialclonefilter")) {
143 struct promisor_remote
*r
;
144 char *remote_name
= xmemdupz(name
, namelen
);
146 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
148 r
= promisor_remote_new(config
, remote_name
);
155 return git_config_string(&r
->partial_clone_filter
, var
, value
);
161 static void promisor_remote_init(struct repository
*r
)
163 struct promisor_remote_config
*config
;
165 if (r
->promisor_remote_config
)
167 config
= r
->promisor_remote_config
=
168 xcalloc(1, sizeof(*r
->promisor_remote_config
));
169 config
->promisors_tail
= &config
->promisors
;
171 repo_config(r
, promisor_remote_config
, config
);
173 if (r
->repository_format_partial_clone
) {
174 struct promisor_remote
*o
, *previous
;
176 o
= promisor_remote_lookup(config
,
177 r
->repository_format_partial_clone
,
180 promisor_remote_move_to_tail(config
, o
, previous
);
182 promisor_remote_new(config
, r
->repository_format_partial_clone
);
186 void promisor_remote_clear(struct promisor_remote_config
*config
)
188 while (config
->promisors
) {
189 struct promisor_remote
*r
= config
->promisors
;
190 config
->promisors
= config
->promisors
->next
;
194 config
->promisors_tail
= &config
->promisors
;
197 void repo_promisor_remote_reinit(struct repository
*r
)
199 promisor_remote_clear(r
->promisor_remote_config
);
200 FREE_AND_NULL(r
->promisor_remote_config
);
201 promisor_remote_init(r
);
204 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
205 const char *remote_name
)
207 promisor_remote_init(r
);
210 return r
->promisor_remote_config
->promisors
;
212 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
215 int repo_has_promisor_remote(struct repository
*r
)
217 return !!repo_promisor_remote_find(r
, NULL
);
220 static int remove_fetched_oids(struct repository
*repo
,
221 struct object_id
**oids
,
222 int oid_nr
, int to_free
)
224 int i
, remaining_nr
= 0;
225 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
226 struct object_id
*old_oids
= *oids
;
227 struct object_id
*new_oids
;
229 for (i
= 0; i
< oid_nr
; i
++)
230 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
231 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
238 CALLOC_ARRAY(new_oids
, remaining_nr
);
239 for (i
= 0; i
< oid_nr
; i
++)
241 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
252 void promisor_remote_get_direct(struct repository
*repo
,
253 const struct object_id
*oids
,
256 struct promisor_remote
*r
;
257 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
258 int remaining_nr
= oid_nr
;
265 promisor_remote_init(repo
);
267 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
268 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
269 if (remaining_nr
== 1)
271 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
272 remaining_nr
, to_free
);
281 for (i
= 0; i
< remaining_nr
; i
++) {
282 if (is_promisor_object(&remaining_oids
[i
]))
283 die(_("could not fetch %s from promisor remote"),
284 oid_to_hex(&remaining_oids
[i
]));
289 free(remaining_oids
);