2 #include "object-store.h"
3 #include "promisor-remote.h"
8 static char *repository_format_partial_clone
;
10 void set_repository_format_partial_clone(char *partial_clone
)
12 repository_format_partial_clone
= xstrdup_or_null(partial_clone
);
15 static int fetch_objects(struct repository
*repo
,
16 const char *remote_name
,
17 const struct object_id
*oids
,
20 struct child_process child
= CHILD_PROCESS_INIT
;
26 strvec_pushl(&child
.args
, "-c", "fetch.negotiationAlgorithm=noop",
27 "fetch", remote_name
, "--no-tags",
28 "--no-write-fetch-head", "--recurse-submodules=no",
29 "--filter=blob:none", "--stdin", NULL
);
30 if (start_command(&child
))
31 die(_("promisor-remote: unable to fork off fetch subprocess"));
32 child_in
= xfdopen(child
.in
, "w");
34 trace2_data_intmax("promisor", repo
, "fetch_count", oid_nr
);
36 for (i
= 0; i
< oid_nr
; i
++) {
37 if (fputs(oid_to_hex(&oids
[i
]), child_in
) < 0)
38 die_errno(_("promisor-remote: could not write to fetch subprocess"));
39 if (fputc('\n', child_in
) < 0)
40 die_errno(_("promisor-remote: could not write to fetch subprocess"));
43 if (fclose(child_in
) < 0)
44 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
45 return finish_command(&child
) ? -1 : 0;
48 static struct promisor_remote
*promisors
;
49 static struct promisor_remote
**promisors_tail
= &promisors
;
51 static struct promisor_remote
*promisor_remote_new(const char *remote_name
)
53 struct promisor_remote
*r
;
55 if (*remote_name
== '/') {
56 warning(_("promisor remote name cannot begin with '/': %s"),
61 FLEX_ALLOC_STR(r
, name
, remote_name
);
64 promisors_tail
= &r
->next
;
69 static struct promisor_remote
*promisor_remote_lookup(const char *remote_name
,
70 struct promisor_remote
**previous
)
72 struct promisor_remote
*r
, *p
;
74 for (p
= NULL
, r
= promisors
; r
; p
= r
, r
= r
->next
)
75 if (!strcmp(r
->name
, remote_name
)) {
84 static void promisor_remote_move_to_tail(struct promisor_remote
*r
,
85 struct promisor_remote
*previous
)
91 previous
->next
= r
->next
;
93 promisors
= r
->next
? r
->next
: r
;
96 promisors_tail
= &r
->next
;
99 static int promisor_remote_config(const char *var
, const char *value
, void *data
)
105 if (parse_config_key(var
, "remote", &name
, &namelen
, &subkey
) < 0)
108 if (!strcmp(subkey
, "promisor")) {
111 if (!git_config_bool(var
, value
))
114 remote_name
= xmemdupz(name
, namelen
);
116 if (!promisor_remote_lookup(remote_name
, NULL
))
117 promisor_remote_new(remote_name
);
122 if (!strcmp(subkey
, "partialclonefilter")) {
123 struct promisor_remote
*r
;
124 char *remote_name
= xmemdupz(name
, namelen
);
126 r
= promisor_remote_lookup(remote_name
, NULL
);
128 r
= promisor_remote_new(remote_name
);
135 return git_config_string(&r
->partial_clone_filter
, var
, value
);
141 static int initialized
;
143 static void promisor_remote_init(void)
149 git_config(promisor_remote_config
, NULL
);
151 if (repository_format_partial_clone
) {
152 struct promisor_remote
*o
, *previous
;
154 o
= promisor_remote_lookup(repository_format_partial_clone
,
157 promisor_remote_move_to_tail(o
, previous
);
159 promisor_remote_new(repository_format_partial_clone
);
163 static void promisor_remote_clear(void)
166 struct promisor_remote
*r
= promisors
;
167 promisors
= promisors
->next
;
171 promisors_tail
= &promisors
;
174 void promisor_remote_reinit(void)
177 promisor_remote_clear();
178 promisor_remote_init();
181 struct promisor_remote
*promisor_remote_find(const char *remote_name
)
183 promisor_remote_init();
188 return promisor_remote_lookup(remote_name
, NULL
);
191 int has_promisor_remote(void)
193 return !!promisor_remote_find(NULL
);
196 static int remove_fetched_oids(struct repository
*repo
,
197 struct object_id
**oids
,
198 int oid_nr
, int to_free
)
200 int i
, remaining_nr
= 0;
201 int *remaining
= xcalloc(oid_nr
, sizeof(*remaining
));
202 struct object_id
*old_oids
= *oids
;
203 struct object_id
*new_oids
;
205 for (i
= 0; i
< oid_nr
; i
++)
206 if (oid_object_info_extended(repo
, &old_oids
[i
], NULL
,
207 OBJECT_INFO_SKIP_FETCH_OBJECT
)) {
214 CALLOC_ARRAY(new_oids
, remaining_nr
);
215 for (i
= 0; i
< oid_nr
; i
++)
217 oidcpy(&new_oids
[j
++], &old_oids
[i
]);
228 int promisor_remote_get_direct(struct repository
*repo
,
229 const struct object_id
*oids
,
232 struct promisor_remote
*r
;
233 struct object_id
*remaining_oids
= (struct object_id
*)oids
;
234 int remaining_nr
= oid_nr
;
241 promisor_remote_init();
243 for (r
= promisors
; r
; r
= r
->next
) {
244 if (fetch_objects(repo
, r
->name
, remaining_oids
, remaining_nr
) < 0) {
245 if (remaining_nr
== 1)
247 remaining_nr
= remove_fetched_oids(repo
, &remaining_oids
,
248 remaining_nr
, to_free
);
259 free(remaining_oids
);