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
;
23 /* TODO: This should use NO_LAZY_FETCH_ENVIRONMENT */
24 if (git_env_bool("GIT_NO_LAZY_FETCH", 0)) {
25 static int warning_shown
;
28 warning(_("lazy fetching disabled; some objects may not be available"));
35 if (repo
!= the_repository
)
36 prepare_other_repo_env(&child
.env
, repo
->gitdir
);
37 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
38 "fetch", remote_name
, "--no-tags",
39 "--no-write-fetch-head", "--recurse-submodules=no",
40 "--filter=blob:none", "--stdin", NULL
);
41 if (start_command(&child
))
42 die(_("promisor-remote: unable to fork off fetch subprocess"));
43 child_in
= xfdopen(child
.in
, "w");
45 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
47 for (i
= 0; i
< oid_nr
; i
++) {
48 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
49 die_errno(_("promisor-remote: could not write to fetch subprocess"));
50 if (fputc('\n', child_in
) < 0)
51 die_errno(_("promisor-remote: could not write to fetch subprocess"));
54 if (fclose(child_in
) < 0)
55 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
56 return finish_command(&child
) ? -1 : 0;
59 static struct promisor_remote
*promisor_remote_new(struct promisor_remote_config
*config
,
60 const char *remote_name
)
62 struct promisor_remote
*r
;
64 if (*remote_name
== '/') {
65 warning(_("promisor remote name cannot begin with '/': %s"),
70 FLEX_ALLOC_STR(r
, name
, remote_name
);
72 *config
->promisors_tail
= r
;
73 config
->promisors_tail
= &r
->next
;
78 static struct promisor_remote
*promisor_remote_lookup(struct promisor_remote_config
*config
,
79 const char *remote_name
,
80 struct promisor_remote
**previous
)
82 struct promisor_remote
*r
, *p
;
84 for (p
= NULL
, r
= config
->promisors
; r
; p
= r
, r
= r
->next
)
85 if (!strcmp(r
->name
, remote_name
)) {
94 static void promisor_remote_move_to_tail(struct promisor_remote_config
*config
,
95 struct promisor_remote
*r
,
96 struct promisor_remote
*previous
)
102 previous
->next
= r
->next
;
104 config
->promisors
= r
->next
? r
->next
: r
;
106 *config
->promisors_tail
= r
;
107 config
->promisors_tail
= &r
->next
;
110 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
112 struct promisor_remote_config
*config
= data
;
117 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
120 if (!strcmp(subkey
, "promisor")) {
123 if (!git_config_bool(var
, value
))
126 remote_name
= xmemdupz(name
, namelen
);
128 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
129 promisor_remote_new(config
, remote_name
);
134 if (!strcmp(subkey
, "partialclonefilter")) {
135 struct promisor_remote
*r
;
136 char *remote_name
= xmemdupz(name
, namelen
);
138 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
140 r
= promisor_remote_new(config
, remote_name
);
147 return git_config_string(&r
->partial_clone_filter
, var
, value
);
153 static void promisor_remote_init(struct repository
*r
)
155 struct promisor_remote_config
*config
;
157 if (r
->promisor_remote_config
)
159 config
= r
->promisor_remote_config
=
160 xcalloc(1, sizeof(*r
->promisor_remote_config
));
161 config
->promisors_tail
= &config
->promisors
;
163 repo_config(r
, promisor_remote_config
, config
);
165 if (r
->repository_format_partial_clone
) {
166 struct promisor_remote
*o
, *previous
;
168 o
= promisor_remote_lookup(config
,
169 r
->repository_format_partial_clone
,
172 promisor_remote_move_to_tail(config
, o
, previous
);
174 promisor_remote_new(config
, r
->repository_format_partial_clone
);
178 void promisor_remote_clear(struct promisor_remote_config
*config
)
180 while (config
->promisors
) {
181 struct promisor_remote
*r
= config
->promisors
;
182 config
->promisors
= config
->promisors
->next
;
186 config
->promisors_tail
= &config
->promisors
;
189 void repo_promisor_remote_reinit(struct repository
*r
)
191 promisor_remote_clear(r
->promisor_remote_config
);
192 FREE_AND_NULL(r
->promisor_remote_config
);
193 promisor_remote_init(r
);
196 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
197 const char *remote_name
)
199 promisor_remote_init(r
);
202 return r
->promisor_remote_config
->promisors
;
204 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
207 int repo_has_promisor_remote(struct repository
*r
)
209 return !!repo_promisor_remote_find(r
, NULL
);
212 static int remove_fetched_oids(struct repository
*repo
,
213 struct object_id
**oids
,
214 int oid_nr
, int to_free
)
216 int i
, remaining_nr
= 0;
217 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
218 struct object_id
*old_oids
= *oids
;
219 struct object_id
*new_oids
;
221 for (i
= 0; i
< oid_nr
; i
++)
222 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
223 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
230 CALLOC_ARRAY(new_oids
, remaining_nr
);
231 for (i
= 0; i
< oid_nr
; i
++)
233 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
244 void promisor_remote_get_direct(struct repository
*repo
,
245 const struct object_id
*oids
,
248 struct promisor_remote
*r
;
249 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
250 int remaining_nr
= oid_nr
;
257 promisor_remote_init(repo
);
259 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
260 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
261 if (remaining_nr
== 1)
263 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
264 remaining_nr
, to_free
);
273 for (i
= 0; i
< remaining_nr
; i
++) {
274 if (is_promisor_object(&remaining_oids
[i
]))
275 die(_("could not fetch %s from promisor remote"),
276 oid_to_hex(&remaining_oids
[i
]));
281 free(remaining_oids
);