Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 1 | #include "cache.h" |
Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 2 | #include "pkt-line.h" |
Junio C Hamano | b10d0ec | 2005-07-08 07:02:52 | [diff] [blame] | 3 | #include "quote.h" |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 4 | #include <sys/wait.h> |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 5 | #include <sys/socket.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <arpa/inet.h> |
| 8 | #include <netdb.h> |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 9 | |
Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 10 | /* |
| 11 | * Read all the refs from the other end |
| 12 | */ |
| 13 | struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match) |
| 14 | { |
| 15 | *list = NULL; |
| 16 | for (;;) { |
| 17 | struct ref *ref; |
| 18 | unsigned char old_sha1[20]; |
| 19 | static char buffer[1000]; |
| 20 | char *name; |
| 21 | int len; |
| 22 | |
| 23 | len = packet_read_line(in, buffer, sizeof(buffer)); |
| 24 | if (!len) |
| 25 | break; |
| 26 | if (buffer[len-1] == '\n') |
| 27 | buffer[--len] = 0; |
| 28 | |
| 29 | if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ') |
| 30 | die("protocol error: expected sha/ref, got '%s'", buffer); |
| 31 | name = buffer + 41; |
| 32 | if (nr_match && !path_match(name, nr_match, match)) |
| 33 | continue; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 34 | ref = xcalloc(1, sizeof(*ref) + len - 40); |
Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 35 | memcpy(ref->old_sha1, old_sha1, 20); |
Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 36 | memcpy(ref->name, buffer + 41, len - 40); |
Linus Torvalds | d1c133f | 2005-07-16 20:55:50 | [diff] [blame] | 37 | *list = ref; |
| 38 | list = &ref->next; |
| 39 | } |
| 40 | return list; |
| 41 | } |
| 42 | |
Linus Torvalds | 41cb748 | 2005-07-05 22:44:09 | [diff] [blame] | 43 | int get_ack(int fd, unsigned char *result_sha1) |
| 44 | { |
| 45 | static char line[1000]; |
| 46 | int len = packet_read_line(fd, line, sizeof(line)); |
| 47 | |
| 48 | if (!len) |
| 49 | die("git-fetch-pack: expected ACK/NAK, got EOF"); |
| 50 | if (line[len-1] == '\n') |
| 51 | line[--len] = 0; |
| 52 | if (!strcmp(line, "NAK")) |
| 53 | return 0; |
| 54 | if (!strncmp(line, "ACK ", 3)) { |
| 55 | if (!get_sha1_hex(line+4, result_sha1)) |
| 56 | return 1; |
| 57 | } |
| 58 | die("git-fetch_pack: expected ACK/NAK, got '%s'", line); |
| 59 | } |
| 60 | |
Linus Torvalds | 013e7c7 | 2005-07-04 20:24:30 | [diff] [blame] | 61 | int path_match(const char *path, int nr, char **match) |
| 62 | { |
| 63 | int i; |
| 64 | int pathlen = strlen(path); |
| 65 | |
| 66 | for (i = 0; i < nr; i++) { |
| 67 | char *s = match[i]; |
| 68 | int len = strlen(s); |
| 69 | |
| 70 | if (!len || len > pathlen) |
| 71 | continue; |
| 72 | if (memcmp(path + pathlen - len, s, len)) |
| 73 | continue; |
| 74 | if (pathlen > len && path[pathlen - len - 1] != '/') |
| 75 | continue; |
| 76 | *s = 0; |
| 77 | return 1; |
| 78 | } |
| 79 | return 0; |
| 80 | } |
| 81 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 82 | struct refspec { |
| 83 | char *src; |
| 84 | char *dst; |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 85 | char force; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 86 | }; |
| 87 | |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 88 | /* |
| 89 | * A:B means fast forward remote B with local A. |
| 90 | * +A:B means overwrite remote B with local A. |
| 91 | * +A is a shorthand for +A:A. |
| 92 | * A is a shorthand for A:A. |
| 93 | */ |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 94 | static struct refspec *parse_ref_spec(int nr_refspec, char **refspec) |
| 95 | { |
| 96 | int i; |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 97 | struct refspec *rs = xcalloc(sizeof(*rs), (nr_refspec + 1)); |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 98 | for (i = 0; i < nr_refspec; i++) { |
| 99 | char *sp, *dp, *ep; |
| 100 | sp = refspec[i]; |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 101 | if (*sp == '+') { |
| 102 | rs[i].force = 1; |
| 103 | sp++; |
| 104 | } |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 105 | ep = strchr(sp, ':'); |
| 106 | if (ep) { |
| 107 | dp = ep + 1; |
| 108 | *ep = 0; |
| 109 | } |
| 110 | else |
| 111 | dp = sp; |
| 112 | rs[i].src = sp; |
| 113 | rs[i].dst = dp; |
| 114 | } |
| 115 | rs[nr_refspec].src = rs[nr_refspec].dst = NULL; |
| 116 | return rs; |
| 117 | } |
| 118 | |
| 119 | static int count_refspec_match(const char *pattern, |
| 120 | struct ref *refs, |
| 121 | struct ref **matched_ref) |
| 122 | { |
| 123 | int match; |
| 124 | int patlen = strlen(pattern); |
| 125 | |
| 126 | for (match = 0; refs; refs = refs->next) { |
| 127 | char *name = refs->name; |
| 128 | int namelen = strlen(name); |
| 129 | if (namelen < patlen || |
| 130 | memcmp(name + namelen - patlen, pattern, patlen)) |
| 131 | continue; |
| 132 | if (namelen != patlen && name[namelen - patlen - 1] != '/') |
| 133 | continue; |
| 134 | match++; |
| 135 | *matched_ref = refs; |
| 136 | } |
| 137 | return match; |
| 138 | } |
| 139 | |
| 140 | static void link_dst_tail(struct ref *ref, struct ref ***tail) |
| 141 | { |
| 142 | **tail = ref; |
| 143 | *tail = &ref->next; |
| 144 | **tail = NULL; |
| 145 | } |
| 146 | |
Junio C Hamano | 15e02b3 | 2005-08-06 17:12:03 | [diff] [blame] | 147 | static struct ref *try_explicit_object_name(const char *name) |
| 148 | { |
| 149 | unsigned char sha1[20]; |
| 150 | struct ref *ref; |
| 151 | int len; |
| 152 | if (get_sha1(name, sha1)) |
| 153 | return NULL; |
| 154 | len = strlen(name) + 1; |
| 155 | ref = xcalloc(1, sizeof(*ref) + len); |
| 156 | memcpy(ref->name, name, len); |
| 157 | memcpy(ref->new_sha1, sha1, 20); |
| 158 | return ref; |
| 159 | } |
| 160 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 161 | static int match_explicit_refs(struct ref *src, struct ref *dst, |
| 162 | struct ref ***dst_tail, struct refspec *rs) |
| 163 | { |
| 164 | int i, errs; |
| 165 | for (i = errs = 0; rs[i].src; i++) { |
| 166 | struct ref *matched_src, *matched_dst; |
| 167 | |
| 168 | matched_src = matched_dst = NULL; |
| 169 | switch (count_refspec_match(rs[i].src, src, &matched_src)) { |
| 170 | case 1: |
| 171 | break; |
| 172 | case 0: |
Junio C Hamano | 15e02b3 | 2005-08-06 17:12:03 | [diff] [blame] | 173 | /* The source could be in the get_sha1() format |
| 174 | * not a reference name. |
| 175 | */ |
| 176 | matched_src = try_explicit_object_name(rs[i].src); |
| 177 | if (matched_src) |
| 178 | break; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 179 | errs = 1; |
Timo Sirainen | 4ec99bf | 2005-08-09 15:30:22 | [diff] [blame] | 180 | error("src refspec %s does not match any.", |
| 181 | rs[i].src); |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 182 | break; |
| 183 | default: |
| 184 | errs = 1; |
| 185 | error("src refspec %s matches more than one.", |
| 186 | rs[i].src); |
| 187 | break; |
| 188 | } |
| 189 | switch (count_refspec_match(rs[i].dst, dst, &matched_dst)) { |
| 190 | case 1: |
| 191 | break; |
| 192 | case 0: |
| 193 | if (!memcmp(rs[i].dst, "refs/", 5)) { |
| 194 | int len = strlen(rs[i].dst) + 1; |
| 195 | matched_dst = xcalloc(1, sizeof(*dst) + len); |
| 196 | memcpy(matched_dst->name, rs[i].dst, len); |
| 197 | link_dst_tail(matched_dst, dst_tail); |
| 198 | } |
| 199 | else if (!strcmp(rs[i].src, rs[i].dst) && |
| 200 | matched_src) { |
| 201 | /* pushing "master:master" when |
| 202 | * remote does not have master yet. |
| 203 | */ |
Junio C Hamano | 4fa1604 | 2005-08-05 23:50:54 | [diff] [blame] | 204 | int len = strlen(matched_src->name) + 1; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 205 | matched_dst = xcalloc(1, sizeof(*dst) + len); |
| 206 | memcpy(matched_dst->name, matched_src->name, |
| 207 | len); |
| 208 | link_dst_tail(matched_dst, dst_tail); |
| 209 | } |
| 210 | else { |
| 211 | errs = 1; |
| 212 | error("dst refspec %s does not match any " |
| 213 | "existing ref on the remote and does " |
| 214 | "not start with refs/.", rs[i].dst); |
| 215 | } |
| 216 | break; |
| 217 | default: |
| 218 | errs = 1; |
| 219 | error("dst refspec %s matches more than one.", |
| 220 | rs[i].dst); |
| 221 | break; |
| 222 | } |
| 223 | if (errs) |
| 224 | continue; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 225 | if (matched_dst->peer_ref) { |
| 226 | errs = 1; |
| 227 | error("dst ref %s receives from more than one src.", |
| 228 | matched_dst->name); |
| 229 | } |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 230 | else { |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 231 | matched_dst->peer_ref = matched_src; |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 232 | matched_dst->force = rs[i].force; |
| 233 | } |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 234 | } |
| 235 | return -errs; |
| 236 | } |
| 237 | |
| 238 | static struct ref *find_ref_by_name(struct ref *list, const char *name) |
| 239 | { |
| 240 | for ( ; list; list = list->next) |
| 241 | if (!strcmp(list->name, name)) |
| 242 | return list; |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | int match_refs(struct ref *src, struct ref *dst, struct ref ***dst_tail, |
| 247 | int nr_refspec, char **refspec, int all) |
| 248 | { |
| 249 | struct refspec *rs = parse_ref_spec(nr_refspec, refspec); |
| 250 | |
| 251 | if (nr_refspec) |
| 252 | return match_explicit_refs(src, dst, dst_tail, rs); |
| 253 | |
| 254 | /* pick the remainder */ |
| 255 | for ( ; src; src = src->next) { |
| 256 | struct ref *dst_peer; |
| 257 | if (src->peer_ref) |
| 258 | continue; |
| 259 | dst_peer = find_ref_by_name(dst, src->name); |
Alecs King | 635d37a | 2005-08-04 03:35:37 | [diff] [blame] | 260 | if ((dst_peer && dst_peer->peer_ref) || (!dst_peer && !all)) |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 261 | continue; |
| 262 | if (!dst_peer) { |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 263 | /* Create a new one and link it */ |
| 264 | int len = strlen(src->name) + 1; |
| 265 | dst_peer = xcalloc(1, sizeof(*dst_peer) + len); |
| 266 | memcpy(dst_peer->name, src->name, len); |
| 267 | memcpy(dst_peer->new_sha1, src->new_sha1, 20); |
| 268 | link_dst_tail(dst_peer, dst_tail); |
| 269 | } |
| 270 | dst_peer->peer_ref = src; |
| 271 | } |
| 272 | return 0; |
| 273 | } |
| 274 | |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 275 | enum protocol { |
| 276 | PROTO_LOCAL = 1, |
| 277 | PROTO_SSH, |
| 278 | PROTO_GIT, |
| 279 | }; |
| 280 | |
| 281 | static enum protocol get_protocol(const char *name) |
| 282 | { |
| 283 | if (!strcmp(name, "ssh")) |
| 284 | return PROTO_SSH; |
| 285 | if (!strcmp(name, "git")) |
| 286 | return PROTO_GIT; |
| 287 | die("I don't handle protocol '%s'", name); |
| 288 | } |
| 289 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 290 | #define STR_(s) # s |
| 291 | #define STR(s) STR_(s) |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 292 | |
| 293 | static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path) |
| 294 | { |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 295 | int sockfd = -1; |
| 296 | char *colon, *end; |
| 297 | char *port = STR(DEFAULT_GIT_PORT); |
| 298 | struct addrinfo hints, *ai0, *ai; |
| 299 | int gai; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 300 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 301 | if (host[0] == '[') { |
| 302 | end = strchr(host + 1, ']'); |
| 303 | if (end) { |
| 304 | *end = 0; |
| 305 | end++; |
| 306 | host++; |
| 307 | } else |
| 308 | end = host; |
| 309 | } else |
| 310 | end = host; |
| 311 | colon = strchr(end, ':'); |
| 312 | |
Linus Torvalds | ce6f8e7 | 2005-07-23 18:10:21 | [diff] [blame] | 313 | if (colon) { |
| 314 | *colon = 0; |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 315 | port = colon + 1; |
Linus Torvalds | ce6f8e7 | 2005-07-23 18:10:21 | [diff] [blame] | 316 | } |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 317 | |
| 318 | memset(&hints, 0, sizeof(hints)); |
| 319 | hints.ai_socktype = SOCK_STREAM; |
| 320 | hints.ai_protocol = IPPROTO_TCP; |
| 321 | |
| 322 | gai = getaddrinfo(host, port, &hints, &ai); |
| 323 | if (gai) |
| 324 | die("Unable to look up %s (%s)", host, gai_strerror(gai)); |
| 325 | |
| 326 | for (ai0 = ai; ai; ai = ai->ai_next) { |
| 327 | sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); |
| 328 | if (sockfd < 0) |
| 329 | continue; |
| 330 | if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) { |
| 331 | close(sockfd); |
| 332 | sockfd = -1; |
| 333 | continue; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 334 | } |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 335 | break; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 336 | } |
| 337 | |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 338 | freeaddrinfo(ai0); |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 339 | |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 340 | if (sockfd < 0) |
YOSHIFUJI Hideaki | 5ba8844 | 2005-07-21 13:10:36 | [diff] [blame] | 341 | die("unable to connect a socket (%s)", strerror(errno)); |
| 342 | |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 343 | fd[0] = sockfd; |
| 344 | fd[1] = sockfd; |
| 345 | packet_write(sockfd, "%s %s\n", prog, path); |
| 346 | return 0; |
| 347 | } |
| 348 | |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 349 | /* |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 350 | * Yeah, yeah, fixme. Need to pass in the heads etc. |
| 351 | */ |
| 352 | int git_connect(int fd[2], char *url, const char *prog) |
| 353 | { |
| 354 | char command[1024]; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 355 | char *host, *path; |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 356 | char *colon; |
| 357 | int pipefd[2][2]; |
| 358 | pid_t pid; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 359 | enum protocol protocol; |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 360 | |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 361 | host = NULL; |
| 362 | path = url; |
| 363 | colon = strchr(url, ':'); |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 364 | protocol = PROTO_LOCAL; |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 365 | if (colon) { |
| 366 | *colon = 0; |
| 367 | host = url; |
| 368 | path = colon+1; |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 369 | protocol = PROTO_SSH; |
| 370 | if (!memcmp(path, "//", 2)) { |
| 371 | char *slash = strchr(path + 2, '/'); |
| 372 | if (slash) { |
| 373 | int nr = slash - path - 2; |
| 374 | memmove(path, path+2, nr); |
| 375 | path[nr] = 0; |
| 376 | protocol = get_protocol(url); |
| 377 | host = path; |
| 378 | path = slash; |
| 379 | } |
| 380 | } |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 381 | } |
Linus Torvalds | 2386d65 | 2005-07-14 01:46:20 | [diff] [blame] | 382 | |
| 383 | if (protocol == PROTO_GIT) |
| 384 | return git_tcp_connect(fd, prog, host, path); |
| 385 | |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 386 | if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0) |
| 387 | die("unable to create pipe pair for communication"); |
| 388 | pid = fork(); |
| 389 | if (!pid) { |
Junio C Hamano | b10d0ec | 2005-07-08 07:02:52 | [diff] [blame] | 390 | snprintf(command, sizeof(command), "%s %s", prog, |
| 391 | sq_quote(path)); |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 392 | dup2(pipefd[1][0], 0); |
| 393 | dup2(pipefd[0][1], 1); |
| 394 | close(pipefd[0][0]); |
| 395 | close(pipefd[0][1]); |
| 396 | close(pipefd[1][0]); |
| 397 | close(pipefd[1][1]); |
Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 398 | if (protocol == PROTO_SSH) { |
Jason Riedy | c7c81b3 | 2005-08-23 20:34:07 | [diff] [blame] | 399 | const char *ssh, *ssh_basename; |
| 400 | ssh = getenv("GIT_SSH"); |
| 401 | if (!ssh) ssh = "ssh"; |
| 402 | ssh_basename = strrchr(ssh, '/'); |
Martin Sivak | 4852f72 | 2005-08-03 15:15:42 | [diff] [blame] | 403 | if (!ssh_basename) |
| 404 | ssh_basename = ssh; |
| 405 | else |
| 406 | ssh_basename++; |
| 407 | execlp(ssh, ssh_basename, host, command, NULL); |
| 408 | } |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 409 | else |
| 410 | execlp("sh", "sh", "-c", command, NULL); |
| 411 | die("exec failed"); |
| 412 | } |
| 413 | fd[0] = pipefd[0][0]; |
| 414 | fd[1] = pipefd[1][1]; |
| 415 | close(pipefd[0][1]); |
| 416 | close(pipefd[1][0]); |
| 417 | return pid; |
| 418 | } |
| 419 | |
| 420 | int finish_connect(pid_t pid) |
| 421 | { |
| 422 | int ret; |
| 423 | |
| 424 | for (;;) { |
| 425 | ret = waitpid(pid, NULL, 0); |
| 426 | if (!ret) |
| 427 | break; |
| 428 | if (errno != EINTR) |
| 429 | break; |
| 430 | } |
| 431 | return ret; |
| 432 | } |