jGit - 如何将所有文件添加到暂存区域 [英] jGit - how to add all files to staging area
问题描述
我尝试了很多方法来用jGit克隆一个回购(它有效)。
然后,我在存储库中写入一些存档,并尝试添加所有( 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);
尝试{
clone.call();
} catch(GitAPIException e){
e.printStackTrace();
Files.write(测试它...,新文件(文件夹+/test2.txt),
Charsets.UTF_8);
Git g = new Git(repository);
g.add()。addFilepattern(*)。call();
我做错了什么?
谢谢。
尝试addFilePattern(。)时出现异常:
线程main中的异常org.eclipse.jgit.errors.NoWorkTreeException:Bare Repository既没有工作树也没有索引
at org.eclipse.jgit.lib.Repository.getIndexFile(Repository.java:850)
在org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:264)
在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)
调试这个是看看 AddCommand 在 JGit repo 中: AddCommandTest.java $ c $你会看到为了添加所有文件,模式
*
是从未使用过,但是。
是。
它被用于名为... testAddWholeRepo()
(!)
$ b
git.add()。addFilepattern( 。)。呼叫();
例外:
线程main中的异常org.eclipse.jgit.errors.NoWorkTreeException:
Bare Repository既没有工作树也没有索引
非常明确:您需要在非裸露的回购库中添加文件。
请参阅测试方法 testCloneRepository()
与您自己的克隆进行比较,并查看是否有任何区别。
I tried in a lot of ways to clone a repo with jGit (it works).
Then, I write some archive in the repository, and tried to add all (a git add *
, git add -A
or something like it).. but it don't work. The files simple are not added to the staging area.
My code is like this:
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();
What am I doing wrong? Thanks.
Exception while trying what with 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)
One easy way to debug this is to look at the tests of the AddCommand in the JGit repo: AddCommandTest.java
You will see that in order to add all files the pattern "*
" is never used, but ".
" is.
And it is used in the test function named... testAddWholeRepo()
(!)
git.add().addFilepattern(".").call();
The Exception:
Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException:
Bare Repository has neither a working tree, nor an index
is quite explicit: you need to add file in a non-bare repo.
See test method testCloneRepository()
to compare with your own clone, and see if there is any difference.
这篇关于jGit - 如何将所有文件添加到暂存区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!