6 #include <sys/socket.h>
7 #include <netinet/in.h>
11 static char *server_capabilities
= NULL
;
14 * Read all the refs from the other end
16 struct ref
**get_remote_heads(int in
, struct ref
**list
,
17 int nr_match
, char **match
, int ignore_funny
)
22 unsigned char old_sha1
[20];
23 static char buffer
[1000];
27 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
30 if (buffer
[len
-1] == '\n')
33 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
34 die("protocol error: expected sha/ref, got '%s'", buffer
);
37 if (ignore_funny
&& 45 < len
&& !memcmp(name
, "refs/", 5) &&
38 check_ref_format(name
+ 5))
41 name_len
= strlen(name
);
42 if (len
!= name_len
+ 41) {
43 if (server_capabilities
)
44 free(server_capabilities
);
45 server_capabilities
= strdup(name
+ name_len
+ 1);
48 if (nr_match
&& !path_match(name
, nr_match
, match
))
50 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
51 memcpy(ref
->old_sha1
, old_sha1
, 20);
52 memcpy(ref
->name
, buffer
+ 41, len
- 40);
59 int server_supports(const char *feature
)
61 return server_capabilities
&&
62 strstr(server_capabilities
, feature
) != NULL
;
65 int get_ack(int fd
, unsigned char *result_sha1
)
67 static char line
[1000];
68 int len
= packet_read_line(fd
, line
, sizeof(line
));
71 die("git-fetch-pack: expected ACK/NAK, got EOF");
72 if (line
[len
-1] == '\n')
74 if (!strcmp(line
, "NAK"))
76 if (!strncmp(line
, "ACK ", 3)) {
77 if (!get_sha1_hex(line
+4, result_sha1
)) {
78 if (strstr(line
+45, "continue"))
83 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
86 int path_match(const char *path
, int nr
, char **match
)
89 int pathlen
= strlen(path
);
91 for (i
= 0; i
< nr
; i
++) {
95 if (!len
|| len
> pathlen
)
97 if (memcmp(path
+ pathlen
- len
, s
, len
))
99 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
114 * A:B means fast forward remote B with local A.
115 * +A:B means overwrite remote B with local A.
116 * +A is a shorthand for +A:A.
117 * A is a shorthand for A:A.
119 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
122 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
123 for (i
= 0; i
< nr_refspec
; i
++) {
130 ep
= strchr(sp
, ':');
140 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
144 static int count_refspec_match(const char *pattern
,
146 struct ref
**matched_ref
)
149 int patlen
= strlen(pattern
);
151 for (match
= 0; refs
; refs
= refs
->next
) {
152 char *name
= refs
->name
;
153 int namelen
= strlen(name
);
154 if (namelen
< patlen
||
155 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
157 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
165 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
172 static struct ref
*try_explicit_object_name(const char *name
)
174 unsigned char sha1
[20];
177 if (get_sha1(name
, sha1
))
179 len
= strlen(name
) + 1;
180 ref
= xcalloc(1, sizeof(*ref
) + len
);
181 memcpy(ref
->name
, name
, len
);
182 memcpy(ref
->new_sha1
, sha1
, 20);
186 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
187 struct ref
***dst_tail
, struct refspec
*rs
)
190 for (i
= errs
= 0; rs
[i
].src
; i
++) {
191 struct ref
*matched_src
, *matched_dst
;
193 matched_src
= matched_dst
= NULL
;
194 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
198 /* The source could be in the get_sha1() format
199 * not a reference name.
201 matched_src
= try_explicit_object_name(rs
[i
].src
);
205 error("src refspec %s does not match any.",
210 error("src refspec %s matches more than one.",
214 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
218 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
219 int len
= strlen(rs
[i
].dst
) + 1;
220 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
221 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
222 link_dst_tail(matched_dst
, dst_tail
);
224 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
226 /* pushing "master:master" when
227 * remote does not have master yet.
229 int len
= strlen(matched_src
->name
) + 1;
230 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
231 memcpy(matched_dst
->name
, matched_src
->name
,
233 link_dst_tail(matched_dst
, dst_tail
);
237 error("dst refspec %s does not match any "
238 "existing ref on the remote and does "
239 "not start with refs/.", rs
[i
].dst
);
244 error("dst refspec %s matches more than one.",
250 if (matched_dst
->peer_ref
) {
252 error("dst ref %s receives from more than one src.",
256 matched_dst
->peer_ref
= matched_src
;
257 matched_dst
->force
= rs
[i
].force
;
263 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
265 for ( ; list
; list
= list
->next
)
266 if (!strcmp(list
->name
, name
))
271 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
272 int nr_refspec
, char **refspec
, int all
)
274 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
277 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
279 /* pick the remainder */
280 for ( ; src
; src
= src
->next
) {
281 struct ref
*dst_peer
;
284 dst_peer
= find_ref_by_name(dst
, src
->name
);
285 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
288 /* Create a new one and link it */
289 int len
= strlen(src
->name
) + 1;
290 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
291 memcpy(dst_peer
->name
, src
->name
, len
);
292 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
293 link_dst_tail(dst_peer
, dst_tail
);
295 dst_peer
->peer_ref
= src
;
306 static enum protocol
get_protocol(const char *name
)
308 if (!strcmp(name
, "ssh"))
310 if (!strcmp(name
, "git"))
312 if (!strcmp(name
, "git+ssh"))
314 if (!strcmp(name
, "ssh+git"))
316 die("I don't handle protocol '%s'", name
);
320 #define STR(s) STR_(s)
324 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
328 char *port
= STR(DEFAULT_GIT_PORT
);
329 struct addrinfo hints
, *ai0
, *ai
;
332 if (host
[0] == '[') {
333 end
= strchr(host
+ 1, ']');
342 colon
= strchr(end
, ':');
349 memset(&hints
, 0, sizeof(hints
));
350 hints
.ai_socktype
= SOCK_STREAM
;
351 hints
.ai_protocol
= IPPROTO_TCP
;
353 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
355 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
357 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
358 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
361 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
372 die("unable to connect a socket (%s)", strerror(errno
));
376 packet_write(sockfd
, "%s %s\n", prog
, path
);
382 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
386 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
388 struct sockaddr_in sa
;
392 if (host
[0] == '[') {
393 end
= strchr(host
+ 1, ']');
402 colon
= strchr(end
, ':');
410 he
= gethostbyname(host
);
412 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
413 nport
= strtoul(port
, &ep
, 10);
414 if ( ep
== port
|| *ep
) {
416 struct servent
*se
= getservbyname(port
,"tcp");
418 die("Unknown port %s\n", port
);
422 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
423 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
427 memset(&sa
, 0, sizeof sa
);
428 sa
.sin_family
= he
->h_addrtype
;
429 sa
.sin_port
= htons(nport
);
430 memcpy(&sa
.sin_addr
, ap
, he
->h_length
);
432 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
441 die("unable to connect a socket (%s)", strerror(errno
));
445 packet_write(sockfd
, "%s %s\n", prog
, path
);
452 * Yeah, yeah, fixme. Need to pass in the heads etc.
454 int git_connect(int fd
[2], char *url
, const char *prog
)
461 enum protocol protocol
;
465 colon
= strchr(url
, ':');
466 protocol
= PROTO_LOCAL
;
471 protocol
= PROTO_SSH
;
472 if (!memcmp(path
, "//", 2)) {
473 char *slash
= strchr(path
+ 2, '/');
475 int nr
= slash
- path
- 2;
476 memmove(path
, path
+2, nr
);
478 protocol
= get_protocol(url
);
485 if (protocol
== PROTO_GIT
)
486 return git_tcp_connect(fd
, prog
, host
, path
);
488 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
489 die("unable to create pipe pair for communication");
492 snprintf(command
, sizeof(command
), "%s %s", prog
,
494 dup2(pipefd
[1][0], 0);
495 dup2(pipefd
[0][1], 1);
500 if (protocol
== PROTO_SSH
) {
501 const char *ssh
, *ssh_basename
;
502 ssh
= getenv("GIT_SSH");
503 if (!ssh
) ssh
= "ssh";
504 ssh_basename
= strrchr(ssh
, '/');
509 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
512 execlp("sh", "sh", "-c", command
, NULL
);
515 fd
[0] = pipefd
[0][0];
516 fd
[1] = pipefd
[1][1];
522 int finish_connect(pid_t pid
)
527 ret
= waitpid(pid
, NULL
, 0);