Search code examples
libgit2

Is there a way to specify the ssh port to be used by libgit2


This code block works perfectly well if I use port 22 to connect in ssh (rather usual 22 basic port of ssh)

But for some reason, I need to connect to ssh on port 443. And at this moment the code doesn't work anymore and tries to connect always on port 22.

So I ask if there is a method to tell the libgit2 to use port 443 for ssh connection.

 int creds(git_cred **out, const char *url, const char *username_from_url,
             unsigned int allowed_types, void *payload) {
    std::cout << "Calling creds" << std::endl;

   int result = git_credential_ssh_key_new(out, username_from_url,"path/to/ssh_key_public", "path/to/ssh_key_private", "");
   if (result != 0) {
      std::cout << giterr_last()->message << std::endl;
      throw;
      return result;
    }
    return result;
  }

int main() {
    git_libgit2_init();
    git_repository *repo = nullptr;
    git_remote *local_remote = nullptr;
    std::string repo_path = "/Users/luclambour/Desktop/repo_test_libgit/git-test-clone";
    int error = 0;
    error = git_repository_open(&repo, repo_path.data());
    std::cout<<error<<std::endl;
    if (error != 0) {    std::cout << giterr_last()->message << std::endl;}


    error =git_remote_create_anonymous(&local_remote, repo, "[email protected]:/root/luc_clone_des/clone_simple");
    if (error != 0) {    std::cout << giterr_last()->message << std::endl;}


    git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
    callbacks.credentials=creds;
    error = git_remote_connect(local_remote, GIT_DIRECTION_PUSH, &callbacks, NULL, NULL);
    if (error != 0) {    std::cout << giterr_last()->message << std::endl;}

    git_push_options opts = GIT_PUSH_OPTIONS_INIT;
    opts.callbacks.credentials = creds;
    opts.proxy_opts.type = GIT_PROXY_NONE;

    error = git_remote_push(local_remote,NULL,&opts);
    if (error != 0) {    std::cout << giterr_last()->message << std::endl;}


    git_remote_free(local_remote);
    git_repository_free(repo);

    git_libgit2_shutdown();
    return 0;
}

Solution

  • scp-like syntax doesn't support port number, so you should use ssh:// URL syntax as:

    git_remote_create_anonymous(&local_remote, repo, "ssh://[email protected]:443/root/luc_clone_des/clone_simple");
    

    Details can be found in the "GIT URLS" section of git-clone(1). https://git-scm.com/docs/git-clone#_git_urls