5 #include <sys/socket.h>
6 #include <netinet/in.h>
11 * Read all the refs from the other end
13 struct ref
**get_remote_heads(int in
, struct ref
**list
,
14 int nr_match
, char **match
, int ignore_funny
)
19 unsigned char old_sha1
[20];
20 static char buffer
[1000];
24 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
27 if (buffer
[len
-1] == '\n')
30 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
31 die("protocol error: expected sha/ref, got '%s'", buffer
);
34 if (ignore_funny
&& 45 < len
&& !memcmp(name
, "refs/", 5) &&
35 check_ref_format(name
+ 5))
38 if (nr_match
&& !path_match(name
, nr_match
, match
))
40 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
41 memcpy(ref
->old_sha1
, old_sha1
, 20);
42 memcpy(ref
->name
, buffer
+ 41, len
- 40);
49 int get_ack(int fd
, unsigned char *result_sha1
)
51 static char line
[1000];
52 int len
= packet_read_line(fd
, line
, sizeof(line
));
55 die("git-fetch-pack: expected ACK/NAK, got EOF");
56 if (line
[len
-1] == '\n')
58 if (!strcmp(line
, "NAK"))
60 if (!strncmp(line
, "ACK ", 3)) {
61 if (!get_sha1_hex(line
+4, result_sha1
))
64 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
67 int path_match(const char *path
, int nr
, char **match
)
70 int pathlen
= strlen(path
);
72 for (i
= 0; i
< nr
; i
++) {
76 if (!len
|| len
> pathlen
)
78 if (memcmp(path
+ pathlen
- len
, s
, len
))
80 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
95 * A:B means fast forward remote B with local A.
96 * +A:B means overwrite remote B with local A.
97 * +A is a shorthand for +A:A.
98 * A is a shorthand for A:A.
100 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
103 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
104 for (i
= 0; i
< nr_refspec
; i
++) {
111 ep
= strchr(sp
, ':');
121 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
125 static int count_refspec_match(const char *pattern
,
127 struct ref
**matched_ref
)
130 int patlen
= strlen(pattern
);
132 for (match
= 0; refs
; refs
= refs
->next
) {
133 char *name
= refs
->name
;
134 int namelen
= strlen(name
);
135 if (namelen
< patlen
||
136 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
138 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
146 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
153 static struct ref
*try_explicit_object_name(const char *name
)
155 unsigned char sha1
[20];
158 if (get_sha1(name
, sha1
))
160 len
= strlen(name
) + 1;
161 ref
= xcalloc(1, sizeof(*ref
) + len
);
162 memcpy(ref
->name
, name
, len
);
163 memcpy(ref
->new_sha1
, sha1
, 20);
167 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
168 struct ref
***dst_tail
, struct refspec
*rs
)
171 for (i
= errs
= 0; rs
[i
].src
; i
++) {
172 struct ref
*matched_src
, *matched_dst
;
174 matched_src
= matched_dst
= NULL
;
175 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
179 /* The source could be in the get_sha1() format
180 * not a reference name.
182 matched_src
= try_explicit_object_name(rs
[i
].src
);
186 error("src refspec %s does not match any.",
191 error("src refspec %s matches more than one.",
195 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
199 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
200 int len
= strlen(rs
[i
].dst
) + 1;
201 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
202 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
203 link_dst_tail(matched_dst
, dst_tail
);
205 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
207 /* pushing "master:master" when
208 * remote does not have master yet.
210 int len
= strlen(matched_src
->name
) + 1;
211 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
212 memcpy(matched_dst
->name
, matched_src
->name
,
214 link_dst_tail(matched_dst
, dst_tail
);
218 error("dst refspec %s does not match any "
219 "existing ref on the remote and does "
220 "not start with refs/.", rs
[i
].dst
);
225 error("dst refspec %s matches more than one.",
231 if (matched_dst
->peer_ref
) {
233 error("dst ref %s receives from more than one src.",
237 matched_dst
->peer_ref
= matched_src
;
238 matched_dst
->force
= rs
[i
].force
;
244 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
246 for ( ; list
; list
= list
->next
)
247 if (!strcmp(list
->name
, name
))
252 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
253 int nr_refspec
, char **refspec
, int all
)
255 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
258 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
260 /* pick the remainder */
261 for ( ; src
; src
= src
->next
) {
262 struct ref
*dst_peer
;
265 dst_peer
= find_ref_by_name(dst
, src
->name
);
266 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
269 /* Create a new one and link it */
270 int len
= strlen(src
->name
) + 1;
271 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
272 memcpy(dst_peer
->name
, src
->name
, len
);
273 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
274 link_dst_tail(dst_peer
, dst_tail
);
276 dst_peer
->peer_ref
= src
;
287 static enum protocol
get_protocol(const char *name
)
289 if (!strcmp(name
, "ssh"))
291 if (!strcmp(name
, "git"))
293 if (!strcmp(name
, "git+ssh"))
295 if (!strcmp(name
, "ssh+git"))
297 die("I don't handle protocol '%s'", name
);
301 #define STR(s) STR_(s)
305 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
309 char *port
= STR(DEFAULT_GIT_PORT
);
310 struct addrinfo hints
, *ai0
, *ai
;
313 if (host
[0] == '[') {
314 end
= strchr(host
+ 1, ']');
323 colon
= strchr(end
, ':');
330 memset(&hints
, 0, sizeof(hints
));
331 hints
.ai_socktype
= SOCK_STREAM
;
332 hints
.ai_protocol
= IPPROTO_TCP
;
334 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
336 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
338 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
339 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
342 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
353 die("unable to connect a socket (%s)", strerror(errno
));
357 packet_write(sockfd
, "%s %s\n", prog
, path
);
363 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
367 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
369 struct sockaddr_in sa
;
373 if (host
[0] == '[') {
374 end
= strchr(host
+ 1, ']');
383 colon
= strchr(end
, ':');
391 he
= gethostbyname(host
);
393 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
394 nport
= strtoul(port
, &ep
, 10);
395 if ( ep
== port
|| *ep
) {
397 struct servent
*se
= getservbyname(port
,"tcp");
399 die("Unknown port %s\n", port
);
403 for (ap
= he
->h_addr_list
; *ap
; ap
++) {
404 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
408 memset(&sa
, 0, sizeof sa
);
409 sa
.sin_family
= he
->h_addrtype
;
410 sa
.sin_port
= htons(nport
);
411 memcpy(&sa
.sin_addr
, ap
, he
->h_length
);
413 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
422 die("unable to connect a socket (%s)", strerror(errno
));
426 packet_write(sockfd
, "%s %s\n", prog
, path
);
433 * Yeah, yeah, fixme. Need to pass in the heads etc.
435 int git_connect(int fd
[2], char *url
, const char *prog
)
442 enum protocol protocol
;
446 colon
= strchr(url
, ':');
447 protocol
= PROTO_LOCAL
;
452 protocol
= PROTO_SSH
;
453 if (!memcmp(path
, "//", 2)) {
454 char *slash
= strchr(path
+ 2, '/');
456 int nr
= slash
- path
- 2;
457 memmove(path
, path
+2, nr
);
459 protocol
= get_protocol(url
);
466 if (protocol
== PROTO_GIT
)
467 return git_tcp_connect(fd
, prog
, host
, path
);
469 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
470 die("unable to create pipe pair for communication");
473 snprintf(command
, sizeof(command
), "%s %s", prog
,
475 dup2(pipefd
[1][0], 0);
476 dup2(pipefd
[0][1], 1);
481 if (protocol
== PROTO_SSH
) {
482 const char *ssh
, *ssh_basename
;
483 ssh
= getenv("GIT_SSH");
484 if (!ssh
) ssh
= "ssh";
485 ssh_basename
= strrchr(ssh
, '/');
490 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
493 execlp("sh", "sh", "-c", command
, NULL
);
496 fd
[0] = pipefd
[0][0];
497 fd
[1] = pipefd
[1][1];
503 int finish_connect(pid_t pid
)
508 ret
= waitpid(pid
, NULL
, 0);