6 #include <sys/socket.h>
7 #include <netinet/in.h>
12 * Read all the refs from the other end
14 struct ref
**get_remote_heads(int in
, struct ref
**list
,
15 int nr_match
, char **match
, int ignore_funny
)
20 unsigned char old_sha1
[20];
21 static char buffer
[1000];
25 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
28 if (buffer
[len
-1] == '\n')
31 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
32 die("protocol error: expected sha/ref, got '%s'", buffer
);
35 if (ignore_funny
&& 45 < len
&& !memcmp(name
, "refs/", 5) &&
36 check_ref_format(name
+ 5))
39 if (nr_match
&& !path_match(name
, nr_match
, match
))
41 ref
= xcalloc(1, sizeof(*ref
) + len
- 40);
42 memcpy(ref
->old_sha1
, old_sha1
, 20);
43 memcpy(ref
->name
, buffer
+ 41, len
- 40);
50 int get_ack(int fd
, unsigned char *result_sha1
)
52 static char line
[1000];
53 int len
= packet_read_line(fd
, line
, sizeof(line
));
56 die("git-fetch-pack: expected ACK/NAK, got EOF");
57 if (line
[len
-1] == '\n')
59 if (!strcmp(line
, "NAK"))
61 if (!strncmp(line
, "ACK ", 3)) {
62 if (!get_sha1_hex(line
+4, result_sha1
))
65 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
68 int path_match(const char *path
, int nr
, char **match
)
71 int pathlen
= strlen(path
);
73 for (i
= 0; i
< nr
; i
++) {
77 if (!len
|| len
> pathlen
)
79 if (memcmp(path
+ pathlen
- len
, s
, len
))
81 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
96 * A:B means fast forward remote B with local A.
97 * +A:B means overwrite remote B with local A.
98 * +A is a shorthand for +A:A.
99 * A is a shorthand for A:A.
101 static struct refspec
*parse_ref_spec(int nr_refspec
, char **refspec
)
104 struct refspec
*rs
= xcalloc(sizeof(*rs
), (nr_refspec
+ 1));
105 for (i
= 0; i
< nr_refspec
; i
++) {
112 ep
= strchr(sp
, ':');
122 rs
[nr_refspec
].src
= rs
[nr_refspec
].dst
= NULL
;
126 static int count_refspec_match(const char *pattern
,
128 struct ref
**matched_ref
)
131 int patlen
= strlen(pattern
);
133 for (match
= 0; refs
; refs
= refs
->next
) {
134 char *name
= refs
->name
;
135 int namelen
= strlen(name
);
136 if (namelen
< patlen
||
137 memcmp(name
+ namelen
- patlen
, pattern
, patlen
))
139 if (namelen
!= patlen
&& name
[namelen
- patlen
- 1] != '/')
147 static void link_dst_tail(struct ref
*ref
, struct ref
***tail
)
154 static struct ref
*try_explicit_object_name(const char *name
)
156 unsigned char sha1
[20];
159 if (get_sha1(name
, sha1
))
161 len
= strlen(name
) + 1;
162 ref
= xcalloc(1, sizeof(*ref
) + len
);
163 memcpy(ref
->name
, name
, len
);
164 memcpy(ref
->new_sha1
, sha1
, 20);
168 static int match_explicit_refs(struct ref
*src
, struct ref
*dst
,
169 struct ref
***dst_tail
, struct refspec
*rs
)
172 for (i
= errs
= 0; rs
[i
].src
; i
++) {
173 struct ref
*matched_src
, *matched_dst
;
175 matched_src
= matched_dst
= NULL
;
176 switch (count_refspec_match(rs
[i
].src
, src
, &matched_src
)) {
180 /* The source could be in the get_sha1() format
181 * not a reference name.
183 matched_src
= try_explicit_object_name(rs
[i
].src
);
187 error("src refspec %s does not match any.",
192 error("src refspec %s matches more than one.",
196 switch (count_refspec_match(rs
[i
].dst
, dst
, &matched_dst
)) {
200 if (!memcmp(rs
[i
].dst
, "refs/", 5)) {
201 int len
= strlen(rs
[i
].dst
) + 1;
202 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
203 memcpy(matched_dst
->name
, rs
[i
].dst
, len
);
204 link_dst_tail(matched_dst
, dst_tail
);
206 else if (!strcmp(rs
[i
].src
, rs
[i
].dst
) &&
208 /* pushing "master:master" when
209 * remote does not have master yet.
211 int len
= strlen(matched_src
->name
) + 1;
212 matched_dst
= xcalloc(1, sizeof(*dst
) + len
);
213 memcpy(matched_dst
->name
, matched_src
->name
,
215 link_dst_tail(matched_dst
, dst_tail
);
219 error("dst refspec %s does not match any "
220 "existing ref on the remote and does "
221 "not start with refs/.", rs
[i
].dst
);
226 error("dst refspec %s matches more than one.",
232 if (matched_dst
->peer_ref
) {
234 error("dst ref %s receives from more than one src.",
238 matched_dst
->peer_ref
= matched_src
;
239 matched_dst
->force
= rs
[i
].force
;
245 static struct ref
*find_ref_by_name(struct ref
*list
, const char *name
)
247 for ( ; list
; list
= list
->next
)
248 if (!strcmp(list
->name
, name
))
253 int match_refs(struct ref
*src
, struct ref
*dst
, struct ref
***dst_tail
,
254 int nr_refspec
, char **refspec
, int all
)
256 struct refspec
*rs
= parse_ref_spec(nr_refspec
, refspec
);
259 return match_explicit_refs(src
, dst
, dst_tail
, rs
);
261 /* pick the remainder */
262 for ( ; src
; src
= src
->next
) {
263 struct ref
*dst_peer
;
266 dst_peer
= find_ref_by_name(dst
, src
->name
);
267 if ((dst_peer
&& dst_peer
->peer_ref
) || (!dst_peer
&& !all
))
270 /* Create a new one and link it */
271 int len
= strlen(src
->name
) + 1;
272 dst_peer
= xcalloc(1, sizeof(*dst_peer
) + len
);
273 memcpy(dst_peer
->name
, src
->name
, len
);
274 memcpy(dst_peer
->new_sha1
, src
->new_sha1
, 20);
275 link_dst_tail(dst_peer
, dst_tail
);
277 dst_peer
->peer_ref
= src
;
288 static enum protocol
get_protocol(const char *name
)
290 if (!strcmp(name
, "ssh"))
292 if (!strcmp(name
, "git"))
294 die("I don't handle protocol '%s'", name
);
298 #define STR(s) STR_(s)
300 static int git_tcp_connect(int fd
[2], const char *prog
, char *host
, char *path
)
304 char *port
= STR(DEFAULT_GIT_PORT
);
305 struct addrinfo hints
, *ai0
, *ai
;
308 if (host
[0] == '[') {
309 end
= strchr(host
+ 1, ']');
318 colon
= strchr(end
, ':');
325 memset(&hints
, 0, sizeof(hints
));
326 hints
.ai_socktype
= SOCK_STREAM
;
327 hints
.ai_protocol
= IPPROTO_TCP
;
329 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
331 die("Unable to look up %s (%s)", host
, gai_strerror(gai
));
333 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
334 sockfd
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
337 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
348 die("unable to connect a socket (%s)", strerror(errno
));
352 packet_write(sockfd
, "%s %s\n", prog
, path
);
357 * Yeah, yeah, fixme. Need to pass in the heads etc.
359 int git_connect(int fd
[2], char *url
, const char *prog
)
366 enum protocol protocol
;
370 colon
= strchr(url
, ':');
371 protocol
= PROTO_LOCAL
;
376 protocol
= PROTO_SSH
;
377 if (!memcmp(path
, "//", 2)) {
378 char *slash
= strchr(path
+ 2, '/');
380 int nr
= slash
- path
- 2;
381 memmove(path
, path
+2, nr
);
383 protocol
= get_protocol(url
);
390 if (protocol
== PROTO_GIT
)
391 return git_tcp_connect(fd
, prog
, host
, path
);
393 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
394 die("unable to create pipe pair for communication");
397 snprintf(command
, sizeof(command
), "%s %s", prog
,
399 dup2(pipefd
[1][0], 0);
400 dup2(pipefd
[0][1], 1);
405 if (protocol
== PROTO_SSH
) {
406 const char *ssh
, *ssh_basename
;
407 ssh
= getenv("GIT_SSH");
408 if (!ssh
) ssh
= "ssh";
409 ssh_basename
= strrchr(ssh
, '/');
414 execlp(ssh
, ssh_basename
, host
, command
, NULL
);
417 execlp("sh", "sh", "-c", command
, NULL
);
420 fd
[0] = pipefd
[0][0];
421 fd
[1] = pipefd
[1][1];
427 int finish_connect(pid_t pid
)
432 ret
= waitpid(pid
, NULL
, 0);