1 #include "git-compat-util.h"
6 #include "run-command.h"
8 #include "spawn-pipe.h"
10 static char *server_capabilities
;
12 static int check_ref(const char *name
, int len
, unsigned int flags
)
17 if (len
< 5 || memcmp(name
, "refs/", 5))
20 /* Skip the "refs/" part */
24 /* REF_NORMAL means that we don't want the magic fake tag refs */
25 if ((flags
& REF_NORMAL
) && check_ref_format(name
) < 0)
28 /* REF_HEADS means that we want regular branch heads */
29 if ((flags
& REF_HEADS
) && !memcmp(name
, "heads/", 6))
32 /* REF_TAGS means that we want tags */
33 if ((flags
& REF_TAGS
) && !memcmp(name
, "tags/", 5))
36 /* All type bits clear means that we are ok with anything */
37 return !(flags
& ~REF_NORMAL
);
41 * Read all the refs from the other end
43 struct ref
**get_remote_heads(int in
, struct ref
**list
,
44 int nr_match
, char **match
,
50 unsigned char old_sha1
[20];
51 static char buffer
[1000];
55 len
= packet_read_line(in
, buffer
, sizeof(buffer
));
58 if (buffer
[len
-1] == '\n')
61 if (len
< 42 || get_sha1_hex(buffer
, old_sha1
) || buffer
[40] != ' ')
62 die("protocol error: expected sha/ref, got '%s'", buffer
);
65 name_len
= strlen(name
);
66 if (len
!= name_len
+ 41) {
67 if (server_capabilities
)
68 free(server_capabilities
);
69 server_capabilities
= xstrdup(name
+ name_len
+ 1);
72 if (!check_ref(name
, name_len
, flags
))
74 if (nr_match
&& !path_match(name
, nr_match
, match
))
76 ref
= alloc_ref(len
- 40);
77 hashcpy(ref
->old_sha1
, old_sha1
);
78 memcpy(ref
->name
, buffer
+ 41, len
- 40);
85 int server_supports(const char *feature
)
87 return server_capabilities
&&
88 strstr(server_capabilities
, feature
) != NULL
;
91 int get_ack(int fd
, unsigned char *result_sha1
)
93 static char line
[1000];
94 int len
= packet_read_line(fd
, line
, sizeof(line
));
97 die("git-fetch-pack: expected ACK/NAK, got EOF");
98 if (line
[len
-1] == '\n')
100 if (!strcmp(line
, "NAK"))
102 if (!prefixcmp(line
, "ACK ")) {
103 if (!get_sha1_hex(line
+4, result_sha1
)) {
104 if (strstr(line
+45, "continue"))
109 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
112 int path_match(const char *path
, int nr
, char **match
)
115 int pathlen
= strlen(path
);
117 for (i
= 0; i
< nr
; i
++) {
121 if (!len
|| len
> pathlen
)
123 if (memcmp(path
+ pathlen
- len
, s
, len
))
125 if (pathlen
> len
&& path
[pathlen
- len
- 1] != '/')
139 static enum protocol
get_protocol(const char *name
)
141 if (!strcmp(name
, "ssh"))
143 if (!strcmp(name
, "git"))
145 if (!strcmp(name
, "git+ssh"))
147 if (!strcmp(name
, "ssh+git"))
149 if (!strcmp(name
, "file"))
151 die("I don't handle protocol '%s'", name
);
155 #define STR(s) STR_(s)
159 static const char *ai_name(const struct addrinfo
*ai
)
161 static char addr
[INET_ADDRSTRLEN
];
162 if ( AF_INET
== ai
->ai_family
) {
163 struct sockaddr_in
*in
;
164 in
= (struct sockaddr_in
*)ai
->ai_addr
;
165 inet_ntop(ai
->ai_family
, &in
->sin_addr
, addr
, sizeof(addr
));
166 } else if ( AF_INET6
== ai
->ai_family
) {
167 struct sockaddr_in6
*in
;
168 in
= (struct sockaddr_in6
*)ai
->ai_addr
;
169 inet_ntop(ai
->ai_family
, &in
->sin6_addr
, addr
, sizeof(addr
));
171 strcpy(addr
, "(unknown)");
177 * Returns a connected socket() fd, or else die()s.
179 static int git_tcp_connect_sock(char *host
, int flags
)
181 int sockfd
= -1, saved_errno
= 0;
183 const char *port
= STR(DEFAULT_GIT_PORT
);
184 struct addrinfo hints
, *ai0
, *ai
;
188 if (host
[0] == '[') {
189 end
= strchr(host
+ 1, ']');
198 colon
= strchr(end
, ':');
207 memset(&hints
, 0, sizeof(hints
));
208 hints
.ai_socktype
= SOCK_STREAM
;
209 hints
.ai_protocol
= IPPROTO_TCP
;
211 if (flags
& CONNECT_VERBOSE
)
212 fprintf(stderr
, "Looking up %s ... ", host
);
214 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
216 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
218 if (flags
& CONNECT_VERBOSE
)
219 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
221 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
222 sockfd
= socket(ai
->ai_family
,
223 ai
->ai_socktype
, ai
->ai_protocol
);
228 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
230 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
234 strerror(saved_errno
));
239 if (flags
& CONNECT_VERBOSE
)
240 fprintf(stderr
, "%s ", ai_name(ai
));
247 die("unable to connect a socket (%s)", strerror(saved_errno
));
249 if (flags
& CONNECT_VERBOSE
)
250 fprintf(stderr
, "done.\n");
258 * Returns a connected socket() fd, or else die()s.
260 static int git_tcp_connect_sock(char *host
, int flags
)
262 int sockfd
= -1, saved_errno
= 0;
264 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
266 struct sockaddr_in sa
;
271 if (host
[0] == '[') {
272 end
= strchr(host
+ 1, ']');
281 colon
= strchr(end
, ':');
288 if (flags
& CONNECT_VERBOSE
)
289 fprintf(stderr
, "Looking up %s ... ", host
);
291 he
= gethostbyname(host
);
293 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
294 nport
= strtoul(port
, &ep
, 10);
295 if ( ep
== port
|| *ep
) {
297 struct servent
*se
= getservbyname(port
,"tcp");
299 die("Unknown port %s\n", port
);
303 if (flags
& CONNECT_VERBOSE
)
304 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
306 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
307 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
313 memset(&sa
, 0, sizeof sa
);
314 sa
.sin_family
= he
->h_addrtype
;
315 sa
.sin_port
= htons(nport
);
316 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
318 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
320 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
323 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
324 strerror(saved_errno
));
329 if (flags
& CONNECT_VERBOSE
)
330 fprintf(stderr
, "%s ",
331 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
336 die("unable to connect a socket (%s)", strerror(saved_errno
));
338 if (flags
& CONNECT_VERBOSE
)
339 fprintf(stderr
, "done.\n");
347 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
350 int sockfd
= git_tcp_connect_sock(host
, flags
);
355 if (WSAStartup(MAKEWORD(2,2), &wsa
))
356 die("unable to initialize winsock subsystem, error %d",
358 atexit((void(*)(void)) WSACleanup
);
360 sockfd
= git_tcp_connect_sock(host
, flags
);
361 /* convert into a file descriptor */
362 if ((sockfd
= _open_osfhandle(sockfd
, O_RDWR
|O_BINARY
)) < 0)
363 die("unable to make a socket file descriptor: %s",
372 static char *git_proxy_command
;
373 static const char *rhost_name
;
374 static int rhost_len
;
376 static int git_proxy_command_options(const char *var
, const char *value
)
378 if (!strcmp(var
, "core.gitproxy")) {
383 if (git_proxy_command
)
386 * ;# matches www.kernel.org as well
387 * gitproxy = netcatter-1 for kernel.org
388 * gitproxy = netcatter-2 for sample.xz
389 * gitproxy = netcatter-default
391 for_pos
= strstr(value
, " for ");
393 /* matches everybody */
394 matchlen
= strlen(value
);
396 hostlen
= strlen(for_pos
+ 5);
397 if (rhost_len
< hostlen
)
399 else if (!strncmp(for_pos
+ 5,
400 rhost_name
+ rhost_len
- hostlen
,
402 ((rhost_len
== hostlen
) ||
403 rhost_name
[rhost_len
- hostlen
-1] == '.'))
404 matchlen
= for_pos
- value
;
409 /* core.gitproxy = none for kernel.org */
411 !memcmp(value
, "none", 4))
413 git_proxy_command
= xmalloc(matchlen
+ 1);
414 memcpy(git_proxy_command
, value
, matchlen
);
415 git_proxy_command
[matchlen
] = 0;
420 return git_default_config(var
, value
);
423 static int git_use_proxy(const char *host
)
426 rhost_len
= strlen(host
);
427 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
428 git_config(git_proxy_command_options
);
430 return (git_proxy_command
&& *git_proxy_command
);
433 static void git_proxy_connect(int fd
[2], char *host
)
435 const char *port
= STR(DEFAULT_GIT_PORT
);
438 struct child_process proxy
;
440 if (host
[0] == '[') {
441 end
= strchr(host
+ 1, ']');
450 colon
= strchr(end
, ':');
457 argv
[0] = git_proxy_command
;
461 memset(&proxy
, 0, sizeof(proxy
));
465 if (start_command(&proxy
))
466 die("cannot start proxy %s", argv
[0]);
467 fd
[0] = proxy
.out
; /* read from proxy stdout */
468 fd
[1] = proxy
.in
; /* write to proxy stdin */
471 #define MAX_CMD_LEN 1024
473 char *get_port(char *host
)
476 char *p
= strchr(host
, ':');
479 strtol(p
+1, &end
, 10);
490 * This returns 0 if the transport protocol does not need fork(2),
491 * or a process id if it does. Once done, finish the connection
492 * with finish_connect() with the value returned from this function
493 * (it is safe to call finish_connect() with 0 to support the former
496 * Does not return a negative value on error; it just dies.
498 pid_t
git_connect(int fd
[2], char *url
, const char *prog
, int flags
)
500 char *host
, *path
= url
;
505 enum protocol protocol
= PROTO_LOCAL
;
509 /* Without this we cannot rely on waitpid() to tell
510 * what happened to our children.
512 signal(SIGCHLD
, SIG_DFL
);
514 host
= strstr(url
, "://");
517 protocol
= get_protocol(url
);
525 if (host
[0] == '[') {
526 end
= strchr(host
+ 1, ']');
536 path
= strchr(end
, c
);
537 /* host must have at least 2 chars to catch DOS C:/path */
538 if (path
&& path
- end
> 1) {
540 protocol
= PROTO_SSH
;
547 die("No path specified. See 'man git-pull' for valid url syntax");
550 * null-terminate hostname and point path to ~ for URL's like this:
551 * ssh://host.xz/~user/repo
553 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
566 * Add support for ssh port: ssh://host.xy:<port>/...
568 if (protocol
== PROTO_SSH
&& host
!= url
)
569 port
= get_port(host
);
571 if (protocol
== PROTO_GIT
) {
572 /* These underlying connection commands die() if they
575 char *target_host
= xstrdup(host
);
576 if (git_use_proxy(host
))
577 git_proxy_connect(fd
, host
);
579 git_tcp_connect(fd
, host
, flags
);
581 * Separate original protocol components prog and path
582 * from extended components with a NUL byte.
594 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
595 die("unable to create pipe pair for communication");
597 char command
[MAX_CMD_LEN
];
598 char *posn
= command
;
599 int size
= MAX_CMD_LEN
;
602 of
|= add_to_string(&posn
, &size
, prog
, 0);
603 of
|= add_to_string(&posn
, &size
, " ", 0);
604 of
|= add_to_string(&posn
, &size
, path
, 1);
607 die("command line too long");
609 if (protocol
== PROTO_SSH
) {
610 const char *arg
[] = { NULL
, NULL
, NULL
, host
, command
, NULL
};
612 const char *ssh
= getenv("GIT_SSH");
613 if (!ssh
) ssh
= "ssh";
621 pid
= spawnvpe_pipe(ssh
, argv
, environ
, pipefd
[1], pipefd
[0]);
624 const char *argv
[] = { NULL
, "-c", command
, NULL
};
625 const char **env
= copy_environ();
626 env_unsetenv(env
, ALTERNATE_DB_ENVIRONMENT
);
627 env_unsetenv(env
, DB_ENVIRONMENT
);
628 env_unsetenv(env
, GIT_DIR_ENVIRONMENT
);
629 env_unsetenv(env
, GIT_WORK_TREE_ENVIRONMENT
);
630 env_unsetenv(env
, GRAFT_ENVIRONMENT
);
631 env_unsetenv(env
, INDEX_ENVIRONMENT
);
632 pid
= spawnvpe_pipe("sh", argv
, env
, pipefd
[1], pipefd
[0]);
635 die("unable to fork");
636 fd
[0] = pipefd
[0][0];
637 fd
[1] = pipefd
[1][1];
643 int finish_connect(pid_t pid
)
648 while (waitpid(pid
, NULL
, 0) < 0) {