Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support for ssh+git and git+ssh protocols #3555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions deps/http-parser/http_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ do { \
FOR##_mark = NULL; \
} \
} while (0)

/* Run the data callback FOR and consume the current byte */
#define CALLBACK_DATA(FOR) \
CALLBACK_DATA_(FOR, p - FOR##_mark, p - data + 1)
Expand Down Expand Up @@ -444,14 +444,17 @@ parse_url_char(enum state s, const char ch)
return s_req_path;
}

/* The schema must start with an alpha character. After that, it may
* consist of digits, '+', '-' or '.', followed by a ':'.
*/
if (IS_ALPHA(ch)) {
return s_req_schema;
}

break;

case s_req_schema:
if (IS_ALPHA(ch)) {
if (IS_ALPHANUM(ch) || ch == '+' || ch == '-' || ch == '.') {
return s;
}

Expand Down
2 changes: 2 additions & 0 deletions src/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ static transport_definition transports[] = {
{ "file://", git_transport_local, NULL },
#ifdef GIT_SSH
{ "ssh://", git_transport_smart, &ssh_subtransport_definition },
{ "ssh+git://", git_transport_smart, &ssh_subtransport_definition },
{ "git+ssh://", git_transport_smart, &ssh_subtransport_definition },
#endif
{ NULL, 0, 0 }
};
Expand Down
49 changes: 32 additions & 17 deletions src/transports/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@

#define OWNING_SUBTRANSPORT(s) ((ssh_subtransport *)(s)->parent.subtransport)

static const char prefix_ssh[] = "ssh://";
static const char *ssh_prefixes[] = { "ssh://", "ssh+git://", "git+ssh://" };

static const char cmd_uploadpack[] = "git-upload-pack";
static const char cmd_receivepack[] = "git-receive-pack";

Expand Down Expand Up @@ -62,17 +63,24 @@ static int gen_proto(git_buf *request, const char *cmd, const char *url)
{
char *repo;
int len;
size_t i;

if (!git__prefixcmp(url, prefix_ssh)) {
url = url + strlen(prefix_ssh);
repo = strchr(url, '/');
if (repo && repo[1] == '~')
++repo;
} else {
repo = strchr(url, ':');
if (repo) repo++;
for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
const char *p = ssh_prefixes[i];

if (!git__prefixcmp(url, p)) {
url = url + strlen(p);
repo = strchr(url, '/');
if (repo && repo[1] == '~')
++repo;

goto done;
}
}
repo = strchr(url, ':');
if (repo) repo++;

done:
if (!repo) {
giterr_set(GITERR_NET, "Malformed git protocol URL");
return -1;
Expand Down Expand Up @@ -494,6 +502,7 @@ static int _git_ssh_setup_conn(
char *host=NULL, *port=NULL, *path=NULL, *user=NULL, *pass=NULL;
const char *default_port="22";
int auth_methods, error = 0;
size_t i;
ssh_stream *s;
git_cred *cred = NULL;
LIBSSH2_SESSION* session=NULL;
Expand All @@ -509,16 +518,22 @@ static int _git_ssh_setup_conn(
s->session = NULL;
s->channel = NULL;

if (!git__prefixcmp(url, prefix_ssh)) {
if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0)
goto done;
} else {
if ((error = git_ssh_extract_url_parts(&host, &user, url)) < 0)
goto done;
port = git__strdup(default_port);
GITERR_CHECK_ALLOC(port);
for (i = 0; i < ARRAY_SIZE(ssh_prefixes); ++i) {
const char *p = ssh_prefixes[i];

if (!git__prefixcmp(url, p)) {
if ((error = gitno_extract_url_parts(&host, &port, &path, &user, &pass, url, default_port)) < 0)
goto done;

goto post_extract;
}
}
if ((error = git_ssh_extract_url_parts(&host, &user, url)) < 0)
goto done;
port = git__strdup(default_port);
GITERR_CHECK_ALLOC(port);

post_extract:
if ((error = git_socket_stream_new(&s->io, host, port)) < 0 ||
(error = git_stream_connect(s->io)) < 0)
goto done;
Expand Down
10 changes: 10 additions & 0 deletions tests/transport/register.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ void test_transport_register__custom_transport_ssh(void)

#ifndef GIT_SSH
cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1);
cl_git_fail_with(git_transport_new(&transport, NULL, "ssh+git://somehost:somepath"), -1);
cl_git_fail_with(git_transport_new(&transport, NULL, "git+ssh://somehost:somepath"), -1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you're only testing that ssh+git fails when not compiling with GIT_SSH. But I would expect that with or without this patch.

Can you test that you can successfully build a transport with git+ssh and with ssh+git below in the else block?

cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1);
#else
cl_git_pass(git_transport_new(&transport, NULL, "ssh://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "ssh+git://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "git+ssh://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath"));
transport->free(transport);
#endif
Expand All @@ -60,8 +65,13 @@ void test_transport_register__custom_transport_ssh(void)

#ifndef GIT_SSH
cl_git_fail_with(git_transport_new(&transport, NULL, "ssh://somehost:somepath"), -1);
cl_git_fail_with(git_transport_new(&transport, NULL, "ssh+git://somehost:somepath"), -1);
cl_git_fail_with(git_transport_new(&transport, NULL, "git+ssh://somehost:somepath"), -1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, I think you want to test git+ssh and ssh+git succeeding, below.

cl_git_fail_with(git_transport_new(&transport, NULL, "git@somehost:somepath"), -1);
#else
cl_git_pass(git_transport_new(&transport, NULL, "ssh://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "ssh+git://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "git+ssh://somehost:somepath"));
cl_git_pass(git_transport_new(&transport, NULL, "git@somehost:somepath"));
transport->free(transport);
#endif
Expand Down