support non-default port numbers with SSH and tweak URI parser port handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
diff --git a/got/got.c b/got/got.c
index 30f18d7..9d94bcc 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1013,7 +1013,8 @@ cmd_clone(int argc, char *argv[])
goto done;
if (verbosity >= 0)
- printf("Connected to %s:%s\n", host, port);
+ printf("Connected to %s%s%s\n", host,
+ port ? ":" : "", port ? port : "");
/* Create a config file git-fetch(1) can understand. */
gitconfig_path = got_repo_get_path_gitconfig(repo);
@@ -1384,7 +1385,8 @@ cmd_fetch(int argc, char *argv[])
goto done;
if (verbosity >= 0)
- printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
+ printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
+ port ? ":" : "", port ? port : "");
fpa.last_scaled_size[0] = '\0';
fpa.last_p_indexed = -1;
diff --git a/lib/fetch.c b/lib/fetch.c
index 8edac2b..b5b03f3 100644
--- a/lib/fetch.c
+++ b/lib/fetch.c
@@ -89,24 +89,29 @@ dial_ssh(int *fetchfd, const char *host, const char *port, const char *path,
const struct got_error *error = NULL;
int pid, pfd[2];
char cmd[64];
- char *argv[9];
+ char *argv[11];
int i = 0;
*fetchfd = -1;
+ if (port == NULL)
+ port = "22";
+
argv[0] = GOT_FETCH_PATH_SSH;
+ argv[1] = "-p";
+ argv[2] = (char *)port;
if (verbosity == -1) {
- argv[1 + i++] = "-q";
+ argv[3 + i++] = "-q";
} else {
/* ssh(1) allows up to 3 "-v" options. */
for (i = 0; i < MIN(3, verbosity); i++)
- argv[1 + i] = "-v";
+ argv[3 + i] = "-v";
}
- argv[1 + i] = "--";
- argv[2 + i] = (char *)host;
- argv[3 + i] = (char *)cmd;
- argv[4 + i] = (char *)path;
- argv[5 + i] = NULL;
+ argv[3 + i] = "--";
+ argv[4 + i] = (char *)host;
+ argv[5 + i] = (char *)cmd;
+ argv[6 + i] = (char *)path;
+ argv[7 + i] = NULL;
if (pipe(pfd) == -1)
return got_error_from_errno("pipe");
@@ -146,6 +151,9 @@ dial_git(int *fetchfd, const char *host, const char *port, const char *path,
*fetchfd = -1;
+ if (port == NULL)
+ port = GOT_DEFAULT_GIT_PORT_STR;
+
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
@@ -241,11 +249,6 @@ got_fetch_parse_uri(char **proto, char **host, char **port,
err = got_error_from_errno("strdup");
goto done;
}
- *port = strdup("22");
- if (*port == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
s = (char *)uri;
q = strchr(s, ':');
if (q == NULL) {
@@ -296,10 +299,6 @@ got_fetch_parse_uri(char **proto, char **host, char **port,
err = got_error_from_errno("strndup");
goto done;
}
- if (asprintf(port, "%u", GOT_DEFAULT_GIT_PORT) == -1) {
- err = got_error_from_errno("asprintf");
- goto done;
- }
}
}
diff --git a/regress/fetch/fetch_test.c b/regress/fetch/fetch_test.c
index 31b97a8..1d4429c 100644
--- a/regress/fetch/fetch_test.c
+++ b/regress/fetch/fetch_test.c
@@ -83,23 +83,23 @@ fetch_parse_uri(void)
NULL, NULL, NULL, NULL, NULL, GOT_ERR_PARSE_URI },
{ "git://127.0.0.1/git/myrepo",
- "git", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+ "git", "127.0.0.1", NULL,
"/git/myrepo", "myrepo", GOT_ERR_OK },
{ "http://127.0.0.1/git/myrepo",
- "http", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+ "http", "127.0.0.1", NULL,
"/git/myrepo", "myrepo", GOT_ERR_OK },
{ "gopher://127.0.0.1/git/myrepo",
- "gopher", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+ "gopher", "127.0.0.1", NULL,
"/git/myrepo", "myrepo", GOT_ERR_OK },
{ "git://127.0.0.1:22/git/myrepo",
"git", "127.0.0.1", "22", "/git/myrepo", "myrepo",
GOT_ERR_OK },
{ "git://127.0.0.1/git/repos/foo/bar/myrepo.git",
- "git", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+ "git", "127.0.0.1", NULL,
"/git/repos/foo/bar/myrepo.git", "myrepo", GOT_ERR_OK },
{ "https://127.0.0.1/git/repos/foo/../bar/myrepo.git",
- "https", "127.0.0.1", GOT_DEFAULT_GIT_PORT_STR,
+ "https", "127.0.0.1", NULL,
"/git/repos/foo/../bar/myrepo.git", "myrepo",
GOT_ERR_OK },
@@ -111,13 +111,13 @@ fetch_parse_uri(void)
GOT_ERR_OK },
{ "127.0.0.1:git/myrepo",
- "ssh", "127.0.0.1", "22", "git/myrepo", "myrepo",
+ "ssh", "127.0.0.1", NULL, "git/myrepo", "myrepo",
GOT_ERR_OK },
{ "127.0.0.1:/git/myrepo",
- "ssh", "127.0.0.1", "22", "/git/myrepo", "myrepo",
+ "ssh", "127.0.0.1", NULL, "/git/myrepo", "myrepo",
GOT_ERR_OK },
{ "127.0.0.1:22/git/myrepo",
- "ssh", "127.0.0.1", "22", "22/git/myrepo", "myrepo",
+ "ssh", "127.0.0.1", NULL, "22/git/myrepo", "myrepo",
GOT_ERR_OK },
};
int i;