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
, int nr_match
, char **match
)
18 unsigned char old_sha1
[20];
19 static char buffer
[1000];
23 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
26 if (buffer
[len
-1] == '\n')
29 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
30 die("protocol error: expected sha/ref, got '%s'", buffer
);
32 if (nr_match
&& !path_match(name
, nr_match
, match
))
34 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
35 memcpy(ref
->old_sha1
, old_sha1
, 20);
36 memcpy(ref
->name
, buffer
+ 41, len
- 40);
43 int get_ack(int fd
, unsigned char *result_sha1
)
45 static char line
[1000];
46 int len
= packet_read_line(fd
, line
, sizeof(line
));
49 die("git-fetch-pack: expected ACK/NAK, got EOF");
50 if (line
[len
-1] == '\n')
52 if (!strcmp(line
, "NAK"))
54 if (!strncmp(line
, "ACK ", 3)) {
55 if (!get_sha1_hex(line
+4, result_sha1
))
58 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
61 int path_match(const char *path
, int nr
, char **match
)
64 int pathlen
= strlen(path
);
66 for (i
= 0; i
< nr
; i
++) {
70 if (!len
|| len
> pathlen
)
72 if (memcmp(path
+ pathlen
- len
, s
, len
))
74 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
87 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
90 struct refspec
*rs
= xmalloc(sizeof(*rs
) * (nr_refspec
+ 1));
91 for (i
= 0; i
< nr_refspec
; i
++) {
104 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
108 static int count_refspec_match(const char *pattern
,
110 struct ref
**matched_ref
)
113 int patlen
= strlen(pattern
);
115 for (match
= 0; refs
; refs
= refs
->next
) {
116 char *name
= refs
->name
;
117 int namelen
= strlen(name
);
118 if (namelen
< patlen
||
119 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
121 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
129 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
136 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
137 struct ref
***dst_tail
, struct refspec
*rs
)
140 for (i
= errs
= 0; rs
[i
].src
; i
++) {
141 struct ref
*matched_src
, *matched_dst
;
143 matched_src
= matched_dst
= NULL
;
144 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
149 error("src refspec %s does not match any.");
153 error("src refspec %s matches more than one.",
157 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
161 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
162 int len
= strlen(rs
[i
].dst
) + 1;
163 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
164 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
165 link_dst_tail(matched_dst
, dst_tail
);
167 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
169 /* pushing "master:master" when
170 * remote does not have master yet.
172 int len
= strlen(matched_src
->name
);
173 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
174 memcpy(matched_dst
->name
, matched_src
->name
,
176 link_dst_tail(matched_dst
, dst_tail
);
180 error("dst refspec %s does not match any "
181 "existing ref on the remote and does "
182 "not start with refs/.", rs
[i
].dst
);
187 error("dst refspec %s matches more than one.",
193 if (matched_src
->peer_ref
) {
195 error("src ref %s is sent to more than one dst.",
199 matched_src
->peer_ref
= matched_dst
;
200 if (matched_dst
->peer_ref
) {
202 error("dst ref %s receives from more than one src.",
206 matched_dst
->peer_ref
= matched_src
;
211 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
213 for ( ; list
; list
= list
->next
)
214 if (!strcmp(list
->name
, name
))
219 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
220 int nr_refspec
, char **refspec
, int all
)
222 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
225 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
227 /* pick the remainder */
228 for ( ; src
; src
= src
->next
) {
229 struct ref
*dst_peer
;
232 dst_peer
= find_ref_by_name(dst
, src
->name
);
233 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
236 /* Create a new one and link it */
237 int len
= strlen(src
->name
) + 1;
238 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
239 memcpy(dst_peer
->name
, src
->name
, len
);
240 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
241 link_dst_tail(dst_peer
, dst_tail
);
243 dst_peer
->peer_ref
= src
;
254 static enum protocol
get_protocol(const char *name
)
256 if (!strcmp(name
, "ssh"))
258 if (!strcmp(name
, "git"))
260 die("I don't handle protocol '%s'", name
);
264 #define STR(s) STR_(s)
266 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
270 char *port
= STR(DEFAULT_GIT_PORT
);
271 struct addrinfo hints
, *ai0
, *ai
;
274 if (host
[0] == '[') {
275 end
= strchr(host
+ 1, ']');
284 colon
= strchr(end
, ':');
291 memset(&hints
, 0, sizeof(hints
));
292 hints
.ai_socktype
= SOCK_STREAM
;
293 hints
.ai_protocol
= IPPROTO_TCP
;
295 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
297 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
299 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
300 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
303 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
314 die("unable to connect a socket (%s)", strerror(errno
));
318 packet_write(sockfd
, "%s %s\n", prog
, path
);
323 * Yeah, yeah, fixme. Need to pass in the heads etc.
325 int git_connect(int fd
[2], char *url
, const char *prog
)
332 enum protocol protocol
;
336 colon
= strchr(url
, ':');
337 protocol
= PROTO_LOCAL
;
342 protocol
= PROTO_SSH
;
343 if (!memcmp(path
, "//", 2)) {
344 char *slash
= strchr(path
+ 2, '/');
346 int nr
= slash
- path
- 2;
347 memmove(path
, path
+2, nr
);
349 protocol
= get_protocol(url
);
356 if (protocol
== PROTO_GIT
)
357 return git_tcp_connect(fd
, prog
, host
, path
);
359 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
360 die("unable to create pipe pair for communication");
363 snprintf(command
, sizeof(command
), "%s %s", prog
,
365 dup2(pipefd
[1][0], 0);
366 dup2(pipefd
[0][1], 1);
371 if (protocol
== PROTO_SSH
)
372 execlp("ssh", "ssh", host
, command
, NULL
);
374 execlp("sh", "sh", "-c", command
, NULL
);
377 fd
[0] = pipefd
[0][0];
378 fd
[1] = pipefd
[1][1];
384 int finish_connect(pid_t pid
)
389 ret
= waitpid(pid
, NULL
, 0);