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