]>
Commit | Line | Data |
---|---|---|
9b288516 DB |
1 | #include "cache.h" |
2 | #include "transport.h" | |
3 | #include "run-command.h" | |
c29727d5 DB |
4 | #include "pkt-line.h" |
5 | #include "fetch-pack.h" | |
47a59185 JH |
6 | #include "remote.h" |
7 | #include "connect.h" | |
40cb4fab | 8 | #include "send-pack.h" |
c29727d5 | 9 | #include "walker.h" |
c7a8a162 | 10 | #include "bundle.h" |
cd547b48 JS |
11 | #include "dir.h" |
12 | #include "refs.h" | |
e9fcd1e2 | 13 | #include "branch.h" |
638794cd | 14 | #include "url.h" |
d2b17b32 | 15 | #include "submodule.h" |
a762e51e | 16 | #include "string-list.h" |
13eb4626 | 17 | #include "sha1-array.h" |
af65f68c | 18 | #include "sigchain.h" |
cd547b48 | 19 | |
e9fcd1e2 IL |
20 | static void set_upstreams(struct transport *transport, struct ref *refs, |
21 | int pretend) | |
22 | { | |
23 | struct ref *ref; | |
24 | for (ref = refs; ref; ref = ref->next) { | |
25 | const char *localname; | |
26 | const char *tmp; | |
27 | const char *remotename; | |
28 | unsigned char sha[20]; | |
29 | int flag = 0; | |
30 | /* | |
31 | * Check suitability for tracking. Must be successful / | |
32 | * already up-to-date ref create/modify (not delete). | |
33 | */ | |
34 | if (ref->status != REF_STATUS_OK && | |
35 | ref->status != REF_STATUS_UPTODATE) | |
36 | continue; | |
37 | if (!ref->peer_ref) | |
38 | continue; | |
f4e54d02 | 39 | if (is_null_oid(&ref->new_oid)) |
e9fcd1e2 IL |
40 | continue; |
41 | ||
42 | /* Follow symbolic refs (mainly for HEAD). */ | |
43 | localname = ref->peer_ref->name; | |
44 | remotename = ref->name; | |
7695d118 RS |
45 | tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, |
46 | sha, &flag); | |
e9fcd1e2 | 47 | if (tmp && flag & REF_ISSYMREF && |
59556548 | 48 | starts_with(tmp, "refs/heads/")) |
e9fcd1e2 IL |
49 | localname = tmp; |
50 | ||
51 | /* Both source and destination must be local branches. */ | |
59556548 | 52 | if (!localname || !starts_with(localname, "refs/heads/")) |
e9fcd1e2 | 53 | continue; |
59556548 | 54 | if (!remotename || !starts_with(remotename, "refs/heads/")) |
e9fcd1e2 IL |
55 | continue; |
56 | ||
57 | if (!pretend) | |
58 | install_branch_config(BRANCH_CONFIG_VERBOSE, | |
59 | localname + 11, transport->remote->name, | |
60 | remotename); | |
61 | else | |
7ab1d44f | 62 | printf(_("Would set upstream of '%s' to '%s' of '%s'\n"), |
e9fcd1e2 IL |
63 | localname + 11, remotename + 11, |
64 | transport->remote->name); | |
65 | } | |
66 | } | |
67 | ||
c7a8a162 JS |
68 | struct bundle_transport_data { |
69 | int fd; | |
70 | struct bundle_header header; | |
71 | }; | |
72 | ||
64fcef2d | 73 | static struct ref *get_refs_from_bundle(struct transport *transport, int for_push) |
c7a8a162 JS |
74 | { |
75 | struct bundle_transport_data *data = transport->data; | |
76 | struct ref *result = NULL; | |
77 | int i; | |
78 | ||
64fcef2d DB |
79 | if (for_push) |
80 | return NULL; | |
81 | ||
c7a8a162 JS |
82 | if (data->fd > 0) |
83 | close(data->fd); | |
84 | data->fd = read_bundle_header(transport->url, &data->header); | |
85 | if (data->fd < 0) | |
86 | die ("Could not read bundle '%s'.", transport->url); | |
87 | for (i = 0; i < data->header.references.nr; i++) { | |
88 | struct ref_list_entry *e = data->header.references.list + i; | |
59c69c0c | 89 | struct ref *ref = alloc_ref(e->name); |
f4e54d02 | 90 | hashcpy(ref->old_oid.hash, e->sha1); |
c7a8a162 JS |
91 | ref->next = result; |
92 | result = ref; | |
93 | } | |
94 | return result; | |
95 | } | |
96 | ||
1788c39c | 97 | static int fetch_refs_from_bundle(struct transport *transport, |
37148311 | 98 | int nr_heads, struct ref **to_fetch) |
c7a8a162 JS |
99 | { |
100 | struct bundle_transport_data *data = transport->data; | |
be042aff JH |
101 | return unbundle(&data->header, data->fd, |
102 | transport->progress ? BUNDLE_VERBOSE : 0); | |
c7a8a162 JS |
103 | } |
104 | ||
105 | static int close_bundle(struct transport *transport) | |
106 | { | |
107 | struct bundle_transport_data *data = transport->data; | |
108 | if (data->fd > 0) | |
109 | close(data->fd); | |
f4e95765 | 110 | free(data); |
c7a8a162 JS |
111 | return 0; |
112 | } | |
113 | ||
9b288516 | 114 | struct git_transport_data { |
aa5af974 | 115 | struct git_transport_options options; |
ba227857 DB |
116 | struct child_process *conn; |
117 | int fd[2]; | |
61b075bd | 118 | unsigned got_remote_heads : 1; |
13eb4626 | 119 | struct sha1_array extra_have; |
beea4152 | 120 | struct sha1_array shallow; |
9b288516 DB |
121 | }; |
122 | ||
aa5af974 | 123 | static int set_git_option(struct git_transport_options *opts, |
9b288516 DB |
124 | const char *name, const char *value) |
125 | { | |
c29727d5 | 126 | if (!strcmp(name, TRANS_OPT_UPLOADPACK)) { |
aa5af974 | 127 | opts->uploadpack = value; |
c29727d5 DB |
128 | return 0; |
129 | } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) { | |
aa5af974 | 130 | opts->receivepack = value; |
9b288516 DB |
131 | return 0; |
132 | } else if (!strcmp(name, TRANS_OPT_THIN)) { | |
aa5af974 | 133 | opts->thin = !!value; |
9b288516 | 134 | return 0; |
41fa7d2e | 135 | } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) { |
aa5af974 | 136 | opts->followtags = !!value; |
41fa7d2e | 137 | return 0; |
c29727d5 | 138 | } else if (!strcmp(name, TRANS_OPT_KEEP)) { |
aa5af974 | 139 | opts->keep = !!value; |
c29727d5 | 140 | return 0; |
48d25cae NTND |
141 | } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) { |
142 | opts->update_shallow = !!value; | |
143 | return 0; | |
c29727d5 DB |
144 | } else if (!strcmp(name, TRANS_OPT_DEPTH)) { |
145 | if (!value) | |
aa5af974 | 146 | opts->depth = 0; |
e7622ce8 NTND |
147 | else { |
148 | char *end; | |
149 | opts->depth = strtol(value, &end, 0); | |
150 | if (*end) | |
7ab1d44f | 151 | die(_("transport: invalid depth option '%s'"), value); |
e7622ce8 | 152 | } |
c29727d5 | 153 | return 0; |
9b288516 DB |
154 | } |
155 | return 1; | |
156 | } | |
157 | ||
f3ee9ca5 | 158 | static int connect_setup(struct transport *transport, int for_push) |
ba227857 DB |
159 | { |
160 | struct git_transport_data *data = transport->data; | |
f3ee9ca5 | 161 | int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0; |
61b075bd IL |
162 | |
163 | if (data->conn) | |
164 | return 0; | |
165 | ||
c915f11e EW |
166 | switch (transport->family) { |
167 | case TRANSPORT_FAMILY_ALL: break; | |
168 | case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break; | |
169 | case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break; | |
170 | } | |
171 | ||
5a277f3f JH |
172 | data->conn = git_connect(data->fd, transport->url, |
173 | for_push ? data->options.receivepack : | |
174 | data->options.uploadpack, | |
f3ee9ca5 | 175 | flags); |
61b075bd | 176 | |
ba227857 DB |
177 | return 0; |
178 | } | |
179 | ||
64fcef2d | 180 | static struct ref *get_refs_via_connect(struct transport *transport, int for_push) |
c29727d5 DB |
181 | { |
182 | struct git_transport_data *data = transport->data; | |
183 | struct ref *refs; | |
c29727d5 | 184 | |
f3ee9ca5 | 185 | connect_setup(transport, for_push); |
85edf4f5 | 186 | get_remote_heads(data->fd[0], NULL, 0, &refs, |
beea4152 NTND |
187 | for_push ? REF_NORMAL : 0, |
188 | &data->extra_have, | |
4820a33b | 189 | &data->shallow); |
61b075bd | 190 | data->got_remote_heads = 1; |
c29727d5 DB |
191 | |
192 | return refs; | |
193 | } | |
194 | ||
1788c39c | 195 | static int fetch_refs_via_pack(struct transport *transport, |
37148311 | 196 | int nr_heads, struct ref **to_fetch) |
c29727d5 DB |
197 | { |
198 | struct git_transport_data *data = transport->data; | |
626df76e | 199 | struct ref *refs; |
c29727d5 DB |
200 | char *dest = xstrdup(transport->url); |
201 | struct fetch_pack_args args; | |
00183cbb | 202 | struct ref *refs_tmp = NULL; |
c29727d5 | 203 | |
fa740529 | 204 | memset(&args, 0, sizeof(args)); |
aa5af974 IL |
205 | args.uploadpack = data->options.uploadpack; |
206 | args.keep_pack = data->options.keep; | |
fa740529 | 207 | args.lock_pack = 1; |
aa5af974 IL |
208 | args.use_thin_pack = data->options.thin; |
209 | args.include_tag = data->options.followtags; | |
bd2c86ef | 210 | args.verbose = (transport->verbose > 1); |
fe8aa148 | 211 | args.quiet = (transport->verbose < 0); |
d01b3c02 | 212 | args.no_progress = !transport->progress; |
aa5af974 | 213 | args.depth = data->options.depth; |
c6807a40 NTND |
214 | args.check_self_contained_and_connected = |
215 | data->options.check_self_contained_and_connected; | |
beea4152 | 216 | args.cloning = transport->cloning; |
48d25cae | 217 | args.update_shallow = data->options.update_shallow; |
c29727d5 | 218 | |
61b075bd | 219 | if (!data->got_remote_heads) { |
f3ee9ca5 | 220 | connect_setup(transport, 0); |
b06dcd7d | 221 | get_remote_heads(data->fd[0], NULL, 0, &refs_tmp, 0, |
4820a33b | 222 | NULL, &data->shallow); |
61b075bd | 223 | data->got_remote_heads = 1; |
ba227857 DB |
224 | } |
225 | ||
00183cbb DB |
226 | refs = fetch_pack(&args, data->fd, data->conn, |
227 | refs_tmp ? refs_tmp : transport->remote_refs, | |
beea4152 | 228 | dest, to_fetch, nr_heads, &data->shallow, |
f2db854d | 229 | &transport->pack_lockfile); |
ba227857 DB |
230 | close(data->fd[0]); |
231 | close(data->fd[1]); | |
626df76e JK |
232 | if (finish_connect(data->conn)) { |
233 | free_refs(refs); | |
ba227857 | 234 | refs = NULL; |
626df76e | 235 | } |
ba227857 | 236 | data->conn = NULL; |
61b075bd | 237 | data->got_remote_heads = 0; |
c6807a40 NTND |
238 | data->options.self_contained_and_connected = |
239 | args.self_contained_and_connected; | |
c29727d5 | 240 | |
00183cbb | 241 | free_refs(refs_tmp); |
626df76e | 242 | free_refs(refs); |
c29727d5 | 243 | free(dest); |
c6bc4005 | 244 | return (refs ? 0 : -1); |
c29727d5 DB |
245 | } |
246 | ||
481c7a6d JK |
247 | static int push_had_errors(struct ref *ref) |
248 | { | |
249 | for (; ref; ref = ref->next) { | |
250 | switch (ref->status) { | |
251 | case REF_STATUS_NONE: | |
252 | case REF_STATUS_UPTODATE: | |
253 | case REF_STATUS_OK: | |
254 | break; | |
255 | default: | |
256 | return 1; | |
257 | } | |
258 | } | |
259 | return 0; | |
260 | } | |
261 | ||
f1863d0d | 262 | int transport_refs_pushed(struct ref *ref) |
64fcef2d DB |
263 | { |
264 | for (; ref; ref = ref->next) { | |
265 | switch(ref->status) { | |
266 | case REF_STATUS_NONE: | |
267 | case REF_STATUS_UPTODATE: | |
268 | break; | |
269 | default: | |
270 | return 1; | |
271 | } | |
272 | } | |
273 | return 0; | |
274 | } | |
275 | ||
f1863d0d | 276 | void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose) |
64fcef2d DB |
277 | { |
278 | struct refspec rs; | |
279 | ||
280 | if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE) | |
281 | return; | |
282 | ||
283 | rs.src = ref->name; | |
284 | rs.dst = NULL; | |
285 | ||
286 | if (!remote_find_tracking(remote, &rs)) { | |
287 | if (verbose) | |
288 | fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); | |
289 | if (ref->deletion) { | |
290 | delete_ref(rs.dst, NULL, 0); | |
291 | } else | |
292 | update_ref("update by push", rs.dst, | |
f4e54d02 | 293 | ref->new_oid.hash, NULL, 0, 0); |
64fcef2d DB |
294 | free(rs.dst); |
295 | } | |
296 | } | |
297 | ||
7101e10c JH |
298 | static void print_ref_status(char flag, const char *summary, |
299 | struct ref *to, struct ref *from, const char *msg, | |
300 | int porcelain, int summary_width) | |
64fcef2d | 301 | { |
1965ff74 LA |
302 | if (porcelain) { |
303 | if (from) | |
304 | fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); | |
305 | else | |
306 | fprintf(stdout, "%c\t:%s\t", flag, to->name); | |
307 | if (msg) | |
308 | fprintf(stdout, "%s (%s)\n", summary, msg); | |
309 | else | |
310 | fprintf(stdout, "%s\n", summary); | |
311 | } else { | |
7101e10c | 312 | fprintf(stderr, " %c %-*s ", flag, summary_width, summary); |
1965ff74 LA |
313 | if (from) |
314 | fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name)); | |
315 | else | |
316 | fputs(prettify_refname(to->name), stderr); | |
317 | if (msg) { | |
318 | fputs(" (", stderr); | |
319 | fputs(msg, stderr); | |
320 | fputc(')', stderr); | |
321 | } | |
322 | fputc('\n', stderr); | |
64fcef2d | 323 | } |
64fcef2d DB |
324 | } |
325 | ||
7101e10c | 326 | static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width) |
64fcef2d DB |
327 | { |
328 | if (ref->deletion) | |
7101e10c JH |
329 | print_ref_status('-', "[deleted]", ref, NULL, NULL, |
330 | porcelain, summary_width); | |
f4e54d02 | 331 | else if (is_null_oid(&ref->old_oid)) |
64fcef2d | 332 | print_ref_status('*', |
59556548 | 333 | (starts_with(ref->name, "refs/tags/") ? "[new tag]" : |
1965ff74 | 334 | "[new branch]"), |
7101e10c | 335 | ref, ref->peer_ref, NULL, porcelain, summary_width); |
64fcef2d | 336 | else { |
bd22d4ff | 337 | struct strbuf quickref = STRBUF_INIT; |
64fcef2d DB |
338 | char type; |
339 | const char *msg; | |
340 | ||
1eb47f16 RS |
341 | strbuf_add_unique_abbrev(&quickref, ref->old_oid.hash, |
342 | DEFAULT_ABBREV); | |
5ece083f | 343 | if (ref->forced_update) { |
bd22d4ff | 344 | strbuf_addstr(&quickref, "..."); |
64fcef2d DB |
345 | type = '+'; |
346 | msg = "forced update"; | |
347 | } else { | |
bd22d4ff | 348 | strbuf_addstr(&quickref, ".."); |
64fcef2d DB |
349 | type = ' '; |
350 | msg = NULL; | |
351 | } | |
1eb47f16 RS |
352 | strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, |
353 | DEFAULT_ABBREV); | |
64fcef2d | 354 | |
7101e10c JH |
355 | print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, |
356 | porcelain, summary_width); | |
bd22d4ff | 357 | strbuf_release(&quickref); |
64fcef2d DB |
358 | } |
359 | } | |
360 | ||
7101e10c JH |
361 | static int print_one_push_status(struct ref *ref, const char *dest, int count, |
362 | int porcelain, int summary_width) | |
64fcef2d | 363 | { |
882d49ca JK |
364 | if (!count) { |
365 | char *url = transport_anonymize_url(dest); | |
366 | fprintf(porcelain ? stdout : stderr, "To %s\n", url); | |
367 | free(url); | |
368 | } | |
64fcef2d DB |
369 | |
370 | switch(ref->status) { | |
371 | case REF_STATUS_NONE: | |
7101e10c JH |
372 | print_ref_status('X', "[no match]", ref, NULL, NULL, |
373 | porcelain, summary_width); | |
64fcef2d DB |
374 | break; |
375 | case REF_STATUS_REJECT_NODELETE: | |
376 | print_ref_status('!', "[rejected]", ref, NULL, | |
7101e10c JH |
377 | "remote does not support deleting refs", |
378 | porcelain, summary_width); | |
64fcef2d DB |
379 | break; |
380 | case REF_STATUS_UPTODATE: | |
381 | print_ref_status('=', "[up to date]", ref, | |
7101e10c | 382 | ref->peer_ref, NULL, porcelain, summary_width); |
64fcef2d DB |
383 | break; |
384 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
385 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 386 | "non-fast-forward", porcelain, summary_width); |
64fcef2d | 387 | break; |
dbfeddb1 CR |
388 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
389 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 390 | "already exists", porcelain, summary_width); |
dbfeddb1 | 391 | break; |
75e5c0dc JH |
392 | case REF_STATUS_REJECT_FETCH_FIRST: |
393 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 394 | "fetch first", porcelain, summary_width); |
75e5c0dc JH |
395 | break; |
396 | case REF_STATUS_REJECT_NEEDS_FORCE: | |
397 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 398 | "needs force", porcelain, summary_width); |
75e5c0dc | 399 | break; |
631b5ef2 JH |
400 | case REF_STATUS_REJECT_STALE: |
401 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 402 | "stale info", porcelain, summary_width); |
631b5ef2 | 403 | break; |
4820a33b NTND |
404 | case REF_STATUS_REJECT_SHALLOW: |
405 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c JH |
406 | "new shallow roots not allowed", |
407 | porcelain, summary_width); | |
4820a33b | 408 | break; |
64fcef2d DB |
409 | case REF_STATUS_REMOTE_REJECT: |
410 | print_ref_status('!', "[remote rejected]", ref, | |
7101e10c JH |
411 | ref->deletion ? NULL : ref->peer_ref, |
412 | ref->remote_status, porcelain, summary_width); | |
64fcef2d DB |
413 | break; |
414 | case REF_STATUS_EXPECTING_REPORT: | |
415 | print_ref_status('!', "[remote failure]", ref, | |
7101e10c JH |
416 | ref->deletion ? NULL : ref->peer_ref, |
417 | "remote failed to report status", | |
418 | porcelain, summary_width); | |
64fcef2d | 419 | break; |
4ff17f10 RS |
420 | case REF_STATUS_ATOMIC_PUSH_FAILED: |
421 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 422 | "atomic push failed", porcelain, summary_width); |
4ff17f10 | 423 | break; |
64fcef2d | 424 | case REF_STATUS_OK: |
7101e10c | 425 | print_ok_ref_status(ref, porcelain, summary_width); |
64fcef2d DB |
426 | break; |
427 | } | |
428 | ||
429 | return 1; | |
430 | } | |
431 | ||
11fd66de JH |
432 | int transport_summary_width(const struct ref *refs) |
433 | { | |
434 | return (2 * FALLBACK_DEFAULT_ABBREV + 3); | |
435 | } | |
436 | ||
f1863d0d | 437 | void transport_print_push_status(const char *dest, struct ref *refs, |
10643d4e | 438 | int verbose, int porcelain, unsigned int *reject_reasons) |
64fcef2d DB |
439 | { |
440 | struct ref *ref; | |
441 | int n = 0; | |
f25950f3 CT |
442 | unsigned char head_sha1[20]; |
443 | char *head; | |
11fd66de | 444 | int summary_width = transport_summary_width(refs); |
f25950f3 | 445 | |
7695d118 | 446 | head = resolve_refdup("HEAD", RESOLVE_REF_READING, head_sha1, NULL); |
64fcef2d DB |
447 | |
448 | if (verbose) { | |
449 | for (ref = refs; ref; ref = ref->next) | |
450 | if (ref->status == REF_STATUS_UPTODATE) | |
7101e10c JH |
451 | n += print_one_push_status(ref, dest, n, |
452 | porcelain, summary_width); | |
64fcef2d DB |
453 | } |
454 | ||
455 | for (ref = refs; ref; ref = ref->next) | |
456 | if (ref->status == REF_STATUS_OK) | |
7101e10c JH |
457 | n += print_one_push_status(ref, dest, n, |
458 | porcelain, summary_width); | |
64fcef2d | 459 | |
10643d4e | 460 | *reject_reasons = 0; |
64fcef2d DB |
461 | for (ref = refs; ref; ref = ref->next) { |
462 | if (ref->status != REF_STATUS_NONE && | |
463 | ref->status != REF_STATUS_UPTODATE && | |
464 | ref->status != REF_STATUS_OK) | |
7101e10c JH |
465 | n += print_one_push_status(ref, dest, n, |
466 | porcelain, summary_width); | |
10643d4e | 467 | if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) { |
1d2c14df | 468 | if (head != NULL && !strcmp(head, ref->name)) |
10643d4e | 469 | *reject_reasons |= REJECT_NON_FF_HEAD; |
f25950f3 | 470 | else |
10643d4e | 471 | *reject_reasons |= REJECT_NON_FF_OTHER; |
dbfeddb1 CR |
472 | } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) { |
473 | *reject_reasons |= REJECT_ALREADY_EXISTS; | |
75e5c0dc JH |
474 | } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) { |
475 | *reject_reasons |= REJECT_FETCH_FIRST; | |
476 | } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) { | |
477 | *reject_reasons |= REJECT_NEEDS_FORCE; | |
f25950f3 | 478 | } |
64fcef2d | 479 | } |
dc76c7f7 | 480 | free(head); |
64fcef2d DB |
481 | } |
482 | ||
f1863d0d | 483 | void transport_verify_remote_names(int nr_heads, const char **heads) |
64fcef2d DB |
484 | { |
485 | int i; | |
486 | ||
487 | for (i = 0; i < nr_heads; i++) { | |
488 | const char *local = heads[i]; | |
489 | const char *remote = strrchr(heads[i], ':'); | |
490 | ||
491 | if (*local == '+') | |
492 | local++; | |
493 | ||
494 | /* A matching refspec is okay. */ | |
495 | if (remote == local && remote[1] == '\0') | |
496 | continue; | |
497 | ||
498 | remote = remote ? (remote + 1) : local; | |
8d9c5010 MH |
499 | if (check_refname_format(remote, |
500 | REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN)) | |
501 | die("remote part of refspec is not a valid name in %s", | |
502 | heads[i]); | |
64fcef2d DB |
503 | } |
504 | } | |
505 | ||
506 | static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags) | |
f3fa1838 | 507 | { |
9b288516 | 508 | struct git_transport_data *data = transport->data; |
40cb4fab | 509 | struct send_pack_args args; |
64fcef2d DB |
510 | int ret; |
511 | ||
61b075bd | 512 | if (!data->got_remote_heads) { |
64fcef2d | 513 | struct ref *tmp_refs; |
f3ee9ca5 | 514 | connect_setup(transport, 1); |
64fcef2d | 515 | |
b016918b NTND |
516 | get_remote_heads(data->fd[0], NULL, 0, &tmp_refs, REF_NORMAL, |
517 | NULL, &data->shallow); | |
61b075bd | 518 | data->got_remote_heads = 1; |
64fcef2d | 519 | } |
9b288516 | 520 | |
de1a2fdd | 521 | memset(&args, 0, sizeof(args)); |
94c89ba6 | 522 | args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); |
40cb4fab | 523 | args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); |
aa5af974 | 524 | args.use_thin_pack = data->options.thin; |
8afd8dc0 TRC |
525 | args.verbose = (transport->verbose > 0); |
526 | args.quiet = (transport->verbose < 0); | |
d7c411b7 | 527 | args.progress = transport->progress; |
40cb4fab | 528 | args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); |
77555854 | 529 | args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN); |
d0e8e09c | 530 | args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC); |
f6a4e61f | 531 | args.push_options = transport->push_options; |
9be89160 | 532 | args.url = transport->url; |
40cb4fab | 533 | |
30261094 DB |
534 | if (flags & TRANSPORT_PUSH_CERT_ALWAYS) |
535 | args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; | |
536 | else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) | |
537 | args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; | |
538 | else | |
539 | args.push_cert = SEND_PACK_PUSH_CERT_NEVER; | |
540 | ||
64fcef2d DB |
541 | ret = send_pack(&args, data->fd, data->conn, remote_refs, |
542 | &data->extra_have); | |
543 | ||
544 | close(data->fd[1]); | |
545 | close(data->fd[0]); | |
546 | ret |= finish_connect(data->conn); | |
547 | data->conn = NULL; | |
61b075bd | 548 | data->got_remote_heads = 0; |
64fcef2d DB |
549 | |
550 | return ret; | |
9b288516 DB |
551 | } |
552 | ||
b236752a IL |
553 | static int connect_git(struct transport *transport, const char *name, |
554 | const char *executable, int fd[2]) | |
555 | { | |
556 | struct git_transport_data *data = transport->data; | |
557 | data->conn = git_connect(data->fd, transport->url, | |
558 | executable, 0); | |
559 | fd[0] = data->fd[0]; | |
560 | fd[1] = data->fd[1]; | |
561 | return 0; | |
562 | } | |
563 | ||
f4e95765 SP |
564 | static int disconnect_git(struct transport *transport) |
565 | { | |
ba227857 DB |
566 | struct git_transport_data *data = transport->data; |
567 | if (data->conn) { | |
61b075bd IL |
568 | if (data->got_remote_heads) |
569 | packet_flush(data->fd[1]); | |
ba227857 DB |
570 | close(data->fd[0]); |
571 | close(data->fd[1]); | |
572 | finish_connect(data->conn); | |
573 | } | |
574 | ||
575 | free(data); | |
f4e95765 SP |
576 | return 0; |
577 | } | |
578 | ||
61b075bd IL |
579 | void transport_take_over(struct transport *transport, |
580 | struct child_process *child) | |
581 | { | |
582 | struct git_transport_data *data; | |
583 | ||
584 | if (!transport->smart_options) | |
ef1177d1 | 585 | die("BUG: taking over transport requires non-NULL " |
61b075bd IL |
586 | "smart_options field."); |
587 | ||
588 | data = xcalloc(1, sizeof(*data)); | |
589 | data->options = *transport->smart_options; | |
590 | data->conn = child; | |
591 | data->fd[0] = data->conn->out; | |
592 | data->fd[1] = data->conn->in; | |
593 | data->got_remote_heads = 0; | |
594 | transport->data = data; | |
595 | ||
596 | transport->set_option = NULL; | |
597 | transport->get_refs_list = get_refs_via_connect; | |
598 | transport->fetch = fetch_refs_via_pack; | |
599 | transport->push = NULL; | |
600 | transport->push_refs = git_transport_push; | |
601 | transport->disconnect = disconnect_git; | |
602 | transport->smart_options = &(data->options); | |
b26ed430 JH |
603 | |
604 | transport->cannot_reuse = 1; | |
61b075bd IL |
605 | } |
606 | ||
9b288516 DB |
607 | static int is_file(const char *url) |
608 | { | |
609 | struct stat buf; | |
610 | if (stat(url, &buf)) | |
611 | return 0; | |
612 | return S_ISREG(buf.st_mode); | |
613 | } | |
614 | ||
25d5cc48 IL |
615 | static int external_specification_len(const char *url) |
616 | { | |
617 | return strchr(url, ':') - url; | |
618 | } | |
619 | ||
5088d3b3 | 620 | static const struct string_list *protocol_whitelist(void) |
a5adaced | 621 | { |
5088d3b3 JK |
622 | static int enabled = -1; |
623 | static struct string_list allowed = STRING_LIST_INIT_DUP; | |
a5adaced | 624 | |
5088d3b3 JK |
625 | if (enabled < 0) { |
626 | const char *v = getenv("GIT_ALLOW_PROTOCOL"); | |
627 | if (v) { | |
628 | string_list_split(&allowed, v, ':', -1); | |
629 | string_list_sort(&allowed); | |
630 | enabled = 1; | |
631 | } else { | |
632 | enabled = 0; | |
633 | } | |
634 | } | |
635 | ||
636 | return enabled ? &allowed : NULL; | |
637 | } | |
a5adaced | 638 | |
5088d3b3 JK |
639 | int is_transport_allowed(const char *type) |
640 | { | |
641 | const struct string_list *allowed = protocol_whitelist(); | |
642 | return !allowed || string_list_has_string(allowed, type); | |
643 | } | |
644 | ||
645 | void transport_check_allowed(const char *type) | |
646 | { | |
647 | if (!is_transport_allowed(type)) | |
a5adaced | 648 | die("transport '%s' not allowed", type); |
5088d3b3 JK |
649 | } |
650 | ||
651 | int transport_restrict_protocols(void) | |
652 | { | |
653 | return !!protocol_whitelist(); | |
a5adaced JK |
654 | } |
655 | ||
e5f4e214 | 656 | struct transport *transport_get(struct remote *remote, const char *url) |
9b288516 | 657 | { |
4da50460 | 658 | const char *helper; |
8eb554ae SP |
659 | struct transport *ret = xcalloc(1, sizeof(*ret)); |
660 | ||
d01b3c02 TRC |
661 | ret->progress = isatty(2); |
662 | ||
c1d45cf7 DB |
663 | if (!remote) |
664 | die("No remote provided to transport_get()"); | |
665 | ||
b0d66e15 | 666 | ret->got_remote_refs = 0; |
8eb554ae | 667 | ret->remote = remote; |
4da50460 | 668 | helper = remote->foreign_vcs; |
fb0cc87e | 669 | |
cb21d8f0 | 670 | if (!url && remote->url) |
fb0cc87e | 671 | url = remote->url[0]; |
8eb554ae | 672 | ret->url = url; |
8eb554ae | 673 | |
87422439 JS |
674 | /* maybe it is a foreign URL? */ |
675 | if (url) { | |
676 | const char *p = url; | |
677 | ||
638794cd | 678 | while (is_urlschemechar(p == url, *p)) |
87422439 | 679 | p++; |
59556548 | 680 | if (starts_with(p, "::")) |
4da50460 | 681 | helper = xstrndup(url, p - url); |
87422439 JS |
682 | } |
683 | ||
4da50460 IL |
684 | if (helper) { |
685 | transport_helper_init(ret, helper); | |
59556548 | 686 | } else if (starts_with(url, "rsync:")) { |
0d0bac67 | 687 | die("git-over-rsync is no longer supported"); |
c59ab2e5 | 688 | } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) { |
c7a8a162 | 689 | struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); |
a5adaced | 690 | transport_check_allowed("file"); |
c7a8a162 | 691 | ret->data = data; |
824d5776 SP |
692 | ret->get_refs_list = get_refs_from_bundle; |
693 | ret->fetch = fetch_refs_from_bundle; | |
694 | ret->disconnect = close_bundle; | |
aa5af974 | 695 | ret->smart_options = NULL; |
25d5cc48 | 696 | } else if (!is_url(url) |
59556548 CC |
697 | || starts_with(url, "file://") |
698 | || starts_with(url, "git://") | |
699 | || starts_with(url, "ssh://") | |
07c7782c CMN |
700 | || starts_with(url, "git+ssh://") /* deprecated - do not use */ |
701 | || starts_with(url, "ssh+git://") /* deprecated - do not use */ | |
702 | ) { | |
a5adaced JK |
703 | /* |
704 | * These are builtin smart transports; "allowed" transports | |
705 | * will be checked individually in git_connect. | |
706 | */ | |
9b288516 | 707 | struct git_transport_data *data = xcalloc(1, sizeof(*data)); |
9b288516 | 708 | ret->data = data; |
aa5af974 | 709 | ret->set_option = NULL; |
824d5776 SP |
710 | ret->get_refs_list = get_refs_via_connect; |
711 | ret->fetch = fetch_refs_via_pack; | |
64fcef2d | 712 | ret->push_refs = git_transport_push; |
b236752a | 713 | ret->connect = connect_git; |
f4e95765 | 714 | ret->disconnect = disconnect_git; |
aa5af974 | 715 | ret->smart_options = &(data->options); |
824d5776 | 716 | |
ba227857 | 717 | data->conn = NULL; |
61b075bd | 718 | data->got_remote_heads = 0; |
25d5cc48 IL |
719 | } else { |
720 | /* Unknown protocol in URL. Pass to external handler. */ | |
721 | int len = external_specification_len(url); | |
6b33894f | 722 | char *handler = xmemdupz(url, len); |
25d5cc48 | 723 | transport_helper_init(ret, handler); |
9b288516 | 724 | } |
8eb554ae | 725 | |
aa5af974 IL |
726 | if (ret->smart_options) { |
727 | ret->smart_options->thin = 1; | |
728 | ret->smart_options->uploadpack = "git-upload-pack"; | |
729 | if (remote->uploadpack) | |
730 | ret->smart_options->uploadpack = remote->uploadpack; | |
731 | ret->smart_options->receivepack = "git-receive-pack"; | |
732 | if (remote->receivepack) | |
733 | ret->smart_options->receivepack = remote->receivepack; | |
734 | } | |
735 | ||
9b288516 DB |
736 | return ret; |
737 | } | |
738 | ||
739 | int transport_set_option(struct transport *transport, | |
740 | const char *name, const char *value) | |
741 | { | |
aa5af974 IL |
742 | int git_reports = 1, protocol_reports = 1; |
743 | ||
744 | if (transport->smart_options) | |
745 | git_reports = set_git_option(transport->smart_options, | |
746 | name, value); | |
747 | ||
824d5776 | 748 | if (transport->set_option) |
aa5af974 IL |
749 | protocol_reports = transport->set_option(transport, name, |
750 | value); | |
751 | ||
752 | /* If either report is 0, report 0 (success). */ | |
753 | if (!git_reports || !protocol_reports) | |
754 | return 0; | |
755 | /* If either reports -1 (invalid value), report -1. */ | |
756 | if ((git_reports == -1) || (protocol_reports == -1)) | |
757 | return -1; | |
758 | /* Otherwise if both report unknown, report unknown. */ | |
ab865e6e | 759 | return 1; |
9b288516 DB |
760 | } |
761 | ||
d01b3c02 TRC |
762 | void transport_set_verbosity(struct transport *transport, int verbosity, |
763 | int force_progress) | |
bde873c5 | 764 | { |
bd2c86ef | 765 | if (verbosity >= 1) |
bde873c5 TRC |
766 | transport->verbose = verbosity <= 3 ? verbosity : 3; |
767 | if (verbosity < 0) | |
768 | transport->verbose = -1; | |
d01b3c02 TRC |
769 | |
770 | /** | |
771 | * Rules used to determine whether to report progress (processing aborts | |
772 | * when a rule is satisfied): | |
773 | * | |
01fdc21f CB |
774 | * . Report progress, if force_progress is 1 (ie. --progress). |
775 | * . Don't report progress, if force_progress is 0 (ie. --no-progress). | |
776 | * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ). | |
777 | * . Report progress if isatty(2) is 1. | |
d01b3c02 | 778 | **/ |
01fdc21f CB |
779 | if (force_progress >= 0) |
780 | transport->progress = !!force_progress; | |
781 | else | |
782 | transport->progress = verbosity >= 0 && isatty(2); | |
bde873c5 TRC |
783 | } |
784 | ||
a762e51e HV |
785 | static void die_with_unpushed_submodules(struct string_list *needs_pushing) |
786 | { | |
787 | int i; | |
788 | ||
7ab1d44f VA |
789 | fprintf(stderr, _("The following submodule paths contain changes that can\n" |
790 | "not be found on any remote:\n")); | |
a762e51e | 791 | for (i = 0; i < needs_pushing->nr; i++) |
5cb5fe4a | 792 | fprintf(stderr, " %s\n", needs_pushing->items[i].string); |
7ab1d44f VA |
793 | fprintf(stderr, _("\nPlease try\n\n" |
794 | " git push --recurse-submodules=on-demand\n\n" | |
795 | "or cd to the path and use\n\n" | |
796 | " git push\n\n" | |
797 | "to push them to a remote.\n\n")); | |
a762e51e HV |
798 | |
799 | string_list_clear(needs_pushing, 0); | |
800 | ||
7ab1d44f | 801 | die(_("Aborting.")); |
a762e51e HV |
802 | } |
803 | ||
ec55559f AS |
804 | static int run_pre_push_hook(struct transport *transport, |
805 | struct ref *remote_refs) | |
806 | { | |
807 | int ret = 0, x; | |
808 | struct ref *r; | |
d3180279 | 809 | struct child_process proc = CHILD_PROCESS_INIT; |
ec55559f AS |
810 | struct strbuf buf; |
811 | const char *argv[4]; | |
812 | ||
813 | if (!(argv[0] = find_hook("pre-push"))) | |
814 | return 0; | |
815 | ||
816 | argv[1] = transport->remote->name; | |
817 | argv[2] = transport->url; | |
818 | argv[3] = NULL; | |
819 | ||
ec55559f AS |
820 | proc.argv = argv; |
821 | proc.in = -1; | |
822 | ||
823 | if (start_command(&proc)) { | |
824 | finish_command(&proc); | |
825 | return -1; | |
826 | } | |
827 | ||
af65f68c CB |
828 | sigchain_push(SIGPIPE, SIG_IGN); |
829 | ||
ec55559f AS |
830 | strbuf_init(&buf, 256); |
831 | ||
832 | for (r = remote_refs; r; r = r->next) { | |
833 | if (!r->peer_ref) continue; | |
834 | if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue; | |
631b5ef2 | 835 | if (r->status == REF_STATUS_REJECT_STALE) continue; |
ec55559f AS |
836 | if (r->status == REF_STATUS_UPTODATE) continue; |
837 | ||
838 | strbuf_reset(&buf); | |
839 | strbuf_addf( &buf, "%s %s %s %s\n", | |
f4e54d02 | 840 | r->peer_ref->name, oid_to_hex(&r->new_oid), |
841 | r->name, oid_to_hex(&r->old_oid)); | |
ec55559f | 842 | |
af65f68c CB |
843 | if (write_in_full(proc.in, buf.buf, buf.len) < 0) { |
844 | /* We do not mind if a hook does not read all refs. */ | |
845 | if (errno != EPIPE) | |
846 | ret = -1; | |
ec55559f AS |
847 | break; |
848 | } | |
849 | } | |
850 | ||
851 | strbuf_release(&buf); | |
852 | ||
853 | x = close(proc.in); | |
854 | if (!ret) | |
855 | ret = x; | |
856 | ||
af65f68c CB |
857 | sigchain_pop(SIGPIPE); |
858 | ||
ec55559f AS |
859 | x = finish_command(&proc); |
860 | if (!ret) | |
861 | ret = x; | |
862 | ||
863 | return ret; | |
864 | } | |
865 | ||
9b288516 | 866 | int transport_push(struct transport *transport, |
07436e43 | 867 | int refspec_nr, const char **refspec, int flags, |
10643d4e | 868 | unsigned int *reject_reasons) |
9b288516 | 869 | { |
10643d4e | 870 | *reject_reasons = 0; |
f1863d0d | 871 | transport_verify_remote_names(refspec_nr, refspec); |
64fcef2d | 872 | |
61b075bd | 873 | if (transport->push) { |
e9fcd1e2 IL |
874 | /* Maybe FIXME. But no important transport uses this case. */ |
875 | if (flags & TRANSPORT_PUSH_SET_UPSTREAM) | |
876 | die("This transport does not support using --set-upstream"); | |
877 | ||
64fcef2d | 878 | return transport->push(transport, refspec_nr, refspec, flags); |
61b075bd | 879 | } else if (transport->push_refs) { |
ba928c13 | 880 | struct ref *remote_refs; |
64fcef2d DB |
881 | struct ref *local_refs = get_local_heads(); |
882 | int match_flags = MATCH_REFS_NONE; | |
8afd8dc0 TRC |
883 | int verbose = (transport->verbose > 0); |
884 | int quiet = (transport->verbose < 0); | |
1965ff74 | 885 | int porcelain = flags & TRANSPORT_PUSH_PORCELAIN; |
e9fcd1e2 | 886 | int pretend = flags & TRANSPORT_PUSH_DRY_RUN; |
77555854 | 887 | int push_ret, ret, err; |
64fcef2d | 888 | |
ba928c13 JK |
889 | if (check_push_refs(local_refs, refspec_nr, refspec) < 0) |
890 | return -1; | |
891 | ||
892 | remote_refs = transport->get_refs_list(transport, 1); | |
893 | ||
64fcef2d DB |
894 | if (flags & TRANSPORT_PUSH_ALL) |
895 | match_flags |= MATCH_REFS_ALL; | |
896 | if (flags & TRANSPORT_PUSH_MIRROR) | |
897 | match_flags |= MATCH_REFS_MIRROR; | |
6ddba5e2 FC |
898 | if (flags & TRANSPORT_PUSH_PRUNE) |
899 | match_flags |= MATCH_REFS_PRUNE; | |
c2aba155 JH |
900 | if (flags & TRANSPORT_PUSH_FOLLOW_TAGS) |
901 | match_flags |= MATCH_REFS_FOLLOW_TAGS; | |
64fcef2d | 902 | |
29753cdd JH |
903 | if (match_push_refs(local_refs, &remote_refs, |
904 | refspec_nr, refspec, match_flags)) { | |
64fcef2d DB |
905 | return -1; |
906 | } | |
907 | ||
91048a95 JH |
908 | if (transport->smart_options && |
909 | transport->smart_options->cas && | |
910 | !is_empty_cas(transport->smart_options->cas)) | |
911 | apply_push_cas(transport->smart_options->cas, | |
912 | transport->remote, remote_refs); | |
913 | ||
20e8b465 TRC |
914 | set_ref_status_for_push(remote_refs, |
915 | flags & TRANSPORT_PUSH_MIRROR, | |
916 | flags & TRANSPORT_PUSH_FORCE); | |
917 | ||
ec55559f AS |
918 | if (!(flags & TRANSPORT_PUSH_NO_HOOK)) |
919 | if (run_pre_push_hook(transport, remote_refs)) | |
920 | return -1; | |
921 | ||
eb21c732 | 922 | if ((flags & TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND) && !is_bare_repository()) { |
d2b17b32 FG |
923 | struct ref *ref = remote_refs; |
924 | for (; ref; ref = ref->next) | |
f4e54d02 | 925 | if (!is_null_oid(&ref->new_oid) && |
926 | !push_unpushed_submodules(ref->new_oid.hash, | |
eb21c732 HV |
927 | transport->remote->name)) |
928 | die ("Failed to push all needed submodules!"); | |
929 | } | |
930 | ||
931 | if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | | |
932 | TRANSPORT_RECURSE_SUBMODULES_CHECK)) && !is_bare_repository()) { | |
d2b17b32 | 933 | struct ref *ref = remote_refs; |
f93d7c6f | 934 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; |
a762e51e | 935 | |
d2b17b32 | 936 | for (; ref; ref = ref->next) |
f4e54d02 | 937 | if (!is_null_oid(&ref->new_oid) && |
938 | find_unpushed_submodules(ref->new_oid.hash, | |
a762e51e HV |
939 | transport->remote->name, &needs_pushing)) |
940 | die_with_unpushed_submodules(&needs_pushing); | |
d2b17b32 FG |
941 | } |
942 | ||
77555854 | 943 | push_ret = transport->push_refs(transport, remote_refs, flags); |
42328267 | 944 | err = push_had_errors(remote_refs); |
77555854 | 945 | ret = push_ret | err; |
64fcef2d | 946 | |
42328267 | 947 | if (!quiet || err) |
f1863d0d | 948 | transport_print_push_status(transport->url, remote_refs, |
6ffd7812 | 949 | verbose | porcelain, porcelain, |
10643d4e | 950 | reject_reasons); |
64fcef2d | 951 | |
e9fcd1e2 IL |
952 | if (flags & TRANSPORT_PUSH_SET_UPSTREAM) |
953 | set_upstreams(transport, remote_refs, pretend); | |
954 | ||
64fcef2d DB |
955 | if (!(flags & TRANSPORT_PUSH_DRY_RUN)) { |
956 | struct ref *ref; | |
957 | for (ref = remote_refs; ref; ref = ref->next) | |
f1863d0d | 958 | transport_update_tracking_ref(transport->remote, ref, verbose); |
64fcef2d DB |
959 | } |
960 | ||
77555854 LA |
961 | if (porcelain && !push_ret) |
962 | puts("Done"); | |
66bce02e | 963 | else if (!quiet && !ret && !transport_refs_pushed(remote_refs)) |
64fcef2d | 964 | fprintf(stderr, "Everything up-to-date\n"); |
77555854 | 965 | |
64fcef2d DB |
966 | return ret; |
967 | } | |
968 | return 1; | |
9b288516 DB |
969 | } |
970 | ||
4577370e | 971 | const struct ref *transport_get_remote_refs(struct transport *transport) |
c29727d5 | 972 | { |
b0d66e15 | 973 | if (!transport->got_remote_refs) { |
64fcef2d | 974 | transport->remote_refs = transport->get_refs_list(transport, 0); |
b0d66e15 TRC |
975 | transport->got_remote_refs = 1; |
976 | } | |
61b075bd | 977 | |
c29727d5 DB |
978 | return transport->remote_refs; |
979 | } | |
980 | ||
37148311 | 981 | int transport_fetch_refs(struct transport *transport, struct ref *refs) |
c29727d5 | 982 | { |
425b1393 | 983 | int rc; |
86386829 | 984 | int nr_heads = 0, nr_alloc = 0, nr_refs = 0; |
37148311 DB |
985 | struct ref **heads = NULL; |
986 | struct ref *rm; | |
c29727d5 DB |
987 | |
988 | for (rm = refs; rm; rm = rm->next) { | |
86386829 | 989 | nr_refs++; |
c29727d5 | 990 | if (rm->peer_ref && |
f4e54d02 | 991 | !is_null_oid(&rm->old_oid) && |
992 | !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid)) | |
c29727d5 | 993 | continue; |
7a2bff45 | 994 | ALLOC_GROW(heads, nr_heads + 1, nr_alloc); |
425b1393 | 995 | heads[nr_heads++] = rm; |
c29727d5 DB |
996 | } |
997 | ||
86386829 NP |
998 | if (!nr_heads) { |
999 | /* | |
1000 | * When deepening of a shallow repository is requested, | |
1001 | * then local and remote refs are likely to still be equal. | |
1002 | * Just feed them all to the fetch method in that case. | |
1003 | * This condition shouldn't be met in a non-deepening fetch | |
09b7e220 | 1004 | * (see builtin/fetch.c:quickfetch()). |
86386829 | 1005 | */ |
b32fa95f | 1006 | ALLOC_ARRAY(heads, nr_refs); |
86386829 NP |
1007 | for (rm = refs; rm; rm = rm->next) |
1008 | heads[nr_heads++] = rm; | |
1009 | } | |
1010 | ||
824d5776 | 1011 | rc = transport->fetch(transport, nr_heads, heads); |
61b075bd | 1012 | |
c29727d5 | 1013 | free(heads); |
425b1393 | 1014 | return rc; |
c29727d5 DB |
1015 | } |
1016 | ||
1788c39c SP |
1017 | void transport_unlock_pack(struct transport *transport) |
1018 | { | |
1019 | if (transport->pack_lockfile) { | |
691f1a28 | 1020 | unlink_or_warn(transport->pack_lockfile); |
1788c39c SP |
1021 | free(transport->pack_lockfile); |
1022 | transport->pack_lockfile = NULL; | |
1023 | } | |
1024 | } | |
1025 | ||
b236752a IL |
1026 | int transport_connect(struct transport *transport, const char *name, |
1027 | const char *exec, int fd[2]) | |
1028 | { | |
1029 | if (transport->connect) | |
1030 | return transport->connect(transport, name, exec, fd); | |
1031 | else | |
1032 | die("Operation not supported by protocol"); | |
1033 | } | |
1034 | ||
9b288516 DB |
1035 | int transport_disconnect(struct transport *transport) |
1036 | { | |
1037 | int ret = 0; | |
824d5776 SP |
1038 | if (transport->disconnect) |
1039 | ret = transport->disconnect(transport); | |
9b288516 DB |
1040 | free(transport); |
1041 | return ret; | |
1042 | } | |
47abd85b AE |
1043 | |
1044 | /* | |
a7793a74 | 1045 | * Strip username (and password) from a URL and return |
47abd85b AE |
1046 | * it in a newly allocated string. |
1047 | */ | |
1048 | char *transport_anonymize_url(const char *url) | |
1049 | { | |
21f9d0f6 | 1050 | char *scheme_prefix, *anon_part; |
47abd85b AE |
1051 | size_t anon_len, prefix_len = 0; |
1052 | ||
1053 | anon_part = strchr(url, '@'); | |
c59ab2e5 | 1054 | if (url_is_local_not_ssh(url) || !anon_part) |
47abd85b AE |
1055 | goto literal_copy; |
1056 | ||
1057 | anon_len = strlen(++anon_part); | |
1058 | scheme_prefix = strstr(url, "://"); | |
1059 | if (!scheme_prefix) { | |
1060 | if (!strchr(anon_part, ':')) | |
1061 | /* cannot be "me@there:/path/name" */ | |
1062 | goto literal_copy; | |
1063 | } else { | |
1064 | const char *cp; | |
1065 | /* make sure scheme is reasonable */ | |
1066 | for (cp = url; cp < scheme_prefix; cp++) { | |
1067 | switch (*cp) { | |
1068 | /* RFC 1738 2.1 */ | |
1069 | case '+': case '.': case '-': | |
1070 | break; /* ok */ | |
1071 | default: | |
1072 | if (isalnum(*cp)) | |
1073 | break; | |
1074 | /* it isn't */ | |
1075 | goto literal_copy; | |
1076 | } | |
1077 | } | |
1078 | /* @ past the first slash does not count */ | |
1079 | cp = strchr(scheme_prefix + 3, '/'); | |
1080 | if (cp && cp < anon_part) | |
1081 | goto literal_copy; | |
1082 | prefix_len = scheme_prefix - url + 3; | |
1083 | } | |
21f9d0f6 JK |
1084 | return xstrfmt("%.*s%.*s", (int)prefix_len, url, |
1085 | (int)anon_len, anon_part); | |
47abd85b AE |
1086 | literal_copy: |
1087 | return xstrdup(url); | |
1088 | } | |
36cfda15 | 1089 | |
114a6a88 JK |
1090 | struct alternate_refs_data { |
1091 | alternate_ref_fn *fn; | |
1092 | void *data; | |
1093 | }; | |
1094 | ||
1095 | static int refs_from_alternate_cb(struct alternate_object_database *e, | |
1096 | void *data) | |
36cfda15 JH |
1097 | { |
1098 | char *other; | |
1099 | size_t len; | |
1100 | struct remote *remote; | |
1101 | struct transport *transport; | |
1102 | const struct ref *extra; | |
114a6a88 | 1103 | struct alternate_refs_data *cb = data; |
36cfda15 JH |
1104 | |
1105 | e->name[-1] = '\0'; | |
91b3c7ce | 1106 | other = xstrdup(real_path(e->base)); |
36cfda15 JH |
1107 | e->name[-1] = '/'; |
1108 | len = strlen(other); | |
1109 | ||
1110 | while (other[len-1] == '/') | |
1111 | other[--len] = '\0'; | |
1112 | if (len < 8 || memcmp(other + len - 8, "/objects", 8)) | |
def06971 | 1113 | goto out; |
36cfda15 JH |
1114 | /* Is this a git repository with refs? */ |
1115 | memcpy(other + len - 8, "/refs", 6); | |
1116 | if (!is_directory(other)) | |
def06971 | 1117 | goto out; |
36cfda15 JH |
1118 | other[len - 8] = '\0'; |
1119 | remote = remote_get(other); | |
1120 | transport = transport_get(remote, other); | |
1121 | for (extra = transport_get_remote_refs(transport); | |
1122 | extra; | |
1123 | extra = extra->next) | |
114a6a88 | 1124 | cb->fn(extra, cb->data); |
36cfda15 | 1125 | transport_disconnect(transport); |
def06971 | 1126 | out: |
36cfda15 JH |
1127 | free(other); |
1128 | return 0; | |
1129 | } | |
114a6a88 JK |
1130 | |
1131 | void for_each_alternate_ref(alternate_ref_fn fn, void *data) | |
1132 | { | |
1133 | struct alternate_refs_data cb; | |
1134 | cb.fn = fn; | |
1135 | cb.data = data; | |
1136 | foreach_alt_odb(refs_from_alternate_cb, &cb); | |
1137 | } |