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
;
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
,
104 const struct config_context
*ctx UNUSED
,
107 struct promisor_remote_config
*config
= data
;
112 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
115 if (!strcmp(subkey
, "promisor")) {
118 if (!git_config_bool(var
, value
))
121 remote_name
= xmemdupz(name
, namelen
);
123 if (!promisor_remote_lookup(config
, remote_name
, NULL
))
124 promisor_remote_new(config
, remote_name
);
129 if (!strcmp(subkey
, "partialclonefilter")) {
130 struct promisor_remote
*r
;
131 char *remote_name
= xmemdupz(name
, namelen
);
133 r
= promisor_remote_lookup(config
, remote_name
, NULL
);
135 r
= promisor_remote_new(config
, remote_name
);
142 return git_config_string(&r
->partial_clone_filter
, var
, value
);
148 static void promisor_remote_init(struct repository
*r
)
150 struct promisor_remote_config
*config
;
152 if (r
->promisor_remote_config
)
154 config
= r
->promisor_remote_config
=
155 xcalloc(1, sizeof(*r
->promisor_remote_config
));
156 config
->promisors_tail
= &config
->promisors
;
158 repo_config(r
, promisor_remote_config
, config
);
160 if (r
->repository_format_partial_clone
) {
161 struct promisor_remote
*o
, *previous
;
163 o
= promisor_remote_lookup(config
,
164 r
->repository_format_partial_clone
,
167 promisor_remote_move_to_tail(config
, o
, previous
);
169 promisor_remote_new(config
, r
->repository_format_partial_clone
);
173 void promisor_remote_clear(struct promisor_remote_config
*config
)
175 while (config
->promisors
) {
176 struct promisor_remote
*r
= config
->promisors
;
177 config
->promisors
= config
->promisors
->next
;
181 config
->promisors_tail
= &config
->promisors
;
184 void repo_promisor_remote_reinit(struct repository
*r
)
186 promisor_remote_clear(r
->promisor_remote_config
);
187 FREE_AND_NULL(r
->promisor_remote_config
);
188 promisor_remote_init(r
);
191 struct promisor_remote
*repo_promisor_remote_find(struct repository
*r
,
192 const char *remote_name
)
194 promisor_remote_init(r
);
197 return r
->promisor_remote_config
->promisors
;
199 return promisor_remote_lookup(r
->promisor_remote_config
, remote_name
, NULL
);
202 int repo_has_promisor_remote(struct repository
*r
)
204 return !!repo_promisor_remote_find(r
, NULL
);
207 static int remove_fetched_oids(struct repository
*repo
,
208 struct object_id
**oids
,
209 int oid_nr
, int to_free
)
211 int i
, remaining_nr
= 0;
212 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
213 struct object_id
*old_oids
= *oids
;
214 struct object_id
*new_oids
;
216 for (i
= 0; i
< oid_nr
; i
++)
217 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
218 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
225 CALLOC_ARRAY(new_oids
, remaining_nr
);
226 for (i
= 0; i
< oid_nr
; i
++)
228 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
239 void promisor_remote_get_direct(struct repository
*repo
,
240 const struct object_id
*oids
,
243 struct promisor_remote
*r
;
244 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
245 int remaining_nr
= oid_nr
;
252 promisor_remote_init(repo
);
254 for (r
= repo
->promisor_remote_config
->promisors
; r
; r
= r
->next
) {
255 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
256 if (remaining_nr
== 1)
258 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
259 remaining_nr
, to_free
);
268 for (i
= 0; i
< remaining_nr
; i
++) {
269 if (is_promisor_object(&remaining_oids
[i
]))
270 die(_("could not fetch %s from promisor remote"),
271 oid_to_hex(&remaining_oids
[i
]));
276 free(remaining_oids
);