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