我尝试了很多方法来用 jGit 克隆一个 repo(它有效)。然后,我在存储库中编写了一些存档,并尝试添加所有(agit 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();
我究竟做错了什么?谢谢。
尝试使用 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)