我有一个带有预提交钩子的git repo,它故意在100%的时间内失败.
cat .git/hooks/pre-commit
> exit 1
Run Code Online (Sandbox Code Playgroud)
如果我尝试通过命令行提交,它会按预期失败.但是,如果我从egit提交,则忽略钩子并且提交更改.
egit/jgit不能识别挂钩吗?这有解决方法吗?
提前致谢!
我尝试了很多方法用jGit克隆一个repo(它有效).然后,我写的一些库存档,并尝试添加所有(一个git add *,git add -A或者类似的东西)..但它不工作.文件简单不会添加到临时区域.
我的代码是这样的:
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder.setGitDir(new File(folder))
.readEnvironment().findGitDir().setup().build();
CloneCommand clone = Git.cloneRepository();
clone.setBare(false).setCloneAllBranches(true);
clone.setDirectory(f).setURI("git@192.168.2.43:test.git");
try {
clone.call();
} catch (GitAPIException e) {
e.printStackTrace();
}
Files.write("testing it...", new File(folder + "/test2.txt"),
Charsets.UTF_8);
Git g = new Git(repository);
g.add().addFilepattern("*").call();
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?谢谢.
尝试使用addFilePattern(".")时遇到异常:
Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: Bare Repository has neither a working tree, nor an index
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:850)
at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:264)
at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:906)
at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:138)
at net.ciphersec.git.GitTests.main(GitTests.java:110)
Run Code Online (Sandbox Code Playgroud) 我有以下代码(请参阅有关正在发生的事情的评论):
// Clone repository from GitHub into a local directory.
Git git = Git.cloneRepository()
.setBranch("gh-pages")
.setURI("https://github.com/RAnders00/KayonDoc.git")
.setDirectory(new File("/home/ubuntu/KayonDoc"))
.call();
// Print out remotes in config from JGit
Config config = git.getRepository().getConfig();
config.getSubsections("remote").forEach(it -> {
System.out.println(config.getString("remote", it, "url"));
});
// Prints https://github.com/RAnders00/KayonDoc.git
// Everything seems OK
// You could perform some changes to the repository here...
// Push changes to origin
git.push()
.setCredentialsManager(new UsernamePasswordCredentialsProvider("RAnders00", "hunter2"))
.call();
// Throws exception (look below)
Run Code Online (Sandbox Code Playgroud)
Caught: org.eclipse.jgit.api.errors.TransportException: git@github.com:RAnders00/KayonDoc.git: push not permitted
org.eclipse.jgit.api.errors.TransportException: git@github.com:RAnders00/KayonDoc.git: push not …Run Code Online (Sandbox Code Playgroud) 我想使用JGit显示头版本的所有文件和文件夹的列表.我能够使用TreeWalk列出所有文件,但这不会列出文件夹.
这是我到目前为止:
public class MainClass {
public static void main(String[] args) throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
Repository repository = builder
.setGitDir(new File("C:\\temp\\git\\.git")).readEnvironment()
.findGitDir().build();
listRepositoryContents(repository);
repository.close();
}
private static void listRepositoryContents(Repository repository) throws IOException {
Ref head = repository.getRef("HEAD");
// a RevWalk allows to walk over commits based on some filtering that is defined
RevWalk walk = new RevWalk(repository);
RevCommit commit = walk.parseCommit(head.getObjectId());
RevTree tree = commit.getTree();
System.out.println("Having tree: " + tree);
// now use a TreeWalk to …Run Code Online (Sandbox Code Playgroud) 我正在使用JGit访问远程Git仓库,我需要使用SSH.JGit使用JSch提供安全访问.但是,我不确定如何为JGit设置密钥文件和已知的hosts文件.我试过的内容如下.
使用子类化JSchConfigSessionFactory创建SshSessionFactory的自定义配置:
public class CustomJschConfigSessionFactory extends JschConfigSessionFactory {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setConfig("StrictHostKeyChecking", "yes");
}
}
Run Code Online (Sandbox Code Playgroud)
在我访问远程Git仓库的类中,执行以下操作:
CustomJschConfigSessionFactory jschConfigSessionFactory = new CustomJschConfigSessionFactory();
JSch jsch = new JSch();
try {
jsch.addIdentity(".ssh/id_rsa");
jsch.setKnownHosts(".ssh/known_hosts");
} catch (JSchException e) {
e.printStackTrace();
}
SshSessionFactory.setInstance(jschConfigSessionFactory);
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何将此JSch对象与JGit关联,以便它可以成功连接到远程存储库.当我尝试使用JGit克隆它时,我得到以下异常:
org.eclipse.jgit.api.errors.TransportException: git@git.test.com:abc.org/test_repo.git: reject HostKey: git.test.com
at org.eclipse.jgit.api.FetchCommand.call(FetchCommand.java:137)
at org.eclipse.jgit.api.CloneCommand.fetch(CloneCommand.java:178)
at org.eclipse.jgit.api.CloneCommand.call(CloneCommand.java:125)
at GitTest.cloneRepo(GitTest.java:109)
at GitTest.main(GitTest.java:223)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) …Run Code Online (Sandbox Code Playgroud) 我想在eclipse中使用GIT,最好是EGit,因为它得到了eclipse社区本身的支持.然而,尽管eclipse声称安装了所需的插件,但它并没有出现在透视图,导入和设置菜单中.
我正在使用Eclipse Juno for Java EE,并确认了以下安装:
当我更新软件时,没有更多可用的更新.当我去Egit或JGit的存储库时,eclipse告诉我已经安装了插件.
窗口>首选项>团队不包含Git项目,仅包含CVS和SVN.导入项目不包含Git选项.透视窗口不包含Git repo选项.
该RevCommit有一个getCommitTime()方法,但它返回和int,它有没有作者的时间.如何从RevCommit获取作者和提交日期?
我正在尝试使用jGit克隆存储库并签出特定的提交.
假设提交哈希是:1e9ae842ca94f326215358917c620ac407323c81.
我的第一步是:
// Cloning the repository
Git.cloneRepository()
.setURI(remotePath)
.setDirectory(localPath)
.call();
Run Code Online (Sandbox Code Playgroud)
然后我发现了另一个提出这种方法的问题:
git.checkout().
setCreateBranch(true).
setName("branchName").
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).
setStartPoint("origin/" + branchName).
call();
Run Code Online (Sandbox Code Playgroud)
但我不确定如何将两者联系在一起?
有什么想法吗?
jgit ×10
git ×6
java ×6
egit ×2
circleci ×1
eclipse-juno ×1
githooks ×1
jsch ×1
shallow-copy ×1
ssh ×1
ubuntu-12.04 ×1