我在 scala 中使用 JGit 访问远程 Git 存储库,并且需要使用 SSH 进行身份验证。JGit 使用 JSch 提供安全访问。
我遵循了本教程:http://www.codeaffine.com/2014/12/09/jgit-authentication/
但是,我总是出现com.jcraft.jsch.JSchException:身份验证失败
我的方法是:
def sshCloneRemoteRepository() = {
// 1 / Override SSH configuration with JschConfigSessionFactory
val sshSessionFactory: SshSessionFactory = new JschConfigSessionFactory() {
override protected def createDefaultJSch(fs: FS): JSch = {
val defaultJSch = super.createDefaultJSch(fs)
defaultJSch.removeAllIdentity()
defaultJSch.addIdentity(new File("/home/xw/.ssh/id_rsa").getAbsolutePath)
defaultJSch.setKnownHosts("/home/xw/.ssh/known_hosts")
val configRepository:ConfigRepository = com.jcraft.jsch.OpenSSHConfig.parseFile("/home/xw/.ssh/config")
defaultJSch.setConfigRepository(configRepository)
defaultJSch
}
override protected def configure(host:OpenSshConfig.Host, session:Session ) {
// This still checks existing host keys and will disable "unsafe" authentication mechanisms
// …Run Code Online (Sandbox Code Playgroud) 我正在使用 jgit (version 4.8.0.201706111038-r) 将git repo 克隆到临时目录中,并添加关闭挂钩以在终止后删除临时目录。但是,关闭挂钩无法从.git子目录中删除某些文件(尽管关闭了 jgit 要求的 Git 对象)。
有趣的是,只有当我使用 Path API ( Files.delete(<PATH>)) 时删除才会失败,但如果我使用旧的file.delete().
这是一个最小的独立示例,其唯一依赖项是 jgit 4.8.0.201706111038-r:
public static void main(String... args) throws Exception {
String gitRepo = "https://github.com/netgloo/spring-boot-samples.git";
Path localDir = Files.createTempDirectory(null);
// Clone repo
Git git = Git.cloneRepository().setURI(gitRepo).setBranch("master").setDirectory(localDir.toFile()).call();
// Print some stuff to make sure that the git repo actually works
for (RevCommit c : git.log().call()) {
System.out.println(c);
}
git.getRepository().close(); // Close all the things!
git.close(); // Close all …Run Code Online (Sandbox Code Playgroud) 使用普通git checkout命令完全按照我的预期工作。以下是我尝试使用同一段代码允许的用例:
1)git checkout branchname其中branchname不存在于本地,但远程确实上
2) git checkout branchname,其中branchname已在本地存在
3) git checkout commitid
对于上下文,存储库之前已按如下方式克隆:
repo = Git.cloneRepository()
.setCloneSubmodules(true)
.setURI(repoUrl)
.setDirectory(createTempDir())
.setCloneAllBranches(true)
.call();
Run Code Online (Sandbox Code Playgroud)
标准的 JGit checkout 命令不会在本地自动创建分支。以下代码适用于场景 2 和 3:
repo.checkout()
.setName(branchOrCommitId)
.call();
Run Code Online (Sandbox Code Playgroud)
通过创建新分支的修正,它仅适用于场景 1:
repo.checkout()
.setCreateBranch(true)
.setName(branchOrCommitId)
.call();
Run Code Online (Sandbox Code Playgroud)
考虑到标准 Git CLI 已经在我正在寻找的命令中提供了自动功能,我可以使用这个问题的巧妙解决方案吗?
我需要使用 JGit 获取与特定提交关联的分支名称。我使用 JGit 获取存储库的提交 SHA 的完整列表,现在我需要知道它所属的分支的名称。
如果有人能让我知道如何实现这一目标,我将不胜感激。
让我们首先显示 --global .gitconfig,就像 git 和 jgit 看到的那样!!!从 DOS 窗口以及从 Cgywin 窗口
E:\> git config --list --global
http.auth.preference=Basic
user.name=Josef Stadelmann
user.email=josef.stadelmann@axa-winterthur.ch
http.proxy=http://C770817:MyPassword@bcproxyserver.ch.winterthur.com:8080
E:\>
Run Code Online (Sandbox Code Playgroud)
现在让我们从带有 JGit 的 Cygwin 窗口中查看
$ jgit config --list --global
http.auth.preference=Basic
user.email=josef.stadelmann@axa-winterthur.ch
user.name=Josef Stadelmann
http.proxy=http://C770817:MyPassword@bcproxyserver.ch.winterthur.com:8080
Run Code Online (Sandbox Code Playgroud)
我们可以看到相同的配置 那为什么
C770817@C036357 ~
$ jgit ls-remote https://github.com/stadelma/HelloWorld.git
fatal: Connection time out: github.com
fatal: https://github.com/stadelma/HelloWorld.git: cannot open git-upload-pack
Run Code Online (Sandbox Code Playgroud)
超时但是
C770817@C036357 ~
$ git ls-remote http://github.com/stadelma/HelloWorld.git
35f375cb64208b64ed499c2a47727dfcd8813dea HEAD
35f375cb64208b64ed499c2a47727dfcd8813dea refs/heads/master
C770817@C036357 ~
$
Run Code Online (Sandbox Code Playgroud)
在读取相同的 .gitconfig 时正常工作
/cygdrive/e/mingw/home/.gitconfig
Run Code Online (Sandbox Code Playgroud)
???
欢迎任何建议 - 或者 - 我应该开始调试 jgit …
我想创建一个Java程序,其中
理想情况下,所有这些都应该发生在内存中.
我正在使用JGit与Git进行交互:
InMemoryRepository repo = new InMemoryRepository(new DfsRepositoryDescription());
Git git = new Git(repo);
git.init().call();
PullCommand pull = git.pull();
StoredConfig config = git.getRepository().getConfig();
config.setString("remote", "origin", "url", "https://XXX");
config.save();
PullResult result = pull.call();
Run Code Online (Sandbox Code Playgroud)
在pull.call()以下异常结果:
org.eclipse.jgit.api.errors.NoHeadException: Pull on repository without HEAD currently not supported
at org.eclipse.jgit.api.PullCommand.call(PullCommand.java:191)
Run Code Online (Sandbox Code Playgroud)
如何将存储库的内容检索到内存中的JGit存储库?
我正在使用JGit创建一个新的git存储库,以及该文件夹中已存在的所有内容,我保存为新分支.
我在用
Git.init().setDirectory(workingDir).call();
Run Code Online (Sandbox Code Playgroud)
(在上面的语句之后创建了默认的master分支,所以我将它重命名为"backupBranch").因为主人我以后从远程主人克隆.
问题是当我按下backupbranch时,它被推动但没有我能够建立的远程跟踪.
来自终端:如果我使用git branch -vv结果是
master 5f1994c [origin/master] commitmsg
backupbranch 7a4d671 taking backup of file already in MyTests Folder..
Run Code Online (Sandbox Code Playgroud)
根据链接使现有的Git分支跟踪一个远程分支?我可以使用
git branch -u upstream/foo终端指定推送backupbranch后的任何时间.
如何使用JGit实现它.
以下是我正在使用的代码
创建backupbranch
git.branchRename().setNewName(backUpName).call();
git.add().addFilepattern(".").call();
RevCommit commit = git.commit().setMessage("taking backup of file already in MyTests Folder..").call();
Run Code Online (Sandbox Code Playgroud)
然后第一次推它.
Iterable<PushResult> pushResult = git.push().call();
Run Code Online (Sandbox Code Playgroud)
请建议我为现有分支指定远程跟踪的方式.
我正在尝试使用 JGit 执行有冲突的项目中的所有合并场景。
是否有可能在实际合并之前获得冲突?换句话说,是否可以模拟与 JGit 的合并?
我的目标是为项目的所有合并场景访问每个文件的冲突行。
我试图从Windows服务器上通过https克隆一个git存储库.此服务器使用单点登录,因此与kerberos5相关.对我的简单代码知之甚少:
CloneCommand clone = Git.cloneRepository();
clone.setURI("https://gerrit.intra.infineon.com/dcgr/sys/sysbox");
clone.setCredentialsProvider(new UsernamePasswordCredentialsProvider("user", "pass"));
clone.setDirectory(new File("C:\\userdata\\temp\\gittest"));
clone.setBranch("master");
clone.call();
Run Code Online (Sandbox Code Playgroud)
导致异常:
org.eclipse.jgit.errors.TransportException: https://gerrit.mycompany.com/sampleRepo: cannot open git-upload-pack
at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:518)
at org.eclipse.jgit.transport.TransportHttp.openFetch(TransportHttp.java:296)
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:1138)
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:130)
... 22 more
Caused by: java.io.IOException
at org.eclipse.jgit.transport.HttpAuthMethod$Negotiate.configureRequest(HttpAuthMethod.java:547)
at org.eclipse.jgit.transport.TransportHttp.httpOpen(TransportHttp.java:561)
at org.eclipse.jgit.transport.TransportHttp.httpOpen(TransportHttp.java:523)
at org.eclipse.jgit.transport.TransportHttp.connect(TransportHttp.java:462)
... 27 more
Caused by: GSSException: No valid credentials provided (Mechanism level: No valid credentials provided (Mechanism level: Attempt to obtain new INITIATE credentials failed! (null)))
at sun.security.jgss.spnego.SpNegoContext.initSecContext(SpNegoContext.java:454)
at sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:248)
at sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:179)
at org.eclipse.jgit.transport.HttpAuthMethod$Negotiate.configureRequest(HttpAuthMethod.java:541)
... …Run Code Online (Sandbox Code Playgroud) 我正在开发一个必须使用 JGit 的项目。当我尝试使用远程 git 存储库(未在本地设备中克隆的 git)时遇到问题,我收到以下异常:
不存在 HEAD 且未指定明确的起始修订版
如果我克隆存储库,它运行良好,但存储库非常大,由于项目要求我无法克隆它。有没有办法使用远程存储库?我只做简单的读取操作。