| Jason Riedy | 731043f | 2006-01-25 20:38:36 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 2 | #include "cache.h" |
| Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 3 | #include "pkt-line.h" |
| Junio C Hamano | b10d0ec | 2005-07-08 07:02:52 | [diff] [blame] | 4 | #include "quote.h" |
| Junio C Hamano | 6abf5c0 | 2005-10-16 07:25:26 | [diff] [blame] | 5 | #include "refs.h" |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 6 | #include <sys/wait.h> |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 7 | #include <sys/socket.h> |
| 8 | #include <netinet/in.h> |
| 9 | #include <arpa/inet.h> |
| 10 | #include <netdb.h> |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 11 | |
| Johannes Schindelin | 1f5881b | 2005-10-28 03:56:41 | [diff] [blame] | 12 | static char *server_capabilities = NULL; |
| Johannes Schindelin | 211b5f9 | 2005-10-28 02:48:54 | [diff] [blame] | 13 | |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 14 | /* |
| 15 | * Read all the refs from the other end |
| 16 | */ |
| Junio C Hamano | 1a7141f | 2005-10-14 01:57:40 | [diff] [blame] | 17 | struct ref **get_remote_heads(int in, struct ref **list, |
| 18 | int nr_match, char **match, int ignore_funny) |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 19 | { |
| 20 | *list = NULL; |
| 21 | for (;;) { |
| 22 | struct ref *ref; |
| 23 | unsigned char old_sha1[20]; |
| 24 | static char buffer[1000]; |
| 25 | char *name; |
| Johannes Schindelin | 211b5f9 | 2005-10-28 02:48:54 | [diff] [blame] | 26 | int len, name_len; |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 27 | |
| 28 | len = packet_read_line(in, buffer, sizeof(buffer)); |
| 29 | if (!len) |
| 30 | break; |
| 31 | if (buffer[len-1] == '\n') |
| 32 | buffer[--len] = 0; |
| 33 | |
| 34 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') |
| 35 | die("protocol error: expected sha/ref, got '%s'", buffer); |
| 36 | name = buffer + 41; |
| Junio C Hamano | 1a7141f | 2005-10-14 01:57:40 | [diff] [blame] | 37 | |
| Johannes Schindelin | 211b5f9 | 2005-10-28 02:48:54 | [diff] [blame] | 38 | name_len = strlen(name); |
| 39 | if (len != name_len + 41) { |
| 40 | if (server_capabilities) |
| 41 | free(server_capabilities); |
| 42 | server_capabilities = strdup(name + name_len + 1); |
| 43 | } |
| 44 | |
| Junio C Hamano | cfee10a | 2005-12-26 07:18:37 | [diff] [blame] | 45 | if (ignore_funny && 45 < len && !memcmp(name, "refs/", 5) && |
| 46 | check_ref_format(name + 5)) |
| 47 | continue; |
| 48 | |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 49 | if (nr_match && !path_match(name, nr_match, match)) |
| 50 | continue; |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 51 | ref = xcalloc(1, sizeof(*ref) + len - 40); |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 52 | memcpy(ref->old_sha1, old_sha1, 20); |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 53 | memcpy(ref->name, buffer + 41, len - 40); |
| Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 54 | *list = ref; |
| 55 | list = &ref->next; |
| 56 | } |
| 57 | return list; |
| 58 | } |
| 59 | |
| Johannes Schindelin | 211b5f9 | 2005-10-28 02:48:54 | [diff] [blame] | 60 | int server_supports(const char *feature) |
| 61 | { |
| Johannes Schindelin | 1f5881b | 2005-10-28 03:56:41 | [diff] [blame] | 62 | return server_capabilities && |
| 63 | strstr(server_capabilities, feature) != NULL; |
| Johannes Schindelin | 211b5f9 | 2005-10-28 02:48:54 | [diff] [blame] | 64 | } |
| 65 | |
| Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 66 | int get_ack(int fd, unsigned char *result_sha1) |
| 67 | { |
| 68 | static char line[1000]; |
| 69 | int len = packet_read_line(fd, line, sizeof(line)); |
| 70 | |
| 71 | if (!len) |
| 72 | die("git-fetch-pack: expected ACK/NAK, got EOF"); |
| 73 | if (line[len-1] == '\n') |
| 74 | line[--len] = 0; |
| 75 | if (!strcmp(line, "NAK")) |
| 76 | return 0; |
| 77 | if (!strncmp(line, "ACK ", 3)) { |
| Johannes Schindelin | c4c86f0 | 2005-10-28 02:50:26 | [diff] [blame] | 78 | if (!get_sha1_hex(line+4, result_sha1)) { |
| 79 | if (strstr(line+45, "continue")) |
| 80 | return 2; |
| Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 81 | return 1; |
| Johannes Schindelin | c4c86f0 | 2005-10-28 02:50:26 | [diff] [blame] | 82 | } |
| Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 83 | } |
| 84 | die("git-fetch_pack: expected ACK/NAK, got '%s'", line); |
| 85 | } |
| 86 | |
| Linus Torvalds | 013e7c7 | 2005-07-04 20:24:30 | [diff] [blame] | 87 | int path_match(const char *path, int nr, char **match) |
| 88 | { |
| 89 | int i; |
| 90 | int pathlen = strlen(path); |
| 91 | |
| 92 | for (i = 0; i < nr; i++) { |
| 93 | char *s = match[i]; |
| 94 | int len = strlen(s); |
| 95 | |
| 96 | if (!len || len > pathlen) |
| 97 | continue; |
| 98 | if (memcmp(path + pathlen - len, s, len)) |
| 99 | continue; |
| 100 | if (pathlen > len && path[pathlen - len - 1] != '/') |
| 101 | continue; |
| 102 | *s = 0; |
| 103 | return 1; |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 108 | struct refspec { |
| 109 | char *src; |
| 110 | char *dst; |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 111 | char force; |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 112 | }; |
| 113 | |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 114 | /* |
| 115 | * A:B means fast forward remote B with local A. |
| 116 | * +A:B means overwrite remote B with local A. |
| 117 | * +A is a shorthand for +A:A. |
| 118 | * A is a shorthand for A:A. |
| 119 | */ |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 120 | static struct refspec *parse_ref_spec(int nr_refspec, char **refspec) |
| 121 | { |
| 122 | int i; |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 123 | struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1)); |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 124 | for (i = 0; i < nr_refspec; i++) { |
| 125 | char *sp, *dp, *ep; |
| 126 | sp = refspec[i]; |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 127 | if (*sp == '+') { |
| 128 | rs[i].force = 1; |
| 129 | sp++; |
| 130 | } |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 131 | ep = strchr(sp, ':'); |
| 132 | if (ep) { |
| 133 | dp = ep + 1; |
| 134 | *ep = 0; |
| 135 | } |
| 136 | else |
| 137 | dp = sp; |
| 138 | rs[i].src = sp; |
| 139 | rs[i].dst = dp; |
| 140 | } |
| 141 | rs[nr_refspec].src = rs[nr_refspec].dst = NULL; |
| 142 | return rs; |
| 143 | } |
| 144 | |
| 145 | static int count_refspec_match(const char *pattern, |
| 146 | struct ref *refs, |
| 147 | struct ref **matched_ref) |
| 148 | { |
| 149 | int match; |
| 150 | int patlen = strlen(pattern); |
| 151 | |
| 152 | for (match = 0; refs; refs = refs->next) { |
| 153 | char *name = refs->name; |
| 154 | int namelen = strlen(name); |
| 155 | if (namelen < patlen || |
| 156 | memcmp(name + namelen - patlen, pattern, patlen)) |
| 157 | continue; |
| 158 | if (namelen != patlen && name[namelen - patlen - 1] != '/') |
| 159 | continue; |
| 160 | match++; |
| 161 | *matched_ref = refs; |
| 162 | } |
| 163 | return match; |
| 164 | } |
| 165 | |
| 166 | static void link_dst_tail(struct ref *ref, struct ref ***tail) |
| 167 | { |
| 168 | **tail = ref; |
| 169 | *tail = &ref->next; |
| 170 | **tail = NULL; |
| 171 | } |
| 172 | |
| Junio C Hamano | 15e02b3 | 2005-08-06 17:12:03 | [diff] [blame] | 173 | static struct ref *try_explicit_object_name(const char *name) |
| 174 | { |
| 175 | unsigned char sha1[20]; |
| 176 | struct ref *ref; |
| 177 | int len; |
| 178 | if (get_sha1(name, sha1)) |
| 179 | return NULL; |
| 180 | len = strlen(name) + 1; |
| 181 | ref = xcalloc(1, sizeof(*ref) + len); |
| 182 | memcpy(ref->name, name, len); |
| 183 | memcpy(ref->new_sha1, sha1, 20); |
| 184 | return ref; |
| 185 | } |
| 186 | |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 187 | static int match_explicit_refs(struct ref *src, struct ref *dst, |
| 188 | struct ref ***dst_tail, struct refspec *rs) |
| 189 | { |
| 190 | int i, errs; |
| 191 | for (i = errs = 0; rs[i].src; i++) { |
| 192 | struct ref *matched_src, *matched_dst; |
| 193 | |
| 194 | matched_src = matched_dst = NULL; |
| 195 | switch (count_refspec_match(rs[i].src, src, &matched_src)) { |
| 196 | case 1: |
| 197 | break; |
| 198 | case 0: |
| Junio C Hamano | 15e02b3 | 2005-08-06 17:12:03 | [diff] [blame] | 199 | /* The source could be in the get_sha1() format |
| 200 | * not a reference name. |
| 201 | */ |
| 202 | matched_src = try_explicit_object_name(rs[i].src); |
| 203 | if (matched_src) |
| 204 | break; |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 205 | errs = 1; |
| Timo Sirainen | 4ec99bf | 2005-08-09 15:30:22 | [diff] [blame] | 206 | error("src refspec %s does not match any.", |
| 207 | rs[i].src); |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 208 | break; |
| 209 | default: |
| 210 | errs = 1; |
| 211 | error("src refspec %s matches more than one.", |
| 212 | rs[i].src); |
| 213 | break; |
| 214 | } |
| 215 | switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) { |
| 216 | case 1: |
| 217 | break; |
| 218 | case 0: |
| 219 | if (!memcmp(rs[i].dst, "refs/", 5)) { |
| 220 | int len = strlen(rs[i].dst) + 1; |
| 221 | matched_dst = xcalloc(1, sizeof(*dst) + len); |
| 222 | memcpy(matched_dst->name, rs[i].dst, len); |
| 223 | link_dst_tail(matched_dst, dst_tail); |
| 224 | } |
| 225 | else if (!strcmp(rs[i].src, rs[i].dst) && |
| 226 | matched_src) { |
| 227 | /* pushing "master:master" when |
| 228 | * remote does not have master yet. |
| 229 | */ |
| Junio C Hamano | 4fa1604 | 2005-08-05 23:50:54 | [diff] [blame] | 230 | int len = strlen(matched_src->name) + 1; |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 231 | matched_dst = xcalloc(1, sizeof(*dst) + len); |
| 232 | memcpy(matched_dst->name, matched_src->name, |
| 233 | len); |
| 234 | link_dst_tail(matched_dst, dst_tail); |
| 235 | } |
| 236 | else { |
| 237 | errs = 1; |
| 238 | error("dst refspec %s does not match any " |
| 239 | "existing ref on the remote and does " |
| 240 | "not start with refs/.", rs[i].dst); |
| 241 | } |
| 242 | break; |
| 243 | default: |
| 244 | errs = 1; |
| 245 | error("dst refspec %s matches more than one.", |
| 246 | rs[i].dst); |
| 247 | break; |
| 248 | } |
| 249 | if (errs) |
| 250 | continue; |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 251 | if (matched_dst->peer_ref) { |
| 252 | errs = 1; |
| 253 | error("dst ref %s receives from more than one src.", |
| 254 | matched_dst->name); |
| 255 | } |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 256 | else { |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 257 | matched_dst->peer_ref = matched_src; |
| Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 258 | matched_dst->force = rs[i].force; |
| 259 | } |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 260 | } |
| 261 | return -errs; |
| 262 | } |
| 263 | |
| 264 | static struct ref *find_ref_by_name(struct ref *list, const char *name) |
| 265 | { |
| 266 | for ( ; list; list = list->next) |
| 267 | if (!strcmp(list->name, name)) |
| 268 | return list; |
| 269 | return NULL; |
| 270 | } |
| 271 | |
| 272 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
| 273 | int nr_refspec, char **refspec, int all) |
| 274 | { |
| 275 | struct refspec *rs = parse_ref_spec(nr_refspec, refspec); |
| 276 | |
| 277 | if (nr_refspec) |
| 278 | return match_explicit_refs(src, dst, dst_tail, rs); |
| 279 | |
| 280 | /* pick the remainder */ |
| 281 | for ( ; src; src = src->next) { |
| 282 | struct ref *dst_peer; |
| 283 | if (src->peer_ref) |
| 284 | continue; |
| 285 | dst_peer = find_ref_by_name(dst, src->name); |
| Alecs King | 635d37a | 2005-08-04 03:35:37 | [diff] [blame] | 286 | if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all)) |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 287 | continue; |
| 288 | if (!dst_peer) { |
| Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 289 | /* Create a new one and link it */ |
| 290 | int len = strlen(src->name) + 1; |
| 291 | dst_peer = xcalloc(1, sizeof(*dst_peer) + len); |
| 292 | memcpy(dst_peer->name, src->name, len); |
| 293 | memcpy(dst_peer->new_sha1, src->new_sha1, 20); |
| 294 | link_dst_tail(dst_peer, dst_tail); |
| 295 | } |
| 296 | dst_peer->peer_ref = src; |
| 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 301 | enum protocol { |
| 302 | PROTO_LOCAL = 1, |
| 303 | PROTO_SSH, |
| 304 | PROTO_GIT, |
| 305 | }; |
| 306 | |
| 307 | static enum protocol get_protocol(const char *name) |
| 308 | { |
| 309 | if (!strcmp(name, "ssh")) |
| 310 | return PROTO_SSH; |
| 311 | if (!strcmp(name, "git")) |
| 312 | return PROTO_GIT; |
| Linus Torvalds | c05186c | 2005-10-15 00:14:56 | [diff] [blame] | 313 | if (!strcmp(name, "git+ssh")) |
| 314 | return PROTO_SSH; |
| 315 | if (!strcmp(name, "ssh+git")) |
| 316 | return PROTO_SSH; |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 317 | die("I don't handle protocol '%s'", name); |
| 318 | } |
| 319 | |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 320 | #define STR_(s) # s |
| 321 | #define STR(s) STR_(s) |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 322 | |
| hpa | 49744d6 | 2005-09-28 23:52:21 | [diff] [blame] | 323 | #ifndef NO_IPV6 |
| hpa | 4c505f7 | 2005-09-28 23:37:58 | [diff] [blame] | 324 | |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 325 | static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path) |
| 326 | { |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 327 | int sockfd = -1; |
| 328 | char *colon, *end; |
| 329 | char *port = STR(DEFAULT_GIT_PORT); |
| 330 | struct addrinfo hints, *ai0, *ai; |
| 331 | int gai; |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 332 | |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 333 | if (host[0] == '[') { |
| 334 | end = strchr(host + 1, ']'); |
| 335 | if (end) { |
| 336 | *end = 0; |
| 337 | end++; |
| 338 | host++; |
| 339 | } else |
| 340 | end = host; |
| 341 | } else |
| 342 | end = host; |
| 343 | colon = strchr(end, ':'); |
| 344 | |
| Linus Torvalds | ce6f8e7 | 2005-07-23 18:10:21 | [diff] [blame] | 345 | if (colon) { |
| 346 | *colon = 0; |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 347 | port = colon + 1; |
| Linus Torvalds | ce6f8e7 | 2005-07-23 18:10:21 | [diff] [blame] | 348 | } |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 349 | |
| 350 | memset(&hints, 0, sizeof(hints)); |
| 351 | hints.ai_socktype = SOCK_STREAM; |
| 352 | hints.ai_protocol = IPPROTO_TCP; |
| 353 | |
| 354 | gai = getaddrinfo(host, port, &hints, &ai); |
| 355 | if (gai) |
| 356 | die("Unable to look up %s (%s)", host, gai_strerror(gai)); |
| 357 | |
| 358 | for (ai0 = ai; ai; ai = ai->ai_next) { |
| 359 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 360 | if (sockfd < 0) |
| 361 | continue; |
| 362 | if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 363 | close(sockfd); |
| 364 | sockfd = -1; |
| 365 | continue; |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 366 | } |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 367 | break; |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 368 | } |
| 369 | |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 370 | freeaddrinfo(ai0); |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 371 | |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 372 | if (sockfd < 0) |
| YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 373 | die("unable to connect a socket (%s)", strerror(errno)); |
| 374 | |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 375 | fd[0] = sockfd; |
| 376 | fd[1] = sockfd; |
| 377 | packet_write(sockfd, "%s %s\n", prog, path); |
| 378 | return 0; |
| 379 | } |
| 380 | |
| hpa | 49744d6 | 2005-09-28 23:52:21 | [diff] [blame] | 381 | #else /* NO_IPV6 */ |
| hpa | 4c505f7 | 2005-09-28 23:37:58 | [diff] [blame] | 382 | |
| 383 | static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path) |
| 384 | { |
| 385 | int sockfd = -1; |
| 386 | char *colon, *end; |
| 387 | char *port = STR(DEFAULT_GIT_PORT), *ep; |
| 388 | struct hostent *he; |
| 389 | struct sockaddr_in sa; |
| 390 | char **ap; |
| 391 | unsigned int nport; |
| 392 | |
| 393 | if (host[0] == '[') { |
| 394 | end = strchr(host + 1, ']'); |
| 395 | if (end) { |
| 396 | *end = 0; |
| 397 | end++; |
| 398 | host++; |
| 399 | } else |
| 400 | end = host; |
| 401 | } else |
| 402 | end = host; |
| 403 | colon = strchr(end, ':'); |
| 404 | |
| 405 | if (colon) { |
| 406 | *colon = 0; |
| 407 | port = colon + 1; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | he = gethostbyname(host); |
| 412 | if (!he) |
| 413 | die("Unable to look up %s (%s)", host, hstrerror(h_errno)); |
| 414 | nport = strtoul(port, &ep, 10); |
| 415 | if ( ep == port || *ep ) { |
| 416 | /* Not numeric */ |
| 417 | struct servent *se = getservbyname(port,"tcp"); |
| 418 | if ( !se ) |
| 419 | die("Unknown port %s\n", port); |
| 420 | nport = se->s_port; |
| 421 | } |
| 422 | |
| 423 | for (ap = he->h_addr_list; *ap; ap++) { |
| 424 | sockfd = socket(he->h_addrtype, SOCK_STREAM, 0); |
| 425 | if (sockfd < 0) |
| 426 | continue; |
| 427 | |
| 428 | memset(&sa, 0, sizeof sa); |
| 429 | sa.sin_family = he->h_addrtype; |
| Peter Anvin | 6573faf | 2005-09-29 00:26:44 | [diff] [blame] | 430 | sa.sin_port = htons(nport); |
| Paul Serice | c616421 | 2005-11-22 13:54:23 | [diff] [blame] | 431 | memcpy(&sa.sin_addr, *ap, he->h_length); |
| hpa | 4c505f7 | 2005-09-28 23:37:58 | [diff] [blame] | 432 | |
| 433 | if (connect(sockfd, (struct sockaddr *)&sa, sizeof sa) < 0) { |
| 434 | close(sockfd); |
| 435 | sockfd = -1; |
| 436 | continue; |
| 437 | } |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | if (sockfd < 0) |
| 442 | die("unable to connect a socket (%s)", strerror(errno)); |
| 443 | |
| 444 | fd[0] = sockfd; |
| 445 | fd[1] = sockfd; |
| 446 | packet_write(sockfd, "%s %s\n", prog, path); |
| 447 | return 0; |
| 448 | } |
| 449 | |
| hpa | 49744d6 | 2005-09-28 23:52:21 | [diff] [blame] | 450 | #endif /* NO_IPV6 */ |
| hpa | 4c505f7 | 2005-09-28 23:37:58 | [diff] [blame] | 451 | |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 452 | static char *git_proxy_command = NULL; |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 453 | static const char *rhost_name = NULL; |
| 454 | static int rhost_len; |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 455 | |
| 456 | static int git_proxy_command_options(const char *var, const char *value) |
| 457 | { |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 458 | if (!strcmp(var, "core.gitproxy")) { |
| YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 03:18:23 | [diff] [blame] | 459 | const char *for_pos; |
| 460 | int matchlen = -1; |
| 461 | int hostlen; |
| 462 | |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 463 | if (git_proxy_command) |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 464 | return 0; |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 465 | /* [core] |
| 466 | * ;# matches www.kernel.org as well |
| 467 | * gitproxy = netcatter-1 for kernel.org |
| 468 | * gitproxy = netcatter-2 for sample.xz |
| 469 | * gitproxy = netcatter-default |
| 470 | */ |
| YOSHIFUJI Hideaki / 吉藤英明 | c3df856 | 2005-11-22 03:18:23 | [diff] [blame] | 471 | for_pos = strstr(value, " for "); |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 472 | if (!for_pos) |
| 473 | /* matches everybody */ |
| 474 | matchlen = strlen(value); |
| 475 | else { |
| 476 | hostlen = strlen(for_pos + 5); |
| 477 | if (rhost_len < hostlen) |
| 478 | matchlen = -1; |
| 479 | else if (!strncmp(for_pos + 5, |
| 480 | rhost_name + rhost_len - hostlen, |
| 481 | hostlen) && |
| 482 | ((rhost_len == hostlen) || |
| 483 | rhost_name[rhost_len - hostlen -1] == '.')) |
| 484 | matchlen = for_pos - value; |
| 485 | else |
| 486 | matchlen = -1; |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 487 | } |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 488 | if (0 <= matchlen) { |
| 489 | /* core.gitproxy = none for kernel.org */ |
| 490 | if (matchlen == 4 && |
| 491 | !memcmp(value, "none", 4)) |
| 492 | matchlen = 0; |
| 493 | git_proxy_command = xmalloc(matchlen + 1); |
| 494 | memcpy(git_proxy_command, value, matchlen); |
| 495 | git_proxy_command[matchlen] = 0; |
| 496 | } |
| 497 | return 0; |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return git_default_config(var, value); |
| 501 | } |
| 502 | |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 503 | static int git_use_proxy(const char *host) |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 504 | { |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 505 | rhost_name = host; |
| 506 | rhost_len = strlen(host); |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 507 | git_proxy_command = getenv("GIT_PROXY_COMMAND"); |
| 508 | git_config(git_proxy_command_options); |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 509 | rhost_name = NULL; |
| 510 | return (git_proxy_command && *git_proxy_command); |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path) |
| 514 | { |
| 515 | char *port = STR(DEFAULT_GIT_PORT); |
| 516 | char *colon, *end; |
| 517 | int pipefd[2][2]; |
| 518 | pid_t pid; |
| 519 | |
| 520 | if (host[0] == '[') { |
| 521 | end = strchr(host + 1, ']'); |
| 522 | if (end) { |
| 523 | *end = 0; |
| 524 | end++; |
| 525 | host++; |
| 526 | } else |
| 527 | end = host; |
| 528 | } else |
| 529 | end = host; |
| 530 | colon = strchr(end, ':'); |
| 531 | |
| 532 | if (colon) { |
| 533 | *colon = 0; |
| 534 | port = colon + 1; |
| 535 | } |
| 536 | |
| 537 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
| 538 | die("unable to create pipe pair for communication"); |
| 539 | pid = fork(); |
| 540 | if (!pid) { |
| 541 | dup2(pipefd[1][0], 0); |
| 542 | dup2(pipefd[0][1], 1); |
| 543 | close(pipefd[0][0]); |
| 544 | close(pipefd[0][1]); |
| 545 | close(pipefd[1][0]); |
| 546 | close(pipefd[1][1]); |
| 547 | execlp(git_proxy_command, git_proxy_command, host, port, NULL); |
| 548 | die("exec failed"); |
| 549 | } |
| 550 | fd[0] = pipefd[0][0]; |
| 551 | fd[1] = pipefd[1][1]; |
| 552 | close(pipefd[0][1]); |
| 553 | close(pipefd[1][0]); |
| 554 | packet_write(fd[1], "%s %s\n", prog, path); |
| 555 | return pid; |
| 556 | } |
| 557 | |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 558 | /* |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 559 | * Yeah, yeah, fixme. Need to pass in the heads etc. |
| 560 | */ |
| 561 | int git_connect(int fd[2], char *url, const char *prog) |
| 562 | { |
| 563 | char command[1024]; |
| Andreas Ericsson | faea9cc | 2005-11-17 19:37:14 | [diff] [blame] | 564 | char *host, *path = url; |
| YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 10:23:42 | [diff] [blame] | 565 | char *end; |
| 566 | int c; |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 567 | int pipefd[2][2]; |
| 568 | pid_t pid; |
| Andreas Ericsson | faea9cc | 2005-11-17 19:37:14 | [diff] [blame] | 569 | enum protocol protocol = PROTO_LOCAL; |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 570 | |
| Andreas Ericsson | faea9cc | 2005-11-17 19:37:14 | [diff] [blame] | 571 | host = strstr(url, "://"); |
| 572 | if(host) { |
| 573 | *host = '\0'; |
| 574 | protocol = get_protocol(url); |
| 575 | host += 3; |
| YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 10:23:42 | [diff] [blame] | 576 | c = '/'; |
| 577 | } else { |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 578 | host = url; |
| YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 10:23:42 | [diff] [blame] | 579 | c = ':'; |
| 580 | } |
| 581 | |
| 582 | if (host[0] == '[') { |
| 583 | end = strchr(host + 1, ']'); |
| 584 | if (end) { |
| 585 | *end = 0; |
| 586 | end++; |
| 587 | host++; |
| 588 | } else |
| 589 | end = host; |
| 590 | } else |
| 591 | end = host; |
| 592 | |
| 593 | path = strchr(end, c); |
| 594 | if (c == ':') { |
| 595 | if (path) { |
| Andreas Ericsson | faea9cc | 2005-11-17 19:37:14 | [diff] [blame] | 596 | protocol = PROTO_SSH; |
| YOSHIFUJI Hideaki / 吉藤英明 | 356bece | 2005-12-21 10:23:42 | [diff] [blame] | 597 | *path++ = '\0'; |
| 598 | } else |
| 599 | path = host; |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 600 | } |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 601 | |
| Andreas Ericsson | faea9cc | 2005-11-17 19:37:14 | [diff] [blame] | 602 | if (!path || !*path) |
| 603 | die("No path specified. See 'man git-pull' for valid url syntax"); |
| 604 | |
| 605 | /* |
| 606 | * null-terminate hostname and point path to ~ for URL's like this: |
| 607 | * ssh://host.xz/~user/repo |
| 608 | */ |
| 609 | if (protocol != PROTO_LOCAL && host != url) { |
| 610 | char *ptr = path; |
| 611 | if (path[1] == '~') |
| 612 | path++; |
| 613 | else |
| 614 | path = strdup(ptr); |
| 615 | |
| 616 | *ptr = '\0'; |
| 617 | } |
| 618 | |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 619 | if (protocol == PROTO_GIT) { |
| Junio C Hamano | e814bc4 | 2005-11-19 11:48:56 | [diff] [blame] | 620 | if (git_use_proxy(host)) |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 621 | return git_proxy_connect(fd, prog, host, path); |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 622 | return git_tcp_connect(fd, prog, host, path); |
| Paul Collins | f801477 | 2005-11-04 14:57:16 | [diff] [blame] | 623 | } |
| Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 624 | |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 625 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
| 626 | die("unable to create pipe pair for communication"); |
| 627 | pid = fork(); |
| 628 | if (!pid) { |
| Junio C Hamano | b10d0ec | 2005-07-08 07:02:52 | [diff] [blame] | 629 | snprintf(command, sizeof(command), "%s %s", prog, |
| 630 | sq_quote(path)); |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 631 | dup2(pipefd[1][0], 0); |
| 632 | dup2(pipefd[0][1], 1); |
| 633 | close(pipefd[0][0]); |
| 634 | close(pipefd[0][1]); |
| 635 | close(pipefd[1][0]); |
| 636 | close(pipefd[1][1]); |
| Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 637 | if (protocol == PROTO_SSH) { |
| Jason Riedy | c7c81b3 | 2005-08-23 20:34:07 | [diff] [blame] | 638 | const char *ssh, *ssh_basename; |
| 639 | ssh = getenv("GIT_SSH"); |
| 640 | if (!ssh) ssh = "ssh"; |
| 641 | ssh_basename = strrchr(ssh, '/'); |
| Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 642 | if (!ssh_basename) |
| 643 | ssh_basename = ssh; |
| 644 | else |
| 645 | ssh_basename++; |
| 646 | execlp(ssh, ssh_basename, host, command, NULL); |
| 647 | } |
| Matt Draisey | 016fb48 | 2006-01-19 20:58:03 | [diff] [blame] | 648 | else { |
| 649 | unsetenv(ALTERNATE_DB_ENVIRONMENT); |
| 650 | unsetenv(DB_ENVIRONMENT); |
| 651 | unsetenv(GIT_DIR_ENVIRONMENT); |
| 652 | unsetenv(GRAFT_ENVIRONMENT); |
| 653 | unsetenv(INDEX_ENVIRONMENT); |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 654 | execlp("sh", "sh", "-c", command, NULL); |
| Matt Draisey | 016fb48 | 2006-01-19 20:58:03 | [diff] [blame] | 655 | } |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 656 | die("exec failed"); |
| Matt Draisey | 016fb48 | 2006-01-19 20:58:03 | [diff] [blame] | 657 | } |
| Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 658 | fd[0] = pipefd[0][0]; |
| 659 | fd[1] = pipefd[1][1]; |
| 660 | close(pipefd[0][1]); |
| 661 | close(pipefd[1][0]); |
| 662 | return pid; |
| 663 | } |
| 664 | |
| 665 | int finish_connect(pid_t pid) |
| 666 | { |
| 667 | int ret; |
| 668 | |
| 669 | for (;;) { |
| 670 | ret = waitpid(pid, NULL, 0); |
| 671 | if (!ret) |
| 672 | break; |
| 673 | if (errno != EINTR) |
| 674 | break; |
| 675 | } |
| 676 | return ret; |
| 677 | } |