@matheusbsbs / matheus-bagliano / commits / f801477645
Fork 0

proxy-command support for git://

Here is an updated patch that first looks for GIT_PROXY_COMMAND in the environment and then git.proxycommand in the repository's configuration file. I have left the calling convention the same argv[1] is the host and argv[2] is the port. I've taken the hostname parsing verbatim from git_tcp_connect(), so it should now support an explicit port number and whatever that business with the square brackets is. (Should I move this to a helper function?) Regarding internal vs. external hosts, the proxy command can simply run netcat locally to internal hosts, so perhaps that is sufficient. Signed-off-by: Junio C Hamano <junkio@cox.net>

Paul Collins committed Nov 4, 2005 at 14:57 UTC f801477645146fd7eff018b4997b86dfa1a0767d
1 file changed +71 -1
connect.c
+71 -1
@@ -448,6 +448,73 @@ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
448
449 #endif /* NO_IPV6 */
450
451 +static char *git_proxy_command = NULL;
452 +
453 +static int git_proxy_command_options(const char *var, const char *value)
454 +{
455 + if (git_proxy_command == NULL) {
456 + if (!strcmp(var, "git.proxycommand")) {
457 + git_proxy_command = xmalloc(strlen(value) + 1);
458 + strcpy(git_proxy_command, value);
459 + return 0;
460 + }
461 + }
462 +
463 + return git_default_config(var, value);
464 +}
465 +
466 +static int git_use_proxy(void)
467 +{
468 + git_proxy_command = getenv("GIT_PROXY_COMMAND");
469 + git_config(git_proxy_command_options);
470 + return git_proxy_command != NULL;
471 +}
472 +
473 +static int git_proxy_connect(int fd[2], const char *prog, char *host, char *path)
474 +{
475 + char *port = STR(DEFAULT_GIT_PORT);
476 + char *colon, *end;
477 + int pipefd[2][2];
478 + pid_t pid;
479 +
480 + if (host[0] == '[') {
481 + end = strchr(host + 1, ']');
482 + if (end) {
483 + *end = 0;
484 + end++;
485 + host++;
486 + } else
487 + end = host;
488 + } else
489 + end = host;
490 + colon = strchr(end, ':');
491 +
492 + if (colon) {
493 + *colon = 0;
494 + port = colon + 1;
495 + }
496 +
497 + if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
498 + die("unable to create pipe pair for communication");
499 + pid = fork();
500 + if (!pid) {
501 + dup2(pipefd[1][0], 0);
502 + dup2(pipefd[0][1], 1);
503 + close(pipefd[0][0]);
504 + close(pipefd[0][1]);
505 + close(pipefd[1][0]);
506 + close(pipefd[1][1]);
507 + execlp(git_proxy_command, git_proxy_command, host, port, NULL);
508 + die("exec failed");
509 + }
510 + fd[0] = pipefd[0][0];
511 + fd[1] = pipefd[1][1];
512 + close(pipefd[0][1]);
513 + close(pipefd[1][0]);
514 + packet_write(fd[1], "%s %s\n", prog, path);
515 + return pid;
516 +}
517 +
518 /*
519 * Yeah, yeah, fixme. Need to pass in the heads etc.
520 */
@@ -493,8 +560,11 @@ int git_connect(int fd[2], char *url, const char *prog)
560 *ptr = '\0';
561 }
562
496 - if (protocol == PROTO_GIT)
563 + if (protocol == PROTO_GIT) {
564 + if (git_use_proxy())
565 + return git_proxy_connect(fd, prog, host, path);
566 return git_tcp_connect(fd, prog, host, path);
567 + }
568
569 if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
570 die("unable to create pipe pair for communication");