6 #include <sys/socket.h>
7 #include <netinet/in.h>
11 static char *server_capabilities
= "";
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 strstr(feature
, server_capabilities
) != NULL
;
64 int get_ack(int fd
, unsigned char *result_sha1
)
66 static char line
[1000];
67 int len
= packet_read_line(fd
, line
, sizeof(line
));
70 die("git-fetch-pack: expected ACK/NAK, got EOF");
71 if (line
[len
-1] == '\n')
73 if (!strcmp(line
, "NAK"))
75 if (!strncmp(line
, "ACK ", 3)) {
76 if (!get_sha1_hex(line
+4, result_sha1
))
79 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
82 int path_match(const char *path
, int nr
, char **match
)
85 int pathlen
= strlen(path
);
87 for (i
= 0; i
< nr
; i
++) {
91 if (!len
|| len
> pathlen
)
93 if (memcmp(path
+ pathlen
- len
, s
, len
))
95 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
110 * A:B means fast forward remote B with local A.
111 * +A:B means overwrite remote B with local A.
112 * +A is a shorthand for +A:A.
113 * A is a shorthand for A:A.
115 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
118 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
119 for (i
= 0; i
< nr_refspec
; i
++) {
126 ep
= strchr(sp
, ':');
136 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
140 static int count_refspec_match(const char *pattern
,
142 struct ref
**matched_ref
)
145 int patlen
= strlen(pattern
);
147 for (match
= 0; refs
; refs
= refs
->next
) {
148 char *name
= refs
->name
;
149 int namelen
= strlen(name
);
150 if (namelen
< patlen
||
151 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
153 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
161 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
168 static struct ref
*try_explicit_object_name(const char *name
)
170 unsigned char sha1
[20];
173 if (get_sha1(name
, sha1
))
175 len
= strlen(name
) + 1;
176 ref
= xcalloc(1, sizeof(*ref
) + len
);
177 memcpy(ref
->name
, name
, len
);
178 memcpy(ref
->new_sha1
, sha1
, 20);
182 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
183 struct ref
***dst_tail
, struct refspec
*rs
)
186 for (i
= errs
= 0; rs
[i
].src
; i
++) {
187 struct ref
*matched_src
, *matched_dst
;
189 matched_src
= matched_dst
= NULL
;
190 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
194 /* The source could be in the get_sha1() format
195 * not a reference name.
197 matched_src
= try_explicit_object_name(rs
[i
].src
);
201 error("src refspec %s does not match any.",
206 error("src refspec %s matches more than one.",
210 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
214 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
215 int len
= strlen(rs
[i
].dst
) + 1;
216 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
217 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
218 link_dst_tail(matched_dst
, dst_tail
);
220 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
222 /* pushing "master:master" when
223 * remote does not have master yet.
225 int len
= strlen(matched_src
->name
) + 1;
226 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
227 memcpy(matched_dst
->name
, matched_src
->name
,
229 link_dst_tail(matched_dst
, dst_tail
);
233 error("dst refspec %s does not match any "
234 "existing ref on the remote and does "
235 "not start with refs/.", rs
[i
].dst
);
240 error("dst refspec %s matches more than one.",
246 if (matched_dst
->peer_ref
) {
248 error("dst ref %s receives from more than one src.",
252 matched_dst
->peer_ref
= matched_src
;
253 matched_dst
->force
= rs
[i
].force
;
259 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
261 for ( ; list
; list
= list
->next
)
262 if (!strcmp(list
->name
, name
))
267 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
268 int nr_refspec
, char **refspec
, int all
)
270 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
273 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
275 /* pick the remainder */
276 for ( ; src
; src
= src
->next
) {
277 struct ref
*dst_peer
;
280 dst_peer
= find_ref_by_name(dst
, src
->name
);
281 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
284 /* Create a new one and link it */
285 int len
= strlen(src
->name
) + 1;
286 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
287 memcpy(dst_peer
->name
, src
->name
, len
);
288 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
289 link_dst_tail(dst_peer
, dst_tail
);
291 dst_peer
->peer_ref
= src
;
302 static enum protocol
get_protocol(const char *name
)
304 if (!strcmp(name
, "ssh"))
306 if (!strcmp(name
, "git"))
308 if (!strcmp(name
, "git+ssh"))
310 if (!strcmp(name
, "ssh+git"))
312 die("I don't handle protocol '%s'", name
);
316 #define STR(s) STR_(s)
320 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
324 char *port
= STR(DEFAULT_GIT_PORT
);
325 struct addrinfo hints
, *ai0
, *ai
;
328 if (host
[0] == '[') {
329 end
= strchr(host
+ 1, ']');
338 colon
= strchr(end
, ':');
345 memset(&hints
, 0, sizeof(hints
));
346 hints
.ai_socktype
= SOCK_STREAM
;
347 hints
.ai_protocol
= IPPROTO_TCP
;
349 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
351 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
353 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
354 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
357 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
368 die("unable to connect a socket (%s)", strerror(errno
));
372 packet_write(sockfd
, "%s %s\n", prog
, path
);
378 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
382 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
384 struct sockaddr_in sa
;
388 if (host
[0] == '[') {
389 end
= strchr(host
+ 1, ']');
398 colon
= strchr(end
, ':');
406 he
= gethostbyname(host
);
408 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
409 nport
= strtoul(port
, &ep
, 10);
410 if ( ep
== port
|| *ep
) {
412 struct servent
*se
= getservbyname(port
,"tcp");
414 die("Unknown port %s\n", port
);
418 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
419 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
423 memset(&sa
, 0, sizeof sa
);
424 sa
.sin_family
= he
->h_addrtype
;
425 sa
.sin_port
= htons(nport
);
426 memcpy(&sa
.sin_addr
, ap
, he
->h_length
);
428 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
437 die("unable to connect a socket (%s)", strerror(errno
));
441 packet_write(sockfd
, "%s %s\n", prog
, path
);
448 * Yeah, yeah, fixme. Need to pass in the heads etc.
450 int git_connect(int fd
[2], char *url
, const char *prog
)
457 enum protocol protocol
;
461 colon
= strchr(url
, ':');
462 protocol
= PROTO_LOCAL
;
467 protocol
= PROTO_SSH
;
468 if (!memcmp(path
, "//", 2)) {
469 char *slash
= strchr(path
+ 2, '/');
471 int nr
= slash
- path
- 2;
472 memmove(path
, path
+2, nr
);
474 protocol
= get_protocol(url
);
481 if (protocol
== PROTO_GIT
)
482 return git_tcp_connect(fd
, prog
, host
, path
);
484 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
485 die("unable to create pipe pair for communication");
488 snprintf(command
, sizeof(command
), "%s %s", prog
,
490 dup2(pipefd
[1][0], 0);
491 dup2(pipefd
[0][1], 1);
496 if (protocol
== PROTO_SSH
) {
497 const char *ssh
, *ssh_basename
;
498 ssh
= getenv("GIT_SSH");
499 if (!ssh
) ssh
= "ssh";
500 ssh_basename
= strrchr(ssh
, '/');
505 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
508 execlp("sh", "sh", "-c", command
, NULL
);
511 fd
[0] = pipefd
[0][0];
512 fd
[1] = pipefd
[1][1];
518 int finish_connect(pid_t pid
)
523 ret
= waitpid(pid
, NULL
, 0);