]>
Commit | Line | Data |
---|---|---|
9b288516 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
9b288516 DB |
3 | #include "transport.h" |
4 | #include "run-command.h" | |
c29727d5 DB |
5 | #include "pkt-line.h" |
6 | #include "fetch-pack.h" | |
47a59185 JH |
7 | #include "remote.h" |
8 | #include "connect.h" | |
40cb4fab | 9 | #include "send-pack.h" |
c29727d5 | 10 | #include "walker.h" |
c7a8a162 | 11 | #include "bundle.h" |
cd547b48 JS |
12 | #include "dir.h" |
13 | #include "refs.h" | |
e9fcd1e2 | 14 | #include "branch.h" |
638794cd | 15 | #include "url.h" |
d2b17b32 | 16 | #include "submodule.h" |
a762e51e | 17 | #include "string-list.h" |
13eb4626 | 18 | #include "sha1-array.h" |
af65f68c | 19 | #include "sigchain.h" |
e967ca38 | 20 | #include "transport-internal.h" |
ad6ac124 | 21 | #include "protocol.h" |
0d4a1321 | 22 | #include "object-store.h" |
960786e7 RD |
23 | #include "color.h" |
24 | ||
25 | static int transport_use_color = -1; | |
26 | static char transport_colors[][COLOR_MAXLEN] = { | |
27 | GIT_COLOR_RESET, | |
28 | GIT_COLOR_RED /* REJECTED */ | |
29 | }; | |
30 | ||
31 | enum color_transport { | |
32 | TRANSPORT_COLOR_RESET = 0, | |
33 | TRANSPORT_COLOR_REJECTED = 1 | |
34 | }; | |
35 | ||
36 | static int transport_color_config(void) | |
37 | { | |
38 | const char *keys[] = { | |
39 | "color.transport.reset", | |
40 | "color.transport.rejected" | |
41 | }, *key = "color.transport"; | |
42 | char *value; | |
43 | int i; | |
44 | static int initialized; | |
45 | ||
46 | if (initialized) | |
47 | return 0; | |
48 | initialized = 1; | |
49 | ||
50 | if (!git_config_get_string(key, &value)) | |
51 | transport_use_color = git_config_colorbool(key, value); | |
52 | ||
53 | if (!want_color_stderr(transport_use_color)) | |
54 | return 0; | |
55 | ||
56 | for (i = 0; i < ARRAY_SIZE(keys); i++) | |
57 | if (!git_config_get_string(keys[i], &value)) { | |
58 | if (!value) | |
59 | return config_error_nonbool(keys[i]); | |
60 | if (color_parse(value, transport_colors[i]) < 0) | |
61 | return -1; | |
62 | } | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
67 | static const char *transport_get_color(enum color_transport ix) | |
68 | { | |
69 | if (want_color_stderr(transport_use_color)) | |
70 | return transport_colors[ix]; | |
71 | return ""; | |
72 | } | |
cd547b48 | 73 | |
e9fcd1e2 IL |
74 | static void set_upstreams(struct transport *transport, struct ref *refs, |
75 | int pretend) | |
76 | { | |
77 | struct ref *ref; | |
78 | for (ref = refs; ref; ref = ref->next) { | |
79 | const char *localname; | |
80 | const char *tmp; | |
81 | const char *remotename; | |
e9fcd1e2 IL |
82 | int flag = 0; |
83 | /* | |
84 | * Check suitability for tracking. Must be successful / | |
85 | * already up-to-date ref create/modify (not delete). | |
86 | */ | |
87 | if (ref->status != REF_STATUS_OK && | |
88 | ref->status != REF_STATUS_UPTODATE) | |
89 | continue; | |
90 | if (!ref->peer_ref) | |
91 | continue; | |
f4e54d02 | 92 | if (is_null_oid(&ref->new_oid)) |
e9fcd1e2 IL |
93 | continue; |
94 | ||
95 | /* Follow symbolic refs (mainly for HEAD). */ | |
96 | localname = ref->peer_ref->name; | |
97 | remotename = ref->name; | |
7695d118 | 98 | tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, |
744c040b | 99 | NULL, &flag); |
e9fcd1e2 | 100 | if (tmp && flag & REF_ISSYMREF && |
59556548 | 101 | starts_with(tmp, "refs/heads/")) |
e9fcd1e2 IL |
102 | localname = tmp; |
103 | ||
104 | /* Both source and destination must be local branches. */ | |
59556548 | 105 | if (!localname || !starts_with(localname, "refs/heads/")) |
e9fcd1e2 | 106 | continue; |
59556548 | 107 | if (!remotename || !starts_with(remotename, "refs/heads/")) |
e9fcd1e2 IL |
108 | continue; |
109 | ||
110 | if (!pretend) | |
111 | install_branch_config(BRANCH_CONFIG_VERBOSE, | |
112 | localname + 11, transport->remote->name, | |
113 | remotename); | |
114 | else | |
7ab1d44f | 115 | printf(_("Would set upstream of '%s' to '%s' of '%s'\n"), |
e9fcd1e2 IL |
116 | localname + 11, remotename + 11, |
117 | transport->remote->name); | |
118 | } | |
119 | } | |
120 | ||
c7a8a162 JS |
121 | struct bundle_transport_data { |
122 | int fd; | |
123 | struct bundle_header header; | |
124 | }; | |
125 | ||
834cf34b BW |
126 | static struct ref *get_refs_from_bundle(struct transport *transport, |
127 | int for_push, | |
128 | const struct argv_array *ref_prefixes) | |
c7a8a162 JS |
129 | { |
130 | struct bundle_transport_data *data = transport->data; | |
131 | struct ref *result = NULL; | |
132 | int i; | |
133 | ||
64fcef2d DB |
134 | if (for_push) |
135 | return NULL; | |
136 | ||
c7a8a162 JS |
137 | if (data->fd > 0) |
138 | close(data->fd); | |
139 | data->fd = read_bundle_header(transport->url, &data->header); | |
140 | if (data->fd < 0) | |
141 | die ("Could not read bundle '%s'.", transport->url); | |
142 | for (i = 0; i < data->header.references.nr; i++) { | |
143 | struct ref_list_entry *e = data->header.references.list + i; | |
59c69c0c | 144 | struct ref *ref = alloc_ref(e->name); |
b8607f35 | 145 | oidcpy(&ref->old_oid, &e->oid); |
c7a8a162 JS |
146 | ref->next = result; |
147 | result = ref; | |
148 | } | |
149 | return result; | |
150 | } | |
151 | ||
1788c39c | 152 | static int fetch_refs_from_bundle(struct transport *transport, |
37148311 | 153 | int nr_heads, struct ref **to_fetch) |
c7a8a162 JS |
154 | { |
155 | struct bundle_transport_data *data = transport->data; | |
be042aff JH |
156 | return unbundle(&data->header, data->fd, |
157 | transport->progress ? BUNDLE_VERBOSE : 0); | |
c7a8a162 JS |
158 | } |
159 | ||
160 | static int close_bundle(struct transport *transport) | |
161 | { | |
162 | struct bundle_transport_data *data = transport->data; | |
163 | if (data->fd > 0) | |
164 | close(data->fd); | |
f4e95765 | 165 | free(data); |
c7a8a162 JS |
166 | return 0; |
167 | } | |
168 | ||
9b288516 | 169 | struct git_transport_data { |
aa5af974 | 170 | struct git_transport_options options; |
ba227857 DB |
171 | struct child_process *conn; |
172 | int fd[2]; | |
61b075bd | 173 | unsigned got_remote_heads : 1; |
432e9565 | 174 | enum protocol_version version; |
910650d2 | 175 | struct oid_array extra_have; |
176 | struct oid_array shallow; | |
9b288516 DB |
177 | }; |
178 | ||
aa5af974 | 179 | static int set_git_option(struct git_transport_options *opts, |
9b288516 DB |
180 | const char *name, const char *value) |
181 | { | |
c29727d5 | 182 | if (!strcmp(name, TRANS_OPT_UPLOADPACK)) { |
aa5af974 | 183 | opts->uploadpack = value; |
c29727d5 DB |
184 | return 0; |
185 | } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) { | |
aa5af974 | 186 | opts->receivepack = value; |
9b288516 DB |
187 | return 0; |
188 | } else if (!strcmp(name, TRANS_OPT_THIN)) { | |
aa5af974 | 189 | opts->thin = !!value; |
9b288516 | 190 | return 0; |
41fa7d2e | 191 | } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) { |
aa5af974 | 192 | opts->followtags = !!value; |
41fa7d2e | 193 | return 0; |
c29727d5 | 194 | } else if (!strcmp(name, TRANS_OPT_KEEP)) { |
aa5af974 | 195 | opts->keep = !!value; |
c29727d5 | 196 | return 0; |
48d25cae NTND |
197 | } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) { |
198 | opts->update_shallow = !!value; | |
199 | return 0; | |
c29727d5 DB |
200 | } else if (!strcmp(name, TRANS_OPT_DEPTH)) { |
201 | if (!value) | |
aa5af974 | 202 | opts->depth = 0; |
e7622ce8 NTND |
203 | else { |
204 | char *end; | |
205 | opts->depth = strtol(value, &end, 0); | |
206 | if (*end) | |
7ab1d44f | 207 | die(_("transport: invalid depth option '%s'"), value); |
e7622ce8 | 208 | } |
c29727d5 | 209 | return 0; |
508ea882 NTND |
210 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) { |
211 | opts->deepen_since = value; | |
212 | return 0; | |
a45a2600 NTND |
213 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) { |
214 | opts->deepen_not = (const struct string_list *)value; | |
215 | return 0; | |
cccf74e2 NTND |
216 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) { |
217 | opts->deepen_relative = !!value; | |
218 | return 0; | |
88e2f9ed JT |
219 | } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) { |
220 | opts->from_promisor = !!value; | |
221 | return 0; | |
222 | } else if (!strcmp(name, TRANS_OPT_NO_DEPENDENTS)) { | |
223 | opts->no_dependents = !!value; | |
224 | return 0; | |
640d8b72 JH |
225 | } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) { |
226 | parse_list_objects_filter(&opts->filter_options, value); | |
227 | return 0; | |
9b288516 DB |
228 | } |
229 | return 1; | |
230 | } | |
231 | ||
f3ee9ca5 | 232 | static int connect_setup(struct transport *transport, int for_push) |
ba227857 DB |
233 | { |
234 | struct git_transport_data *data = transport->data; | |
f3ee9ca5 | 235 | int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0; |
61b075bd IL |
236 | |
237 | if (data->conn) | |
238 | return 0; | |
239 | ||
c915f11e EW |
240 | switch (transport->family) { |
241 | case TRANSPORT_FAMILY_ALL: break; | |
242 | case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break; | |
243 | case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break; | |
244 | } | |
245 | ||
5a277f3f JH |
246 | data->conn = git_connect(data->fd, transport->url, |
247 | for_push ? data->options.receivepack : | |
248 | data->options.uploadpack, | |
f3ee9ca5 | 249 | flags); |
61b075bd | 250 | |
ba227857 DB |
251 | return 0; |
252 | } | |
253 | ||
834cf34b BW |
254 | static struct ref *get_refs_via_connect(struct transport *transport, int for_push, |
255 | const struct argv_array *ref_prefixes) | |
c29727d5 DB |
256 | { |
257 | struct git_transport_data *data = transport->data; | |
ad6ac124 BW |
258 | struct ref *refs = NULL; |
259 | struct packet_reader reader; | |
c29727d5 | 260 | |
f3ee9ca5 | 261 | connect_setup(transport, for_push); |
ad6ac124 BW |
262 | |
263 | packet_reader_init(&reader, data->fd[0], NULL, 0, | |
264 | PACKET_READ_CHOMP_NEWLINE | | |
265 | PACKET_READ_GENTLE_ON_EOF); | |
266 | ||
432e9565 BW |
267 | data->version = discover_version(&reader); |
268 | switch (data->version) { | |
8f6982b4 | 269 | case protocol_v2: |
834cf34b BW |
270 | get_remote_refs(data->fd[1], &reader, &refs, for_push, |
271 | ref_prefixes); | |
8f6982b4 | 272 | break; |
ad6ac124 BW |
273 | case protocol_v1: |
274 | case protocol_v0: | |
275 | get_remote_heads(&reader, &refs, | |
276 | for_push ? REF_NORMAL : 0, | |
277 | &data->extra_have, | |
278 | &data->shallow); | |
279 | break; | |
280 | case protocol_unknown_version: | |
281 | BUG("unknown protocol version"); | |
282 | } | |
61b075bd | 283 | data->got_remote_heads = 1; |
c29727d5 DB |
284 | |
285 | return refs; | |
286 | } | |
287 | ||
1788c39c | 288 | static int fetch_refs_via_pack(struct transport *transport, |
37148311 | 289 | int nr_heads, struct ref **to_fetch) |
c29727d5 | 290 | { |
e70a65c5 | 291 | int ret = 0; |
c29727d5 | 292 | struct git_transport_data *data = transport->data; |
432e9565 | 293 | struct ref *refs = NULL; |
c29727d5 DB |
294 | char *dest = xstrdup(transport->url); |
295 | struct fetch_pack_args args; | |
00183cbb | 296 | struct ref *refs_tmp = NULL; |
c29727d5 | 297 | |
fa740529 | 298 | memset(&args, 0, sizeof(args)); |
aa5af974 IL |
299 | args.uploadpack = data->options.uploadpack; |
300 | args.keep_pack = data->options.keep; | |
fa740529 | 301 | args.lock_pack = 1; |
aa5af974 IL |
302 | args.use_thin_pack = data->options.thin; |
303 | args.include_tag = data->options.followtags; | |
bd2c86ef | 304 | args.verbose = (transport->verbose > 1); |
fe8aa148 | 305 | args.quiet = (transport->verbose < 0); |
d01b3c02 | 306 | args.no_progress = !transport->progress; |
aa5af974 | 307 | args.depth = data->options.depth; |
508ea882 | 308 | args.deepen_since = data->options.deepen_since; |
a45a2600 | 309 | args.deepen_not = data->options.deepen_not; |
cccf74e2 | 310 | args.deepen_relative = data->options.deepen_relative; |
c6807a40 NTND |
311 | args.check_self_contained_and_connected = |
312 | data->options.check_self_contained_and_connected; | |
beea4152 | 313 | args.cloning = transport->cloning; |
48d25cae | 314 | args.update_shallow = data->options.update_shallow; |
88e2f9ed JT |
315 | args.from_promisor = data->options.from_promisor; |
316 | args.no_dependents = data->options.no_dependents; | |
640d8b72 | 317 | args.filter_options = data->options.filter_options; |
edc9caf7 | 318 | args.stateless_rpc = transport->stateless_rpc; |
c29727d5 | 319 | |
635365eb | 320 | if (!data->got_remote_heads) |
834cf34b | 321 | refs_tmp = get_refs_via_connect(transport, 0, NULL); |
ba227857 | 322 | |
432e9565 | 323 | switch (data->version) { |
8f6982b4 | 324 | case protocol_v2: |
685fbd32 BW |
325 | refs = fetch_pack(&args, data->fd, data->conn, |
326 | refs_tmp ? refs_tmp : transport->remote_refs, | |
327 | dest, to_fetch, nr_heads, &data->shallow, | |
328 | &transport->pack_lockfile, data->version); | |
8f6982b4 | 329 | break; |
432e9565 BW |
330 | case protocol_v1: |
331 | case protocol_v0: | |
332 | refs = fetch_pack(&args, data->fd, data->conn, | |
333 | refs_tmp ? refs_tmp : transport->remote_refs, | |
334 | dest, to_fetch, nr_heads, &data->shallow, | |
685fbd32 | 335 | &transport->pack_lockfile, data->version); |
432e9565 BW |
336 | break; |
337 | case protocol_unknown_version: | |
338 | BUG("unknown protocol version"); | |
ba227857 DB |
339 | } |
340 | ||
ba227857 DB |
341 | close(data->fd[0]); |
342 | close(data->fd[1]); | |
e70a65c5 MM |
343 | if (finish_connect(data->conn)) |
344 | ret = -1; | |
ba227857 | 345 | data->conn = NULL; |
61b075bd | 346 | data->got_remote_heads = 0; |
c6807a40 NTND |
347 | data->options.self_contained_and_connected = |
348 | args.self_contained_and_connected; | |
c29727d5 | 349 | |
e70a65c5 MM |
350 | if (refs == NULL) |
351 | ret = -1; | |
352 | if (report_unmatched_refs(to_fetch, nr_heads)) | |
353 | ret = -1; | |
354 | ||
00183cbb | 355 | free_refs(refs_tmp); |
626df76e | 356 | free_refs(refs); |
c29727d5 | 357 | free(dest); |
e70a65c5 | 358 | return ret; |
c29727d5 DB |
359 | } |
360 | ||
481c7a6d JK |
361 | static int push_had_errors(struct ref *ref) |
362 | { | |
363 | for (; ref; ref = ref->next) { | |
364 | switch (ref->status) { | |
365 | case REF_STATUS_NONE: | |
366 | case REF_STATUS_UPTODATE: | |
367 | case REF_STATUS_OK: | |
368 | break; | |
369 | default: | |
370 | return 1; | |
371 | } | |
372 | } | |
373 | return 0; | |
374 | } | |
375 | ||
f1863d0d | 376 | int transport_refs_pushed(struct ref *ref) |
64fcef2d DB |
377 | { |
378 | for (; ref; ref = ref->next) { | |
379 | switch(ref->status) { | |
380 | case REF_STATUS_NONE: | |
381 | case REF_STATUS_UPTODATE: | |
382 | break; | |
383 | default: | |
384 | return 1; | |
385 | } | |
386 | } | |
387 | return 0; | |
388 | } | |
389 | ||
f1863d0d | 390 | void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose) |
64fcef2d DB |
391 | { |
392 | struct refspec rs; | |
393 | ||
394 | if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE) | |
395 | return; | |
396 | ||
397 | rs.src = ref->name; | |
398 | rs.dst = NULL; | |
399 | ||
400 | if (!remote_find_tracking(remote, &rs)) { | |
401 | if (verbose) | |
402 | fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); | |
403 | if (ref->deletion) { | |
755b49ae | 404 | delete_ref(NULL, rs.dst, NULL, 0); |
64fcef2d | 405 | } else |
ae077771 | 406 | update_ref("update by push", rs.dst, &ref->new_oid, |
407 | NULL, 0, 0); | |
64fcef2d DB |
408 | free(rs.dst); |
409 | } | |
410 | } | |
411 | ||
7101e10c JH |
412 | static void print_ref_status(char flag, const char *summary, |
413 | struct ref *to, struct ref *from, const char *msg, | |
414 | int porcelain, int summary_width) | |
64fcef2d | 415 | { |
1965ff74 LA |
416 | if (porcelain) { |
417 | if (from) | |
418 | fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to->name); | |
419 | else | |
420 | fprintf(stdout, "%c\t:%s\t", flag, to->name); | |
421 | if (msg) | |
422 | fprintf(stdout, "%s (%s)\n", summary, msg); | |
423 | else | |
424 | fprintf(stdout, "%s\n", summary); | |
425 | } else { | |
960786e7 RD |
426 | const char *red = "", *reset = ""; |
427 | if (push_had_errors(to)) { | |
428 | red = transport_get_color(TRANSPORT_COLOR_REJECTED); | |
429 | reset = transport_get_color(TRANSPORT_COLOR_RESET); | |
430 | } | |
431 | fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width, | |
432 | summary, reset); | |
1965ff74 LA |
433 | if (from) |
434 | fprintf(stderr, "%s -> %s", prettify_refname(from->name), prettify_refname(to->name)); | |
435 | else | |
436 | fputs(prettify_refname(to->name), stderr); | |
437 | if (msg) { | |
438 | fputs(" (", stderr); | |
439 | fputs(msg, stderr); | |
440 | fputc(')', stderr); | |
441 | } | |
442 | fputc('\n', stderr); | |
64fcef2d | 443 | } |
64fcef2d DB |
444 | } |
445 | ||
7101e10c | 446 | static void print_ok_ref_status(struct ref *ref, int porcelain, int summary_width) |
64fcef2d DB |
447 | { |
448 | if (ref->deletion) | |
7101e10c JH |
449 | print_ref_status('-', "[deleted]", ref, NULL, NULL, |
450 | porcelain, summary_width); | |
f4e54d02 | 451 | else if (is_null_oid(&ref->old_oid)) |
64fcef2d | 452 | print_ref_status('*', |
59556548 | 453 | (starts_with(ref->name, "refs/tags/") ? "[new tag]" : |
1965ff74 | 454 | "[new branch]"), |
7101e10c | 455 | ref, ref->peer_ref, NULL, porcelain, summary_width); |
64fcef2d | 456 | else { |
bd22d4ff | 457 | struct strbuf quickref = STRBUF_INIT; |
64fcef2d DB |
458 | char type; |
459 | const char *msg; | |
460 | ||
30e677e0 | 461 | strbuf_add_unique_abbrev(&quickref, &ref->old_oid, |
1eb47f16 | 462 | DEFAULT_ABBREV); |
5ece083f | 463 | if (ref->forced_update) { |
bd22d4ff | 464 | strbuf_addstr(&quickref, "..."); |
64fcef2d DB |
465 | type = '+'; |
466 | msg = "forced update"; | |
467 | } else { | |
bd22d4ff | 468 | strbuf_addstr(&quickref, ".."); |
64fcef2d DB |
469 | type = ' '; |
470 | msg = NULL; | |
471 | } | |
30e677e0 | 472 | strbuf_add_unique_abbrev(&quickref, &ref->new_oid, |
1eb47f16 | 473 | DEFAULT_ABBREV); |
64fcef2d | 474 | |
7101e10c JH |
475 | print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, |
476 | porcelain, summary_width); | |
bd22d4ff | 477 | strbuf_release(&quickref); |
64fcef2d DB |
478 | } |
479 | } | |
480 | ||
7101e10c JH |
481 | static int print_one_push_status(struct ref *ref, const char *dest, int count, |
482 | int porcelain, int summary_width) | |
64fcef2d | 483 | { |
882d49ca JK |
484 | if (!count) { |
485 | char *url = transport_anonymize_url(dest); | |
486 | fprintf(porcelain ? stdout : stderr, "To %s\n", url); | |
487 | free(url); | |
488 | } | |
64fcef2d DB |
489 | |
490 | switch(ref->status) { | |
491 | case REF_STATUS_NONE: | |
7101e10c JH |
492 | print_ref_status('X', "[no match]", ref, NULL, NULL, |
493 | porcelain, summary_width); | |
64fcef2d DB |
494 | break; |
495 | case REF_STATUS_REJECT_NODELETE: | |
496 | print_ref_status('!', "[rejected]", ref, NULL, | |
7101e10c JH |
497 | "remote does not support deleting refs", |
498 | porcelain, summary_width); | |
64fcef2d DB |
499 | break; |
500 | case REF_STATUS_UPTODATE: | |
501 | print_ref_status('=', "[up to date]", ref, | |
7101e10c | 502 | ref->peer_ref, NULL, porcelain, summary_width); |
64fcef2d DB |
503 | break; |
504 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
505 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 506 | "non-fast-forward", porcelain, summary_width); |
64fcef2d | 507 | break; |
dbfeddb1 CR |
508 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
509 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 510 | "already exists", porcelain, summary_width); |
dbfeddb1 | 511 | break; |
75e5c0dc JH |
512 | case REF_STATUS_REJECT_FETCH_FIRST: |
513 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 514 | "fetch first", porcelain, summary_width); |
75e5c0dc JH |
515 | break; |
516 | case REF_STATUS_REJECT_NEEDS_FORCE: | |
517 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 518 | "needs force", porcelain, summary_width); |
75e5c0dc | 519 | break; |
631b5ef2 JH |
520 | case REF_STATUS_REJECT_STALE: |
521 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 522 | "stale info", porcelain, summary_width); |
631b5ef2 | 523 | break; |
4820a33b NTND |
524 | case REF_STATUS_REJECT_SHALLOW: |
525 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c JH |
526 | "new shallow roots not allowed", |
527 | porcelain, summary_width); | |
4820a33b | 528 | break; |
64fcef2d DB |
529 | case REF_STATUS_REMOTE_REJECT: |
530 | print_ref_status('!', "[remote rejected]", ref, | |
7101e10c JH |
531 | ref->deletion ? NULL : ref->peer_ref, |
532 | ref->remote_status, porcelain, summary_width); | |
64fcef2d DB |
533 | break; |
534 | case REF_STATUS_EXPECTING_REPORT: | |
535 | print_ref_status('!', "[remote failure]", ref, | |
7101e10c JH |
536 | ref->deletion ? NULL : ref->peer_ref, |
537 | "remote failed to report status", | |
538 | porcelain, summary_width); | |
64fcef2d | 539 | break; |
4ff17f10 RS |
540 | case REF_STATUS_ATOMIC_PUSH_FAILED: |
541 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 542 | "atomic push failed", porcelain, summary_width); |
4ff17f10 | 543 | break; |
64fcef2d | 544 | case REF_STATUS_OK: |
7101e10c | 545 | print_ok_ref_status(ref, porcelain, summary_width); |
64fcef2d DB |
546 | break; |
547 | } | |
548 | ||
549 | return 1; | |
550 | } | |
551 | ||
db98d9ba JH |
552 | static int measure_abbrev(const struct object_id *oid, int sofar) |
553 | { | |
dc01505f | 554 | char hex[GIT_MAX_HEXSZ + 1]; |
aab9583f | 555 | int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV); |
db98d9ba JH |
556 | |
557 | return (w < sofar) ? sofar : w; | |
558 | } | |
559 | ||
11fd66de JH |
560 | int transport_summary_width(const struct ref *refs) |
561 | { | |
db98d9ba JH |
562 | int maxw = -1; |
563 | ||
564 | for (; refs; refs = refs->next) { | |
565 | maxw = measure_abbrev(&refs->old_oid, maxw); | |
566 | maxw = measure_abbrev(&refs->new_oid, maxw); | |
567 | } | |
568 | if (maxw < 0) | |
569 | maxw = FALLBACK_DEFAULT_ABBREV; | |
570 | return (2 * maxw + 3); | |
11fd66de JH |
571 | } |
572 | ||
f1863d0d | 573 | void transport_print_push_status(const char *dest, struct ref *refs, |
10643d4e | 574 | int verbose, int porcelain, unsigned int *reject_reasons) |
64fcef2d DB |
575 | { |
576 | struct ref *ref; | |
577 | int n = 0; | |
f25950f3 | 578 | char *head; |
11fd66de | 579 | int summary_width = transport_summary_width(refs); |
f25950f3 | 580 | |
960786e7 RD |
581 | if (transport_color_config() < 0) |
582 | warning(_("could not parse transport.color.* config")); | |
583 | ||
efbd4fdf | 584 | head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); |
64fcef2d DB |
585 | |
586 | if (verbose) { | |
587 | for (ref = refs; ref; ref = ref->next) | |
588 | if (ref->status == REF_STATUS_UPTODATE) | |
7101e10c JH |
589 | n += print_one_push_status(ref, dest, n, |
590 | porcelain, summary_width); | |
64fcef2d DB |
591 | } |
592 | ||
593 | for (ref = refs; ref; ref = ref->next) | |
594 | if (ref->status == REF_STATUS_OK) | |
7101e10c JH |
595 | n += print_one_push_status(ref, dest, n, |
596 | porcelain, summary_width); | |
64fcef2d | 597 | |
10643d4e | 598 | *reject_reasons = 0; |
64fcef2d DB |
599 | for (ref = refs; ref; ref = ref->next) { |
600 | if (ref->status != REF_STATUS_NONE && | |
601 | ref->status != REF_STATUS_UPTODATE && | |
602 | ref->status != REF_STATUS_OK) | |
7101e10c JH |
603 | n += print_one_push_status(ref, dest, n, |
604 | porcelain, summary_width); | |
10643d4e | 605 | if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) { |
1d2c14df | 606 | if (head != NULL && !strcmp(head, ref->name)) |
10643d4e | 607 | *reject_reasons |= REJECT_NON_FF_HEAD; |
f25950f3 | 608 | else |
10643d4e | 609 | *reject_reasons |= REJECT_NON_FF_OTHER; |
dbfeddb1 CR |
610 | } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) { |
611 | *reject_reasons |= REJECT_ALREADY_EXISTS; | |
75e5c0dc JH |
612 | } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) { |
613 | *reject_reasons |= REJECT_FETCH_FIRST; | |
614 | } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) { | |
615 | *reject_reasons |= REJECT_NEEDS_FORCE; | |
f25950f3 | 616 | } |
64fcef2d | 617 | } |
dc76c7f7 | 618 | free(head); |
64fcef2d DB |
619 | } |
620 | ||
f1863d0d | 621 | void transport_verify_remote_names(int nr_heads, const char **heads) |
64fcef2d DB |
622 | { |
623 | int i; | |
624 | ||
625 | for (i = 0; i < nr_heads; i++) { | |
626 | const char *local = heads[i]; | |
627 | const char *remote = strrchr(heads[i], ':'); | |
628 | ||
629 | if (*local == '+') | |
630 | local++; | |
631 | ||
632 | /* A matching refspec is okay. */ | |
633 | if (remote == local && remote[1] == '\0') | |
634 | continue; | |
635 | ||
636 | remote = remote ? (remote + 1) : local; | |
8d9c5010 MH |
637 | if (check_refname_format(remote, |
638 | REFNAME_ALLOW_ONELEVEL|REFNAME_REFSPEC_PATTERN)) | |
639 | die("remote part of refspec is not a valid name in %s", | |
640 | heads[i]); | |
64fcef2d DB |
641 | } |
642 | } | |
643 | ||
644 | static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags) | |
f3fa1838 | 645 | { |
9b288516 | 646 | struct git_transport_data *data = transport->data; |
40cb4fab | 647 | struct send_pack_args args; |
432e9565 | 648 | int ret = 0; |
64fcef2d | 649 | |
960786e7 RD |
650 | if (transport_color_config() < 0) |
651 | return -1; | |
652 | ||
635365eb | 653 | if (!data->got_remote_heads) |
834cf34b | 654 | get_refs_via_connect(transport, 1, NULL); |
9b288516 | 655 | |
de1a2fdd | 656 | memset(&args, 0, sizeof(args)); |
94c89ba6 | 657 | args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); |
40cb4fab | 658 | args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); |
aa5af974 | 659 | args.use_thin_pack = data->options.thin; |
8afd8dc0 TRC |
660 | args.verbose = (transport->verbose > 0); |
661 | args.quiet = (transport->verbose < 0); | |
d7c411b7 | 662 | args.progress = transport->progress; |
40cb4fab | 663 | args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); |
77555854 | 664 | args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN); |
d0e8e09c | 665 | args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC); |
f6a4e61f | 666 | args.push_options = transport->push_options; |
9be89160 | 667 | args.url = transport->url; |
40cb4fab | 668 | |
30261094 DB |
669 | if (flags & TRANSPORT_PUSH_CERT_ALWAYS) |
670 | args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; | |
671 | else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) | |
672 | args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; | |
673 | else | |
674 | args.push_cert = SEND_PACK_PUSH_CERT_NEVER; | |
675 | ||
432e9565 | 676 | switch (data->version) { |
8f6982b4 BW |
677 | case protocol_v2: |
678 | die("support for protocol v2 not implemented yet"); | |
679 | break; | |
432e9565 BW |
680 | case protocol_v1: |
681 | case protocol_v0: | |
682 | ret = send_pack(&args, data->fd, data->conn, remote_refs, | |
683 | &data->extra_have); | |
684 | break; | |
685 | case protocol_unknown_version: | |
686 | BUG("unknown protocol version"); | |
687 | } | |
64fcef2d DB |
688 | |
689 | close(data->fd[1]); | |
690 | close(data->fd[0]); | |
691 | ret |= finish_connect(data->conn); | |
692 | data->conn = NULL; | |
61b075bd | 693 | data->got_remote_heads = 0; |
64fcef2d DB |
694 | |
695 | return ret; | |
9b288516 DB |
696 | } |
697 | ||
b236752a IL |
698 | static int connect_git(struct transport *transport, const char *name, |
699 | const char *executable, int fd[2]) | |
700 | { | |
701 | struct git_transport_data *data = transport->data; | |
702 | data->conn = git_connect(data->fd, transport->url, | |
703 | executable, 0); | |
704 | fd[0] = data->fd[0]; | |
705 | fd[1] = data->fd[1]; | |
706 | return 0; | |
707 | } | |
708 | ||
f4e95765 SP |
709 | static int disconnect_git(struct transport *transport) |
710 | { | |
ba227857 DB |
711 | struct git_transport_data *data = transport->data; |
712 | if (data->conn) { | |
61b075bd IL |
713 | if (data->got_remote_heads) |
714 | packet_flush(data->fd[1]); | |
ba227857 DB |
715 | close(data->fd[0]); |
716 | close(data->fd[1]); | |
717 | finish_connect(data->conn); | |
718 | } | |
719 | ||
720 | free(data); | |
f4e95765 SP |
721 | return 0; |
722 | } | |
723 | ||
e967ca38 JT |
724 | static struct transport_vtable taken_over_vtable = { |
725 | NULL, | |
726 | get_refs_via_connect, | |
727 | fetch_refs_via_pack, | |
728 | git_transport_push, | |
729 | NULL, | |
730 | disconnect_git | |
731 | }; | |
732 | ||
61b075bd IL |
733 | void transport_take_over(struct transport *transport, |
734 | struct child_process *child) | |
735 | { | |
736 | struct git_transport_data *data; | |
737 | ||
738 | if (!transport->smart_options) | |
ef1177d1 | 739 | die("BUG: taking over transport requires non-NULL " |
61b075bd IL |
740 | "smart_options field."); |
741 | ||
742 | data = xcalloc(1, sizeof(*data)); | |
743 | data->options = *transport->smart_options; | |
744 | data->conn = child; | |
745 | data->fd[0] = data->conn->out; | |
746 | data->fd[1] = data->conn->in; | |
747 | data->got_remote_heads = 0; | |
748 | transport->data = data; | |
749 | ||
e967ca38 | 750 | transport->vtable = &taken_over_vtable; |
61b075bd | 751 | transport->smart_options = &(data->options); |
b26ed430 JH |
752 | |
753 | transport->cannot_reuse = 1; | |
61b075bd IL |
754 | } |
755 | ||
9b288516 DB |
756 | static int is_file(const char *url) |
757 | { | |
758 | struct stat buf; | |
759 | if (stat(url, &buf)) | |
760 | return 0; | |
761 | return S_ISREG(buf.st_mode); | |
762 | } | |
763 | ||
25d5cc48 IL |
764 | static int external_specification_len(const char *url) |
765 | { | |
766 | return strchr(url, ':') - url; | |
767 | } | |
768 | ||
5088d3b3 | 769 | static const struct string_list *protocol_whitelist(void) |
a5adaced | 770 | { |
5088d3b3 JK |
771 | static int enabled = -1; |
772 | static struct string_list allowed = STRING_LIST_INIT_DUP; | |
a5adaced | 773 | |
5088d3b3 JK |
774 | if (enabled < 0) { |
775 | const char *v = getenv("GIT_ALLOW_PROTOCOL"); | |
776 | if (v) { | |
777 | string_list_split(&allowed, v, ':', -1); | |
778 | string_list_sort(&allowed); | |
779 | enabled = 1; | |
780 | } else { | |
781 | enabled = 0; | |
782 | } | |
783 | } | |
784 | ||
785 | return enabled ? &allowed : NULL; | |
786 | } | |
a5adaced | 787 | |
f1762d77 BW |
788 | enum protocol_allow_config { |
789 | PROTOCOL_ALLOW_NEVER = 0, | |
790 | PROTOCOL_ALLOW_USER_ONLY, | |
791 | PROTOCOL_ALLOW_ALWAYS | |
792 | }; | |
793 | ||
794 | static enum protocol_allow_config parse_protocol_config(const char *key, | |
795 | const char *value) | |
5088d3b3 | 796 | { |
f1762d77 BW |
797 | if (!strcasecmp(value, "always")) |
798 | return PROTOCOL_ALLOW_ALWAYS; | |
799 | else if (!strcasecmp(value, "never")) | |
800 | return PROTOCOL_ALLOW_NEVER; | |
801 | else if (!strcasecmp(value, "user")) | |
802 | return PROTOCOL_ALLOW_USER_ONLY; | |
803 | ||
804 | die("unknown value for config '%s': %s", key, value); | |
5088d3b3 JK |
805 | } |
806 | ||
f1762d77 | 807 | static enum protocol_allow_config get_protocol_config(const char *type) |
5088d3b3 | 808 | { |
f1762d77 BW |
809 | char *key = xstrfmt("protocol.%s.allow", type); |
810 | char *value; | |
811 | ||
812 | /* first check the per-protocol config */ | |
813 | if (!git_config_get_string(key, &value)) { | |
814 | enum protocol_allow_config ret = | |
815 | parse_protocol_config(key, value); | |
816 | free(key); | |
817 | free(value); | |
818 | return ret; | |
819 | } | |
820 | free(key); | |
821 | ||
822 | /* if defined, fallback to user-defined default for unknown protocols */ | |
823 | if (!git_config_get_string("protocol.allow", &value)) { | |
824 | enum protocol_allow_config ret = | |
825 | parse_protocol_config("protocol.allow", value); | |
826 | free(value); | |
827 | return ret; | |
828 | } | |
829 | ||
830 | /* fallback to built-in defaults */ | |
831 | /* known safe */ | |
832 | if (!strcmp(type, "http") || | |
833 | !strcmp(type, "https") || | |
834 | !strcmp(type, "git") || | |
835 | !strcmp(type, "ssh") || | |
836 | !strcmp(type, "file")) | |
837 | return PROTOCOL_ALLOW_ALWAYS; | |
838 | ||
839 | /* known scary; err on the side of caution */ | |
840 | if (!strcmp(type, "ext")) | |
841 | return PROTOCOL_ALLOW_NEVER; | |
842 | ||
843 | /* unknown; by default let them be used only directly by the user */ | |
844 | return PROTOCOL_ALLOW_USER_ONLY; | |
5088d3b3 JK |
845 | } |
846 | ||
a768a022 | 847 | int is_transport_allowed(const char *type, int from_user) |
5088d3b3 | 848 | { |
f1762d77 BW |
849 | const struct string_list *whitelist = protocol_whitelist(); |
850 | if (whitelist) | |
851 | return string_list_has_string(whitelist, type); | |
852 | ||
853 | switch (get_protocol_config(type)) { | |
854 | case PROTOCOL_ALLOW_ALWAYS: | |
855 | return 1; | |
856 | case PROTOCOL_ALLOW_NEVER: | |
857 | return 0; | |
858 | case PROTOCOL_ALLOW_USER_ONLY: | |
a768a022 BW |
859 | if (from_user < 0) |
860 | from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1); | |
861 | return from_user; | |
f1762d77 BW |
862 | } |
863 | ||
864 | die("BUG: invalid protocol_allow_config type"); | |
5088d3b3 JK |
865 | } |
866 | ||
867 | void transport_check_allowed(const char *type) | |
868 | { | |
a768a022 | 869 | if (!is_transport_allowed(type, -1)) |
a5adaced | 870 | die("transport '%s' not allowed", type); |
a5adaced JK |
871 | } |
872 | ||
e967ca38 JT |
873 | static struct transport_vtable bundle_vtable = { |
874 | NULL, | |
875 | get_refs_from_bundle, | |
876 | fetch_refs_from_bundle, | |
877 | NULL, | |
878 | NULL, | |
879 | close_bundle | |
880 | }; | |
881 | ||
882 | static struct transport_vtable builtin_smart_vtable = { | |
883 | NULL, | |
884 | get_refs_via_connect, | |
885 | fetch_refs_via_pack, | |
886 | git_transport_push, | |
887 | connect_git, | |
888 | disconnect_git | |
889 | }; | |
890 | ||
e5f4e214 | 891 | struct transport *transport_get(struct remote *remote, const char *url) |
9b288516 | 892 | { |
4da50460 | 893 | const char *helper; |
8eb554ae SP |
894 | struct transport *ret = xcalloc(1, sizeof(*ret)); |
895 | ||
d01b3c02 TRC |
896 | ret->progress = isatty(2); |
897 | ||
c1d45cf7 DB |
898 | if (!remote) |
899 | die("No remote provided to transport_get()"); | |
900 | ||
b0d66e15 | 901 | ret->got_remote_refs = 0; |
8eb554ae | 902 | ret->remote = remote; |
4da50460 | 903 | helper = remote->foreign_vcs; |
fb0cc87e | 904 | |
cb21d8f0 | 905 | if (!url && remote->url) |
fb0cc87e | 906 | url = remote->url[0]; |
8eb554ae | 907 | ret->url = url; |
8eb554ae | 908 | |
87422439 JS |
909 | /* maybe it is a foreign URL? */ |
910 | if (url) { | |
911 | const char *p = url; | |
912 | ||
638794cd | 913 | while (is_urlschemechar(p == url, *p)) |
87422439 | 914 | p++; |
59556548 | 915 | if (starts_with(p, "::")) |
4da50460 | 916 | helper = xstrndup(url, p - url); |
87422439 JS |
917 | } |
918 | ||
4da50460 IL |
919 | if (helper) { |
920 | transport_helper_init(ret, helper); | |
59556548 | 921 | } else if (starts_with(url, "rsync:")) { |
0d0bac67 | 922 | die("git-over-rsync is no longer supported"); |
c59ab2e5 | 923 | } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) { |
c7a8a162 | 924 | struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); |
a5adaced | 925 | transport_check_allowed("file"); |
c7a8a162 | 926 | ret->data = data; |
e967ca38 | 927 | ret->vtable = &bundle_vtable; |
aa5af974 | 928 | ret->smart_options = NULL; |
25d5cc48 | 929 | } else if (!is_url(url) |
59556548 CC |
930 | || starts_with(url, "file://") |
931 | || starts_with(url, "git://") | |
932 | || starts_with(url, "ssh://") | |
07c7782c CMN |
933 | || starts_with(url, "git+ssh://") /* deprecated - do not use */ |
934 | || starts_with(url, "ssh+git://") /* deprecated - do not use */ | |
935 | ) { | |
a5adaced JK |
936 | /* |
937 | * These are builtin smart transports; "allowed" transports | |
938 | * will be checked individually in git_connect. | |
939 | */ | |
9b288516 | 940 | struct git_transport_data *data = xcalloc(1, sizeof(*data)); |
9b288516 | 941 | ret->data = data; |
e967ca38 | 942 | ret->vtable = &builtin_smart_vtable; |
aa5af974 | 943 | ret->smart_options = &(data->options); |
824d5776 | 944 | |
ba227857 | 945 | data->conn = NULL; |
61b075bd | 946 | data->got_remote_heads = 0; |
25d5cc48 IL |
947 | } else { |
948 | /* Unknown protocol in URL. Pass to external handler. */ | |
949 | int len = external_specification_len(url); | |
6b33894f | 950 | char *handler = xmemdupz(url, len); |
25d5cc48 | 951 | transport_helper_init(ret, handler); |
9b288516 | 952 | } |
8eb554ae | 953 | |
aa5af974 IL |
954 | if (ret->smart_options) { |
955 | ret->smart_options->thin = 1; | |
956 | ret->smart_options->uploadpack = "git-upload-pack"; | |
957 | if (remote->uploadpack) | |
958 | ret->smart_options->uploadpack = remote->uploadpack; | |
959 | ret->smart_options->receivepack = "git-receive-pack"; | |
960 | if (remote->receivepack) | |
961 | ret->smart_options->receivepack = remote->receivepack; | |
962 | } | |
963 | ||
9b288516 DB |
964 | return ret; |
965 | } | |
966 | ||
967 | int transport_set_option(struct transport *transport, | |
968 | const char *name, const char *value) | |
969 | { | |
aa5af974 IL |
970 | int git_reports = 1, protocol_reports = 1; |
971 | ||
972 | if (transport->smart_options) | |
973 | git_reports = set_git_option(transport->smart_options, | |
974 | name, value); | |
975 | ||
e967ca38 JT |
976 | if (transport->vtable->set_option) |
977 | protocol_reports = transport->vtable->set_option(transport, | |
978 | name, value); | |
aa5af974 IL |
979 | |
980 | /* If either report is 0, report 0 (success). */ | |
981 | if (!git_reports || !protocol_reports) | |
982 | return 0; | |
983 | /* If either reports -1 (invalid value), report -1. */ | |
984 | if ((git_reports == -1) || (protocol_reports == -1)) | |
985 | return -1; | |
986 | /* Otherwise if both report unknown, report unknown. */ | |
ab865e6e | 987 | return 1; |
9b288516 DB |
988 | } |
989 | ||
d01b3c02 TRC |
990 | void transport_set_verbosity(struct transport *transport, int verbosity, |
991 | int force_progress) | |
bde873c5 | 992 | { |
bd2c86ef | 993 | if (verbosity >= 1) |
bde873c5 TRC |
994 | transport->verbose = verbosity <= 3 ? verbosity : 3; |
995 | if (verbosity < 0) | |
996 | transport->verbose = -1; | |
d01b3c02 TRC |
997 | |
998 | /** | |
999 | * Rules used to determine whether to report progress (processing aborts | |
1000 | * when a rule is satisfied): | |
1001 | * | |
01fdc21f CB |
1002 | * . Report progress, if force_progress is 1 (ie. --progress). |
1003 | * . Don't report progress, if force_progress is 0 (ie. --no-progress). | |
1004 | * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ). | |
1005 | * . Report progress if isatty(2) is 1. | |
d01b3c02 | 1006 | **/ |
01fdc21f CB |
1007 | if (force_progress >= 0) |
1008 | transport->progress = !!force_progress; | |
1009 | else | |
1010 | transport->progress = verbosity >= 0 && isatty(2); | |
bde873c5 TRC |
1011 | } |
1012 | ||
a762e51e HV |
1013 | static void die_with_unpushed_submodules(struct string_list *needs_pushing) |
1014 | { | |
1015 | int i; | |
1016 | ||
7ab1d44f VA |
1017 | fprintf(stderr, _("The following submodule paths contain changes that can\n" |
1018 | "not be found on any remote:\n")); | |
a762e51e | 1019 | for (i = 0; i < needs_pushing->nr; i++) |
5cb5fe4a | 1020 | fprintf(stderr, " %s\n", needs_pushing->items[i].string); |
7ab1d44f VA |
1021 | fprintf(stderr, _("\nPlease try\n\n" |
1022 | " git push --recurse-submodules=on-demand\n\n" | |
1023 | "or cd to the path and use\n\n" | |
1024 | " git push\n\n" | |
1025 | "to push them to a remote.\n\n")); | |
a762e51e HV |
1026 | |
1027 | string_list_clear(needs_pushing, 0); | |
1028 | ||
7ab1d44f | 1029 | die(_("Aborting.")); |
a762e51e HV |
1030 | } |
1031 | ||
ec55559f AS |
1032 | static int run_pre_push_hook(struct transport *transport, |
1033 | struct ref *remote_refs) | |
1034 | { | |
1035 | int ret = 0, x; | |
1036 | struct ref *r; | |
d3180279 | 1037 | struct child_process proc = CHILD_PROCESS_INIT; |
ec55559f AS |
1038 | struct strbuf buf; |
1039 | const char *argv[4]; | |
1040 | ||
1041 | if (!(argv[0] = find_hook("pre-push"))) | |
1042 | return 0; | |
1043 | ||
1044 | argv[1] = transport->remote->name; | |
1045 | argv[2] = transport->url; | |
1046 | argv[3] = NULL; | |
1047 | ||
ec55559f AS |
1048 | proc.argv = argv; |
1049 | proc.in = -1; | |
1050 | ||
1051 | if (start_command(&proc)) { | |
1052 | finish_command(&proc); | |
1053 | return -1; | |
1054 | } | |
1055 | ||
af65f68c CB |
1056 | sigchain_push(SIGPIPE, SIG_IGN); |
1057 | ||
ec55559f AS |
1058 | strbuf_init(&buf, 256); |
1059 | ||
1060 | for (r = remote_refs; r; r = r->next) { | |
1061 | if (!r->peer_ref) continue; | |
1062 | if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue; | |
631b5ef2 | 1063 | if (r->status == REF_STATUS_REJECT_STALE) continue; |
ec55559f AS |
1064 | if (r->status == REF_STATUS_UPTODATE) continue; |
1065 | ||
1066 | strbuf_reset(&buf); | |
1067 | strbuf_addf( &buf, "%s %s %s %s\n", | |
f4e54d02 | 1068 | r->peer_ref->name, oid_to_hex(&r->new_oid), |
1069 | r->name, oid_to_hex(&r->old_oid)); | |
ec55559f | 1070 | |
af65f68c CB |
1071 | if (write_in_full(proc.in, buf.buf, buf.len) < 0) { |
1072 | /* We do not mind if a hook does not read all refs. */ | |
1073 | if (errno != EPIPE) | |
1074 | ret = -1; | |
ec55559f AS |
1075 | break; |
1076 | } | |
1077 | } | |
1078 | ||
1079 | strbuf_release(&buf); | |
1080 | ||
1081 | x = close(proc.in); | |
1082 | if (!ret) | |
1083 | ret = x; | |
1084 | ||
af65f68c CB |
1085 | sigchain_pop(SIGPIPE); |
1086 | ||
ec55559f AS |
1087 | x = finish_command(&proc); |
1088 | if (!ret) | |
1089 | ret = x; | |
1090 | ||
1091 | return ret; | |
1092 | } | |
1093 | ||
9b288516 | 1094 | int transport_push(struct transport *transport, |
07436e43 | 1095 | int refspec_nr, const char **refspec, int flags, |
10643d4e | 1096 | unsigned int *reject_reasons) |
9b288516 | 1097 | { |
10643d4e | 1098 | *reject_reasons = 0; |
f1863d0d | 1099 | transport_verify_remote_names(refspec_nr, refspec); |
64fcef2d | 1100 | |
960786e7 RD |
1101 | if (transport_color_config() < 0) |
1102 | return -1; | |
1103 | ||
e967ca38 | 1104 | if (transport->vtable->push_refs) { |
ba928c13 | 1105 | struct ref *remote_refs; |
64fcef2d DB |
1106 | struct ref *local_refs = get_local_heads(); |
1107 | int match_flags = MATCH_REFS_NONE; | |
8afd8dc0 TRC |
1108 | int verbose = (transport->verbose > 0); |
1109 | int quiet = (transport->verbose < 0); | |
1965ff74 | 1110 | int porcelain = flags & TRANSPORT_PUSH_PORCELAIN; |
e9fcd1e2 | 1111 | int pretend = flags & TRANSPORT_PUSH_DRY_RUN; |
77555854 | 1112 | int push_ret, ret, err; |
5b872fff BW |
1113 | struct refspec *tmp_rs; |
1114 | struct argv_array ref_prefixes = ARGV_ARRAY_INIT; | |
1115 | int i; | |
64fcef2d | 1116 | |
ba928c13 JK |
1117 | if (check_push_refs(local_refs, refspec_nr, refspec) < 0) |
1118 | return -1; | |
1119 | ||
5b872fff BW |
1120 | tmp_rs = parse_push_refspec(refspec_nr, refspec); |
1121 | for (i = 0; i < refspec_nr; i++) { | |
1122 | const char *prefix = NULL; | |
1123 | ||
1124 | if (tmp_rs[i].dst) | |
1125 | prefix = tmp_rs[i].dst; | |
1126 | else if (tmp_rs[i].src && !tmp_rs[i].exact_sha1) | |
1127 | prefix = tmp_rs[i].src; | |
1128 | ||
1129 | if (prefix) { | |
1130 | const char *glob = strchr(prefix, '*'); | |
1131 | if (glob) | |
1132 | argv_array_pushf(&ref_prefixes, "%.*s", | |
1133 | (int)(glob - prefix), | |
1134 | prefix); | |
1135 | else | |
1136 | expand_ref_prefix(&ref_prefixes, prefix); | |
1137 | } | |
1138 | } | |
1139 | ||
1140 | remote_refs = transport->vtable->get_refs_list(transport, 1, | |
1141 | &ref_prefixes); | |
1142 | ||
1143 | argv_array_clear(&ref_prefixes); | |
1144 | free_refspec(refspec_nr, tmp_rs); | |
ba928c13 | 1145 | |
64fcef2d DB |
1146 | if (flags & TRANSPORT_PUSH_ALL) |
1147 | match_flags |= MATCH_REFS_ALL; | |
1148 | if (flags & TRANSPORT_PUSH_MIRROR) | |
1149 | match_flags |= MATCH_REFS_MIRROR; | |
6ddba5e2 FC |
1150 | if (flags & TRANSPORT_PUSH_PRUNE) |
1151 | match_flags |= MATCH_REFS_PRUNE; | |
c2aba155 JH |
1152 | if (flags & TRANSPORT_PUSH_FOLLOW_TAGS) |
1153 | match_flags |= MATCH_REFS_FOLLOW_TAGS; | |
64fcef2d | 1154 | |
29753cdd JH |
1155 | if (match_push_refs(local_refs, &remote_refs, |
1156 | refspec_nr, refspec, match_flags)) { | |
64fcef2d DB |
1157 | return -1; |
1158 | } | |
1159 | ||
91048a95 JH |
1160 | if (transport->smart_options && |
1161 | transport->smart_options->cas && | |
1162 | !is_empty_cas(transport->smart_options->cas)) | |
1163 | apply_push_cas(transport->smart_options->cas, | |
1164 | transport->remote, remote_refs); | |
1165 | ||
20e8b465 TRC |
1166 | set_ref_status_for_push(remote_refs, |
1167 | flags & TRANSPORT_PUSH_MIRROR, | |
1168 | flags & TRANSPORT_PUSH_FORCE); | |
1169 | ||
ec55559f AS |
1170 | if (!(flags & TRANSPORT_PUSH_NO_HOOK)) |
1171 | if (run_pre_push_hook(transport, remote_refs)) | |
1172 | return -1; | |
1173 | ||
225e8bf7 BW |
1174 | if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | |
1175 | TRANSPORT_RECURSE_SUBMODULES_ONLY)) && | |
1176 | !is_bare_repository()) { | |
d2b17b32 | 1177 | struct ref *ref = remote_refs; |
910650d2 | 1178 | struct oid_array commits = OID_ARRAY_INIT; |
9cfa1c26 | 1179 | |
d2b17b32 | 1180 | for (; ref; ref = ref->next) |
9cfa1c26 | 1181 | if (!is_null_oid(&ref->new_oid)) |
910650d2 | 1182 | oid_array_append(&commits, |
98a72ddc | 1183 | &ref->new_oid); |
9cfa1c26 | 1184 | |
0301c821 | 1185 | if (!push_unpushed_submodules(&commits, |
06bf4ad1 BW |
1186 | transport->remote, |
1187 | refspec, refspec_nr, | |
2a90556d | 1188 | transport->push_options, |
0301c821 | 1189 | pretend)) { |
910650d2 | 1190 | oid_array_clear(&commits); |
9cfa1c26 HV |
1191 | die("Failed to push all needed submodules!"); |
1192 | } | |
910650d2 | 1193 | oid_array_clear(&commits); |
eb21c732 HV |
1194 | } |
1195 | ||
0301c821 | 1196 | if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) || |
225e8bf7 BW |
1197 | ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | |
1198 | TRANSPORT_RECURSE_SUBMODULES_ONLY)) && | |
0301c821 | 1199 | !pretend)) && !is_bare_repository()) { |
d2b17b32 | 1200 | struct ref *ref = remote_refs; |
f93d7c6f | 1201 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; |
910650d2 | 1202 | struct oid_array commits = OID_ARRAY_INIT; |
a762e51e | 1203 | |
d2b17b32 | 1204 | for (; ref; ref = ref->next) |
9cfa1c26 | 1205 | if (!is_null_oid(&ref->new_oid)) |
910650d2 | 1206 | oid_array_append(&commits, |
98a72ddc | 1207 | &ref->new_oid); |
9cfa1c26 HV |
1208 | |
1209 | if (find_unpushed_submodules(&commits, transport->remote->name, | |
1210 | &needs_pushing)) { | |
910650d2 | 1211 | oid_array_clear(&commits); |
9cfa1c26 HV |
1212 | die_with_unpushed_submodules(&needs_pushing); |
1213 | } | |
1214 | string_list_clear(&needs_pushing, 0); | |
910650d2 | 1215 | oid_array_clear(&commits); |
d2b17b32 FG |
1216 | } |
1217 | ||
225e8bf7 | 1218 | if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) |
e967ca38 | 1219 | push_ret = transport->vtable->push_refs(transport, remote_refs, flags); |
225e8bf7 BW |
1220 | else |
1221 | push_ret = 0; | |
42328267 | 1222 | err = push_had_errors(remote_refs); |
77555854 | 1223 | ret = push_ret | err; |
64fcef2d | 1224 | |
42328267 | 1225 | if (!quiet || err) |
f1863d0d | 1226 | transport_print_push_status(transport->url, remote_refs, |
6ffd7812 | 1227 | verbose | porcelain, porcelain, |
10643d4e | 1228 | reject_reasons); |
64fcef2d | 1229 | |
e9fcd1e2 IL |
1230 | if (flags & TRANSPORT_PUSH_SET_UPSTREAM) |
1231 | set_upstreams(transport, remote_refs, pretend); | |
1232 | ||
225e8bf7 BW |
1233 | if (!(flags & (TRANSPORT_PUSH_DRY_RUN | |
1234 | TRANSPORT_RECURSE_SUBMODULES_ONLY))) { | |
64fcef2d DB |
1235 | struct ref *ref; |
1236 | for (ref = remote_refs; ref; ref = ref->next) | |
f1863d0d | 1237 | transport_update_tracking_ref(transport->remote, ref, verbose); |
64fcef2d DB |
1238 | } |
1239 | ||
77555854 LA |
1240 | if (porcelain && !push_ret) |
1241 | puts("Done"); | |
66bce02e | 1242 | else if (!quiet && !ret && !transport_refs_pushed(remote_refs)) |
64fcef2d | 1243 | fprintf(stderr, "Everything up-to-date\n"); |
77555854 | 1244 | |
64fcef2d DB |
1245 | return ret; |
1246 | } | |
1247 | return 1; | |
9b288516 DB |
1248 | } |
1249 | ||
1af8ae1c BW |
1250 | const struct ref *transport_get_remote_refs(struct transport *transport, |
1251 | const struct argv_array *ref_prefixes) | |
c29727d5 | 1252 | { |
b0d66e15 | 1253 | if (!transport->got_remote_refs) { |
1af8ae1c BW |
1254 | transport->remote_refs = |
1255 | transport->vtable->get_refs_list(transport, 0, | |
1256 | ref_prefixes); | |
b0d66e15 TRC |
1257 | transport->got_remote_refs = 1; |
1258 | } | |
61b075bd | 1259 | |
c29727d5 DB |
1260 | return transport->remote_refs; |
1261 | } | |
1262 | ||
37148311 | 1263 | int transport_fetch_refs(struct transport *transport, struct ref *refs) |
c29727d5 | 1264 | { |
425b1393 | 1265 | int rc; |
86386829 | 1266 | int nr_heads = 0, nr_alloc = 0, nr_refs = 0; |
37148311 DB |
1267 | struct ref **heads = NULL; |
1268 | struct ref *rm; | |
c29727d5 DB |
1269 | |
1270 | for (rm = refs; rm; rm = rm->next) { | |
86386829 | 1271 | nr_refs++; |
c29727d5 | 1272 | if (rm->peer_ref && |
f4e54d02 | 1273 | !is_null_oid(&rm->old_oid) && |
1274 | !oidcmp(&rm->peer_ref->old_oid, &rm->old_oid)) | |
c29727d5 | 1275 | continue; |
7a2bff45 | 1276 | ALLOC_GROW(heads, nr_heads + 1, nr_alloc); |
425b1393 | 1277 | heads[nr_heads++] = rm; |
c29727d5 DB |
1278 | } |
1279 | ||
86386829 NP |
1280 | if (!nr_heads) { |
1281 | /* | |
1282 | * When deepening of a shallow repository is requested, | |
1283 | * then local and remote refs are likely to still be equal. | |
1284 | * Just feed them all to the fetch method in that case. | |
1285 | * This condition shouldn't be met in a non-deepening fetch | |
09b7e220 | 1286 | * (see builtin/fetch.c:quickfetch()). |
86386829 | 1287 | */ |
b32fa95f | 1288 | ALLOC_ARRAY(heads, nr_refs); |
86386829 NP |
1289 | for (rm = refs; rm; rm = rm->next) |
1290 | heads[nr_heads++] = rm; | |
1291 | } | |
1292 | ||
e967ca38 | 1293 | rc = transport->vtable->fetch(transport, nr_heads, heads); |
61b075bd | 1294 | |
c29727d5 | 1295 | free(heads); |
425b1393 | 1296 | return rc; |
c29727d5 DB |
1297 | } |
1298 | ||
1788c39c SP |
1299 | void transport_unlock_pack(struct transport *transport) |
1300 | { | |
1301 | if (transport->pack_lockfile) { | |
691f1a28 | 1302 | unlink_or_warn(transport->pack_lockfile); |
6a83d902 | 1303 | FREE_AND_NULL(transport->pack_lockfile); |
1788c39c SP |
1304 | } |
1305 | } | |
1306 | ||
b236752a IL |
1307 | int transport_connect(struct transport *transport, const char *name, |
1308 | const char *exec, int fd[2]) | |
1309 | { | |
e967ca38 JT |
1310 | if (transport->vtable->connect) |
1311 | return transport->vtable->connect(transport, name, exec, fd); | |
b236752a IL |
1312 | else |
1313 | die("Operation not supported by protocol"); | |
1314 | } | |
1315 | ||
9b288516 DB |
1316 | int transport_disconnect(struct transport *transport) |
1317 | { | |
1318 | int ret = 0; | |
e967ca38 JT |
1319 | if (transport->vtable->disconnect) |
1320 | ret = transport->vtable->disconnect(transport); | |
9b288516 DB |
1321 | free(transport); |
1322 | return ret; | |
1323 | } | |
47abd85b AE |
1324 | |
1325 | /* | |
a7793a74 | 1326 | * Strip username (and password) from a URL and return |
47abd85b AE |
1327 | * it in a newly allocated string. |
1328 | */ | |
1329 | char *transport_anonymize_url(const char *url) | |
1330 | { | |
21f9d0f6 | 1331 | char *scheme_prefix, *anon_part; |
47abd85b AE |
1332 | size_t anon_len, prefix_len = 0; |
1333 | ||
1334 | anon_part = strchr(url, '@'); | |
c59ab2e5 | 1335 | if (url_is_local_not_ssh(url) || !anon_part) |
47abd85b AE |
1336 | goto literal_copy; |
1337 | ||
1338 | anon_len = strlen(++anon_part); | |
1339 | scheme_prefix = strstr(url, "://"); | |
1340 | if (!scheme_prefix) { | |
1341 | if (!strchr(anon_part, ':')) | |
1342 | /* cannot be "me@there:/path/name" */ | |
1343 | goto literal_copy; | |
1344 | } else { | |
1345 | const char *cp; | |
1346 | /* make sure scheme is reasonable */ | |
1347 | for (cp = url; cp < scheme_prefix; cp++) { | |
1348 | switch (*cp) { | |
1349 | /* RFC 1738 2.1 */ | |
1350 | case '+': case '.': case '-': | |
1351 | break; /* ok */ | |
1352 | default: | |
1353 | if (isalnum(*cp)) | |
1354 | break; | |
1355 | /* it isn't */ | |
1356 | goto literal_copy; | |
1357 | } | |
1358 | } | |
1359 | /* @ past the first slash does not count */ | |
1360 | cp = strchr(scheme_prefix + 3, '/'); | |
1361 | if (cp && cp < anon_part) | |
1362 | goto literal_copy; | |
1363 | prefix_len = scheme_prefix - url + 3; | |
1364 | } | |
21f9d0f6 JK |
1365 | return xstrfmt("%.*s%.*s", (int)prefix_len, url, |
1366 | (int)anon_len, anon_part); | |
47abd85b AE |
1367 | literal_copy: |
1368 | return xstrdup(url); | |
1369 | } | |
36cfda15 | 1370 | |
a10a1787 JK |
1371 | static void read_alternate_refs(const char *path, |
1372 | alternate_ref_fn *cb, | |
1373 | void *data) | |
1374 | { | |
1375 | struct child_process cmd = CHILD_PROCESS_INIT; | |
1376 | struct strbuf line = STRBUF_INIT; | |
1377 | FILE *fh; | |
1378 | ||
1379 | cmd.git_cmd = 1; | |
1380 | argv_array_pushf(&cmd.args, "--git-dir=%s", path); | |
1381 | argv_array_push(&cmd.args, "for-each-ref"); | |
1382 | argv_array_push(&cmd.args, "--format=%(objectname) %(refname)"); | |
1383 | cmd.env = local_repo_env; | |
1384 | cmd.out = -1; | |
1385 | ||
1386 | if (start_command(&cmd)) | |
1387 | return; | |
1388 | ||
1389 | fh = xfdopen(cmd.out, "r"); | |
1390 | while (strbuf_getline_lf(&line, fh) != EOF) { | |
1391 | struct object_id oid; | |
1392 | ||
1393 | if (get_oid_hex(line.buf, &oid) || | |
1394 | line.buf[GIT_SHA1_HEXSZ] != ' ') { | |
1395 | warning("invalid line while parsing alternate refs: %s", | |
1396 | line.buf); | |
1397 | break; | |
1398 | } | |
1399 | ||
1400 | cb(line.buf + GIT_SHA1_HEXSZ + 1, &oid, data); | |
1401 | } | |
1402 | ||
1403 | fclose(fh); | |
1404 | finish_command(&cmd); | |
1405 | } | |
1406 | ||
114a6a88 JK |
1407 | struct alternate_refs_data { |
1408 | alternate_ref_fn *fn; | |
1409 | void *data; | |
1410 | }; | |
1411 | ||
1412 | static int refs_from_alternate_cb(struct alternate_object_database *e, | |
1413 | void *data) | |
36cfda15 | 1414 | { |
5e8c968c JK |
1415 | struct strbuf path = STRBUF_INIT; |
1416 | size_t base_len; | |
114a6a88 | 1417 | struct alternate_refs_data *cb = data; |
36cfda15 | 1418 | |
5e8c968c JK |
1419 | if (!strbuf_realpath(&path, e->path, 0)) |
1420 | goto out; | |
1421 | if (!strbuf_strip_suffix(&path, "/objects")) | |
def06971 | 1422 | goto out; |
5e8c968c JK |
1423 | base_len = path.len; |
1424 | ||
36cfda15 | 1425 | /* Is this a git repository with refs? */ |
5e8c968c JK |
1426 | strbuf_addstr(&path, "/refs"); |
1427 | if (!is_directory(path.buf)) | |
def06971 | 1428 | goto out; |
5e8c968c JK |
1429 | strbuf_setlen(&path, base_len); |
1430 | ||
a10a1787 JK |
1431 | read_alternate_refs(path.buf, cb->fn, cb->data); |
1432 | ||
def06971 | 1433 | out: |
5e8c968c | 1434 | strbuf_release(&path); |
36cfda15 JH |
1435 | return 0; |
1436 | } | |
114a6a88 JK |
1437 | |
1438 | void for_each_alternate_ref(alternate_ref_fn fn, void *data) | |
1439 | { | |
1440 | struct alternate_refs_data cb; | |
1441 | cb.fn = fn; | |
1442 | cb.data = data; | |
1443 | foreach_alt_odb(refs_from_alternate_cb, &cb); | |
1444 | } |