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 die("I don't handle protocol '%s'", name
);
153 #define STR(s) STR_(s)
157 static const char *ai_name(const struct addrinfo
*ai
)
159 static char addr
[INET_ADDRSTRLEN
];
160 if ( AF_INET
== ai
->ai_family
) {
161 struct sockaddr_in
*in
;
162 in
= (struct sockaddr_in
*)ai
->ai_addr
;
163 inet_ntop(ai
->ai_family
, &in
->sin_addr
, addr
, sizeof(addr
));
164 } else if ( AF_INET6
== ai
->ai_family
) {
165 struct sockaddr_in6
*in
;
166 in
= (struct sockaddr_in6
*)ai
->ai_addr
;
167 inet_ntop(ai
->ai_family
, &in
->sin6_addr
, addr
, sizeof(addr
));
169 strcpy(addr
, "(unknown)");
175 * Returns a connected socket() fd, or else die()s.
177 static int git_tcp_connect_sock(char *host
, int flags
)
179 int sockfd
= -1, saved_errno
= 0;
181 const char *port
= STR(DEFAULT_GIT_PORT
);
182 struct addrinfo hints
, *ai0
, *ai
;
186 if (host
[0] == '[') {
187 end
= strchr(host
+ 1, ']');
196 colon
= strchr(end
, ':');
205 memset(&hints
, 0, sizeof(hints
));
206 hints
.ai_socktype
= SOCK_STREAM
;
207 hints
.ai_protocol
= IPPROTO_TCP
;
209 if (flags
& CONNECT_VERBOSE
)
210 fprintf(stderr
, "Looking up %s ... ", host
);
212 gai
= getaddrinfo(host
, port
, &hints
, &ai
);
214 die("Unable to look up %s (port %s) (%s)", host
, port
, gai_strerror(gai
));
216 if (flags
& CONNECT_VERBOSE
)
217 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
219 for (ai0
= ai
; ai
; ai
= ai
->ai_next
) {
220 sockfd
= socket(ai
->ai_family
,
221 ai
->ai_socktype
, ai
->ai_protocol
);
226 if (connect(sockfd
, ai
->ai_addr
, ai
->ai_addrlen
) < 0) {
228 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
232 strerror(saved_errno
));
237 if (flags
& CONNECT_VERBOSE
)
238 fprintf(stderr
, "%s ", ai_name(ai
));
245 die("unable to connect a socket (%s)", strerror(saved_errno
));
247 if (flags
& CONNECT_VERBOSE
)
248 fprintf(stderr
, "done.\n");
256 * Returns a connected socket() fd, or else die()s.
258 static int git_tcp_connect_sock(char *host
, int flags
)
260 int sockfd
= -1, saved_errno
= 0;
262 char *port
= STR(DEFAULT_GIT_PORT
), *ep
;
264 struct sockaddr_in sa
;
269 if (host
[0] == '[') {
270 end
= strchr(host
+ 1, ']');
279 colon
= strchr(end
, ':');
286 if (flags
& CONNECT_VERBOSE
)
287 fprintf(stderr
, "Looking up %s ... ", host
);
289 he
= gethostbyname(host
);
291 die("Unable to look up %s (%s)", host
, hstrerror(h_errno
));
292 nport
= strtoul(port
, &ep
, 10);
293 if ( ep
== port
|| *ep
) {
295 struct servent
*se
= getservbyname(port
,"tcp");
297 die("Unknown port %s\n", port
);
301 if (flags
& CONNECT_VERBOSE
)
302 fprintf(stderr
, "done.\nConnecting to %s (port %s) ... ", host
, port
);
304 for (cnt
= 0, ap
= he
->h_addr_list
; *ap
; ap
++, cnt
++) {
305 sockfd
= socket(he
->h_addrtype
, SOCK_STREAM
, 0);
311 memset(&sa
, 0, sizeof sa
);
312 sa
.sin_family
= he
->h_addrtype
;
313 sa
.sin_port
= htons(nport
);
314 memcpy(&sa
.sin_addr
, *ap
, he
->h_length
);
316 if (connect(sockfd
, (struct sockaddr
*)&sa
, sizeof sa
) < 0) {
318 fprintf(stderr
, "%s[%d: %s]: errno=%s\n",
321 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
),
322 strerror(saved_errno
));
327 if (flags
& CONNECT_VERBOSE
)
328 fprintf(stderr
, "%s ",
329 inet_ntoa(*(struct in_addr
*)&sa
.sin_addr
));
334 die("unable to connect a socket (%s)", strerror(saved_errno
));
336 if (flags
& CONNECT_VERBOSE
)
337 fprintf(stderr
, "done.\n");
345 static void git_tcp_connect(int fd
[2], char *host
, int flags
)
348 int sockfd
= git_tcp_connect_sock(host
, flags
);
353 if (WSAStartup(MAKEWORD(2,2), &wsa
))
354 die("unable to initialize winsock subsystem, error %d",
356 atexit((void(*)(void)) WSACleanup
);
358 sockfd
= git_tcp_connect_sock(host
, flags
);
359 /* convert into a file descriptor */
360 if ((sockfd
= _open_osfhandle(sockfd
, O_RDWR
|O_BINARY
)) < 0)
361 die("unable to make a socket file descriptor: %s",
370 static char *git_proxy_command
;
371 static const char *rhost_name
;
372 static int rhost_len
;
374 static int git_proxy_command_options(const char *var
, const char *value
)
376 if (!strcmp(var
, "core.gitproxy")) {
381 if (git_proxy_command
)
384 * ;# matches www.kernel.org as well
385 * gitproxy = netcatter-1 for kernel.org
386 * gitproxy = netcatter-2 for sample.xz
387 * gitproxy = netcatter-default
389 for_pos
= strstr(value
, " for ");
391 /* matches everybody */
392 matchlen
= strlen(value
);
394 hostlen
= strlen(for_pos
+ 5);
395 if (rhost_len
< hostlen
)
397 else if (!strncmp(for_pos
+ 5,
398 rhost_name
+ rhost_len
- hostlen
,
400 ((rhost_len
== hostlen
) ||
401 rhost_name
[rhost_len
- hostlen
-1] == '.'))
402 matchlen
= for_pos
- value
;
407 /* core.gitproxy = none for kernel.org */
409 !memcmp(value
, "none", 4))
411 git_proxy_command
= xmalloc(matchlen
+ 1);
412 memcpy(git_proxy_command
, value
, matchlen
);
413 git_proxy_command
[matchlen
] = 0;
418 return git_default_config(var
, value
);
421 static int git_use_proxy(const char *host
)
424 rhost_len
= strlen(host
);
425 git_proxy_command
= getenv("GIT_PROXY_COMMAND");
426 git_config(git_proxy_command_options
);
428 return (git_proxy_command
&& *git_proxy_command
);
431 static void git_proxy_connect(int fd
[2], char *host
)
433 const char *port
= STR(DEFAULT_GIT_PORT
);
436 struct child_process proxy
;
438 if (host
[0] == '[') {
439 end
= strchr(host
+ 1, ']');
448 colon
= strchr(end
, ':');
455 argv
[0] = git_proxy_command
;
459 memset(&proxy
, 0, sizeof(proxy
));
463 if (start_command(&proxy
))
464 die("cannot start proxy %s", argv
[0]);
465 fd
[0] = proxy
.out
; /* read from proxy stdout */
466 fd
[1] = proxy
.in
; /* write to proxy stdin */
469 #define MAX_CMD_LEN 1024
472 * This returns 0 if the transport protocol does not need fork(2),
473 * or a process id if it does. Once done, finish the connection
474 * with finish_connect() with the value returned from this function
475 * (it is safe to call finish_connect() with 0 to support the former
478 * Does not return a negative value on error; it just dies.
480 pid_t
git_connect(int fd
[2], char *url
, const char *prog
, int flags
)
482 char *host
, *path
= url
;
487 enum protocol protocol
= PROTO_LOCAL
;
490 /* Without this we cannot rely on waitpid() to tell
491 * what happened to our children.
494 signal(SIGCHLD
, SIG_DFL
);
497 host
= strstr(url
, "://");
500 protocol
= get_protocol(url
);
508 if (host
[0] == '[') {
509 end
= strchr(host
+ 1, ']');
519 path
= strchr(end
, c
);
521 /* host must have at least 2 chars to catch DOS C:/path */
522 if (path
&& path
- end
> 1) {
523 protocol
= PROTO_SSH
;
530 die("No path specified. See 'man git-pull' for valid url syntax");
533 * null-terminate hostname and point path to ~ for URL's like this:
534 * ssh://host.xz/~user/repo
536 if (protocol
!= PROTO_LOCAL
&& host
!= url
) {
548 if (protocol
== PROTO_GIT
) {
549 /* These underlying connection commands die() if they
552 char *target_host
= xstrdup(host
);
553 if (git_use_proxy(host
))
554 git_proxy_connect(fd
, host
);
556 git_tcp_connect(fd
, host
, flags
);
558 * Separate original protocol components prog and path
559 * from extended components with a NUL byte.
571 if (pipe(pipefd
[0]) < 0 || pipe(pipefd
[1]) < 0)
572 die("unable to create pipe pair for communication");
574 char command
[MAX_CMD_LEN
];
575 char *posn
= command
;
576 int size
= MAX_CMD_LEN
;
579 of
|= add_to_string(&posn
, &size
, prog
, 0);
580 of
|= add_to_string(&posn
, &size
, " ", 0);
581 of
|= add_to_string(&posn
, &size
, path
, 1);
584 die("command line too long");
586 if (protocol
== PROTO_SSH
) {
587 const char *argv
[] = { NULL
, host
, command
, NULL
};
588 const char *ssh
= getenv("GIT_SSH");
589 if (!ssh
) ssh
= "ssh";
590 pid
= spawnvpe_pipe(ssh
, argv
, environ
, pipefd
[1], pipefd
[0]);
594 const char *argv
[] = { "box", "sh", "-c", command
, NULL
};
596 const char *argv
[] = { NULL
, "-c", command
, NULL
};
598 const char **env
= copy_environ();
599 env_unsetenv(env
, ALTERNATE_DB_ENVIRONMENT
);
600 env_unsetenv(env
, DB_ENVIRONMENT
);
601 env_unsetenv(env
, GIT_DIR_ENVIRONMENT
);
602 env_unsetenv(env
, GIT_WORK_TREE_ENVIRONMENT
);
603 env_unsetenv(env
, GRAFT_ENVIRONMENT
);
604 env_unsetenv(env
, INDEX_ENVIRONMENT
);
606 pid
= spawnve_git_cmd(argv
, pipefd
[1], pipefd
[0], env
);
608 pid
= spawnvpe_pipe("sh", argv
, env
, pipefd
[1], pipefd
[0]);
612 die("unable to fork");
613 fd
[0] = pipefd
[0][0];
614 fd
[1] = pipefd
[1][1];
620 int finish_connect(pid_t pid
)
625 while (waitpid(pid
, NULL
, 0) < 0) {