Commit c93d1ebad684423906e1c399a85cbc8105cb0011

Carlos Martín Nieto 2014-09-04T16:46:14

ssh: store error message immediately after a failed agent call When the call to the agent fails, we must retrieve the error message just after the function call, as other calls may overwrite it. As the agent authentication is the only one which has a teardown and there does not seem to be a way to get the error message from a stored error number, this tries to introduce some small changes to store the error from the agent. Clearing the error at the beginning of the loop lets us know whether the agent has already set the libgit2 error message and we should skip it, or if we should set it.

diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index a1081b3..57c2445 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -287,6 +287,10 @@ static int ssh_agent_auth(LIBSSH2_SESSION *session, git_cred_ssh_key *c) {
 	}
 
 shutdown:
+
+	if (rc != LIBSSH2_ERROR_NONE)
+		ssh_error(session, "error authenticating");
+
 	libssh2_agent_disconnect(agent);
 	libssh2_agent_free(agent);
 
@@ -300,6 +304,7 @@ static int _git_ssh_authenticate_session(
 	int rc;
 
 	do {
+		giterr_clear();
 		switch (cred->credtype) {
 		case GIT_CREDTYPE_USERPASS_PLAINTEXT: {
 			git_cred_userpass_plaintext *c = (git_cred_userpass_plaintext *)cred;
@@ -353,7 +358,8 @@ static int _git_ssh_authenticate_session(
 	} while (LIBSSH2_ERROR_EAGAIN == rc || LIBSSH2_ERROR_TIMEOUT == rc);
 
 	if (rc != LIBSSH2_ERROR_NONE) {
-		ssh_error(session, "Failed to authenticate SSH session");
+		if (!giterr_last())
+			ssh_error(session, "Failed to authenticate SSH session");
 		return -1;
 	}