1 #include "git-compat-util.h"
4 #include "object-store-ll.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
;
26 /* TODO: This should use NO_LAZY_FETCH_ENVIRONMENT */
27 if (git_env_bool("GIT_NO_LAZY_FETCH", 0)) {
28 static int warning_shown
;
31 warning(_("lazy fetching disabled; some objects may not be available"));
38 if (repo
!= the_repository
)
39 prepare_other_repo_env(&child
.env
, repo
->gitdir
);
40 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
41 "fetch", remote_name
, "--no-tags",
42 "--no-write-fetch-head", "--recurse-submodules=no",
43 "--filter=blob:none", "--stdin", NULL
);
44 if (start_command(&child
))
45 die(_("promisor-remote: unable to fork off fetch subprocess"));
46 child_in
= xfdopen(child
.in
, "w");
48 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
50 for (i
= 0; i
< oid_nr
; i
++) {
51 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
52 die_errno(_("promisor-remote: could not write to fetch subprocess"));
53 if (fputc('\n', child_in
) < 0)
54 die_errno(_("promisor-remote: could not write to fetch subprocess"));
57 if (fclose(child_in
) < 0)
58 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
59 return finish_command(&child
) ? -1 : 0;
62 static struct promisor_remote
*promisor_remote_new(struct promisor_remote_config
*config
,
63 const char *remote_name
)
65 struct promisor_remote
*r
;
67 if (*remote_name
== '/') {
68 warning(_("promisor remote name cannot begin with '/': %s"),
73 FLEX_ALLOC_STR(r
, name
, remote_name
);
75 *config
->promisors_tail
= r
;
76 config
->promisors_tail
= &r
->next
;
81 static struct promisor_remote
*promisor_remote_lookup(struct promisor_remote_config
*config
,
82 const char *remote_name
,
83 struct promisor_remote
**previous
)
85 struct promisor_remote
*r
, *p
;
87 for (p
= NULL
, r
= config
->promisors
; r
; p
= r
, r
= r
->next
)
88 if (!strcmp(r
->name
, remote_name
)) {
97 static void promisor_remote_move_to_tail(struct promisor_remote_config
*config
,
98 struct promisor_remote
*r
,
99 struct promisor_remote
*previous
)
105 previous
->next
= r
->next
;
107 config
->promisors
= r
->next
? r
->next
: r
;
109 *config
->promisors_tail
= r
;
110 config
->promisors_tail
= &r
->next
;
113 static int promisor_remote_config(const char *var
, const char *value
,
114 const struct config_context
*ctx UNUSED
,
117 struct promisor_remote_config
*config
= data
;
122 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
125 if (!strcmp(subkey
, "promisor")) {
128 if (!git_config_bool(var
, value
))
131 remote_name
= xmemdupz(name
, namelen
);
133 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
134 promisor_remote_new(config
, remote_name
);
139 if (!strcmp(subkey
, "partialclonefilter")) {
140 struct promisor_remote
*r
;
141 char *remote_name
= xmemdupz(name
, namelen
);
143 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
145 r
= promisor_remote_new(config
, remote_name
);
152 return git_config_string(&r
->partial_clone_filter
, var
, value
);
158 static void promisor_remote_init(struct repository
*r
)
160 struct promisor_remote_config
*config
;
162 if (r
->promisor_remote_config
)
164 config
= r
->promisor_remote_config
=
165 xcalloc(1, sizeof(*r
->promisor_remote_config
));
166 config
->promisors_tail
= &config
->promisors
;
168 repo_config(r
, promisor_remote_config
, config
);
170 if (r
->repository_format_partial_clone
) {
171 struct promisor_remote
*o
, *previous
;
173 o
= promisor_remote_lookup(config
,
174 r
->repository_format_partial_clone
,
177 promisor_remote_move_to_tail(config
, o
, previous
);
179 promisor_remote_new(config
, r
->repository_format_partial_clone
);
183 void promisor_remote_clear(struct promisor_remote_config
*config
)
185 while (config
->promisors
) {
186 struct promisor_remote
*r
= config
->promisors
;
187 config
->promisors
= config
->promisors
->next
;
191 config
->promisors_tail
= &config
->promisors
;
194 void repo_promisor_remote_reinit(struct repository
*r
)
196 promisor_remote_clear(r
->promisor_remote_config
);
197 FREE_AND_NULL(r
->promisor_remote_config
);
198 promisor_remote_init(r
);
201 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
202 const char *remote_name
)
204 promisor_remote_init(r
);
207 return r
->promisor_remote_config
->promisors
;
209 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
212 int repo_has_promisor_remote(struct repository
*r
)
214 return !!repo_promisor_remote_find(r
, NULL
);
217 static int remove_fetched_oids(struct repository
*repo
,
218 struct object_id
**oids
,
219 int oid_nr
, int to_free
)
221 int i
, remaining_nr
= 0;
222 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
223 struct object_id
*old_oids
= *oids
;
224 struct object_id
*new_oids
;
226 for (i
= 0; i
< oid_nr
; i
++)
227 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
228 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
235 CALLOC_ARRAY(new_oids
, remaining_nr
);
236 for (i
= 0; i
< oid_nr
; i
++)
238 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
249 void promisor_remote_get_direct(struct repository
*repo
,
250 const struct object_id
*oids
,
253 struct promisor_remote
*r
;
254 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
255 int remaining_nr
= oid_nr
;
262 promisor_remote_init(repo
);
264 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
265 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
266 if (remaining_nr
== 1)
268 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
269 remaining_nr
, to_free
);
278 for (i
= 0; i
< remaining_nr
; i
++) {
279 if (is_promisor_object(&remaining_oids
[i
]))
280 die(_("could not fetch %s from promisor remote"),
281 oid_to_hex(&remaining_oids
[i
]));
286 free(remaining_oids
);