The session part connections with the private key, no problem. However when I do a git Clone, it gives the error 'Auth Fail'. How do I wrap, bind or make the connected session work with git clone. I'm using NGIT under .NET 4.0, but don't think this matters as JGIT is pretty much the same.
Any ideas ?
Thanks Gavin
JSch jsch = new JSch();
Session session = jsch.GetSession(gUser, gHost, 22);
jsch.AddIdentity(PrivateKeyFile); // If I leave this line out, the session fails to Auth. therefore it works.
Hashtable table = new Hashtable();
table["StrictHostKeyChecking"] = "no"; // this works
session.SetConfig(table);
session.Connect(); // the session connects.
URIish u = new URIish();
u.SetPort(22);
u.SetHost(gHost);
u.SetUser(gUser);
NGit.Transport.JschSession jschSession = new JschSession(session,u );
if (session.IsConnected())
{
try
{
CloneCommand clone = Git.CloneRepository()
.SetURI(gitAddress)
.SetDirectory(folderToSave);
clone.Call();
// MessageBox.Show(Status, gitAddress, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
// AUth Fail..... ????
}
}
else
{
session.Disconnect();
}
session.Disconnect();
The problem here is the session object is not actually associated with the CloneCommand at any time. Therefore, all the work you've done to set up the session doesn't really do anything, as the CloneCommand is going to create its own session (with the default session items) itself.
The clone commands will get the session it will actually use from the
SSHSessionFactory
. First, you need to create a class that implements theSSHSessionFactory
abstract class, like I've done below:Then you can set all new Git commands to use this factory for when they want to use a session:
Note that I'm still figuring this library out, so I might have not written MySSHSessionFactory in an optimal way (does it handle fault tolerance for sessions that close, for example?). But this at least a start.