git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH 1/2] Refactor SSH key loading so we don't duplicate keys

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 15, 2008, 17:35 UTC
Message-ID
<1218821705-2631-1-git-send-email-spearce@spearce.org>

We only want to read each private key once, so we cache the names of the keys we have processed before, adding keys which we have not yet seen. This allows us to alter add keys on the fly and avoid duplication.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../jgit/transport/DefaultSshSessionFactory.java   |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
index b4578d4..0484fc0 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DefaultSshSessionFactory.java
@@ -78,6 +78,8 @@
 	/** IANA assigned port number for SSH. */
 	private static final int SSH_PORT = 22;
 
+	private Set<String> loadedIdentities;
+
 	private JSch userJSch;
 
 	@Override
@@ -106,10 +108,10 @@ public String run() {
 
 	private JSch getUserJSch() throws JSchException {
 		if (userJSch == null) {
-			final JSch sch = new JSch();
-			knownHosts(sch);
-			identities(sch);
-			userJSch = sch;
+			loadedIdentities = new HashSet<String>();
+			userJSch = new JSch();
+			knownHosts(userJSch);
+			identities();
 		}
 		return userJSch;
 	}
@@ -133,7 +135,7 @@ private void knownHosts(final JSch sch) throws JSchException {
 		}
 	}
 
-	private void identities(final JSch sch) throws JSchException {
+	private void identities() throws JSchException {
 		final File home = FS.userHome();
 		if (home == null)
 			return;
@@ -149,10 +151,16 @@ private void identities(final JSch sch) throws JSchException {
 			final File k = new File(sshdir, n.substring(0, n.length() - 4));
 			if (!k.isFile())
 				continue;
-			sch.addIdentity(k.getAbsolutePath());
+			addIdentity(k);
 		}
 	}
 
+	private void addIdentity(final File identityFile) throws JSchException {
+		final String path = identityFile.getAbsolutePath();
+		if (loadedIdentities.add(path))
+			userJSch.addIdentity(path);
+	}
+
 	private static class AWT_UserInfo implements UserInfo,
 			UIKeyboardInteractive {
 		private String passwd;
-- 
1.6.0.rc3.250.g8dd0
Next: Shawn O. Pearce
Message 1 of 4 in “Refactor SSH key loading so we don't duplicate keys”
  1. 1/2 Refactor SSH key loading so we don't duplicate keysShawn O. Pearce, Aug 15, 2008
  2. 2/2 Honor ~/.ssh/config whenever possible during SSH based transportShawn O. Pearce, Aug 15, 2008
  3. Robin RosenbergAug 20, 2008
  4. Shawn O. PearceAug 20, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.