2013-04-05 29 views
5

對源代碼控制使用jgit和gitolite,我有一個應用程序可以在命令上生成某些代碼,並且我們希望將它提交給源代碼控制。目標是快速前進,提交新代碼,然後推送它。在jgit中配置known_hosts

我有以下方法:

private void commitToGitRepository(String updateComment, Config config) 
     throws IOException, NoFilepatternException, GitAPIException 
{ 
    if(git == null) 
    { 
     git = Git.open(new File(config.getDpuCheckoutDir())); 
    } 
    PullCommand pull = git.pull(); 
    pull.call(); 
} 

這種方法失敗的pull.call()方法調用,但下列情況除外:

com.jcraft.jsch.JSchException: UnknownHostKey: www.somehost.com. RSA key fingerprint is 9d:92:a9:c5:5d:cb:5f:dc:57:ff:38:7e:34:31:fe:75 
at com.jcraft.jsch.Session.checkHost(Session.java:748) 
at com.jcraft.jsch.Session.connect(Session.java:319) 
at org.eclipse.jgit.transport.JschConfigSessionFactory.getSession(JschConfigSessionFactory.java:116) 
at org.eclipse.jgit.transport.SshTransport.getSession(SshTransport.java:121) 
at org.eclipse.jgit.transport.TransportGitSsh$SshFetchConnection.<init>(TransportGitSsh.java:248) 
at org.eclipse.jgit.transport.TransportGitSsh.openFetch(TransportGitSsh.java:147) 
at org.eclipse.jgit.transport.FetchProcess.executeImp(FetchProcess.java:136) 
at org.eclipse.jgit.transport.FetchProcess.execute(FetchProcess.java:122) 
at org.eclipse.jgit.transport.Transport.fetch(Transport.java:1104) 
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:128) 
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:245) 
at net.intellidata.dpu.controller.schema.EntityMappingController.commitToGitRepository(EntityMappingController.java:149) 
... (truncated where it meets my code) 

我看這個問題的方法,似乎它沒有找到我的知名主機文件user_home/.git。但是,我一直在尋找一個小時,而且我沒有找到一種方法來配置JGit來告訴JSch在哪裏查找known_hosts文件。

對此提出建議?我知道原點的條目存在於我的known_hosts文件

回答

7

This answer提到:

​​

但是,你正在使用jgit,並jsch (the Java secure shell)直接,讓我們看看:

C:\Users\VonC\prog\git>git clone https://github.com/eclipse/jgit 
Cloning into 'jgit'... 
remote: Counting objects: 37854, done. 
remote: Compressing objects: 100% (7743/7743), done. 
remote: Total 37854 (delta 22009), reused 34367 (delta 18831) 
Receiving objects: 100% (37854/37854), 6.73 MiB | 1.37 MiB/s, done. 
Resolving deltas: 100% (22009/22009), done. 

C:\Users\VonC\prog\git>cd jgit 

C:\Users\VonC\prog\git\jgit>grep -nrHI "setKnownHosts" * 
org.eclipse.jgit/src/org/eclipse/jgit/transport/JschConfigSessionFactory.java:262:        sch.setKnownHosts(in); 

找到了!

這個來自JschConfigSessionFactory.java#knownHosts(),看起來像:

new File(new File(home, ".ssh"), "known_hosts"); 
# with: 
home = fs.userHome(); 

userHome是基於System.getProperty("user.home")

所以確保你的java會話有一個user.home defined,並且你有一個%USERPROFILE%/.ssh/known_hosts文件。

user.home應該由java設置爲%USERPROFILE%,也就是說,如果你在Windows上:在某些情況下,this won't always work)。


現在,如果你%USERPROFILE%/.ssh/known_hosts,那麼,作爲mentioned here

只是SSH客戶端(使用命令行工具ssh),這將增加進入您的~/.ssh/known_hosts file


在這種情況下,StormeHawke提到了the comments

因爲我在Tomcat作爲窗口服務運行此,Jsch(通過擴展JGit)一直在尋找不在我的用戶文件夾中,但在SYSTEM帳戶的主文件夾.ssh文件夾。
在這種情況下,我繼續並將.ssh文件夾複製到SYSTEM主文件夾中,因爲Tomcat只能在我的機器上運行以用於開發和測試目的(可能不是最佳安全策略,但在此情況下風險最小)。

this questionthis one,該目錄爲LocalSystem Account應該是:

C:\Documents and Settings\Default User 
# or Wind7/2008 
C:\Windows\System32\Config\systemprofile 

的OP提到:

根據這一呼籲:

System.out.println(System.getProperty("user.home")); 

默認SYSTEM坎用於Windows7的e目錄(以及任何其他基於NT的Windows系統)僅僅是C:\
(如此不理想,但對於快速修復,它的工作原理)。

+0

先生,您是一位天才。當我突然意識到這一點後,我一直在靠牆敲打我的頭,試圖弄清楚爲什麼它不能識別known_hosts條目,因爲我在Tomcat中將它作爲一個Windows服務運行,Jsch(以及JGit的擴展)是查看不在_my_用戶文件夾中,而是在_SYSTEM_帳戶的.ssh文件夾的主文件夾中。在這種情況下,我繼續並將.ssh文件夾複製到SYSTEM主文件夾中,因爲Tomcat只能在我的機器上運行,用於開發和測試目的(可能不是最好的安全策略,但在這種情況下風險極小)。謝謝! – StormeHawke 2013-04-08 16:12:15

+0

@StormeHawke非常好!我已將您的評論納入答案中,以獲得更多的知名度。您的SYSTEM帳戶的目錄是什麼?我提出了一些建議,但我不確定確切的位置。 – VonC 2013-04-08 17:13:04

+0

根據這個調用: 'System.out.println(System.getProperty(「user.home」));' 對於Windows7(以及任何其他基於NT的Windows系統),默認SYSTEM主目錄僅僅是'C :\\' – StormeHawke 2013-04-08 17:31:09