git jGit - 如何将所有文件添加到暂存区
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12734760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jGit - how to add all files to staging area
提问by caarlos0
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.
我尝试了很多方法来使用 jGit 克隆一个 repo(它有效)。然后,我写的一些库存档,并尝试添加所有(一个git add *
,git add -A
或者类似的东西)..但它不工作。文件 simple 不会添加到暂存区。
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("[email protected]: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("."):
尝试使用 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)
回答by VonC
One easy way to debug this is to look at the tests of the AddCommandin the JGit repo: AddCommandTest.java
一个简单的方法来调试这是看的测试AddCommand在JGit回购: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()
(!)
您将看到,为了添加所有文件,*
从不使用模式“ ”,而使用“ .
”。
它用于名为...的测试函数中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.
查看测试方法testCloneRepository()
与您自己的克隆进行比较,看看是否有任何差异。
回答by hipsandy
I had a situation where I had to move a file f1 from the current directory to another directory called 'temp'. After moving the file, calling git.add().addFilePattern(".").call() acted in a weird way since git status gave the following result:
我遇到了一种情况,我不得不将文件 f1 从当前目录移动到另一个名为“temp”的目录。移动文件后,调用 git.add().addFilePattern(".").call() 的行为很奇怪,因为 git status 给出了以下结果:
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: temp/f1.html
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: f1.html
It recognized that a new file temp/f1 was created but didn't detect that the file was deleted first. This was perhaps because moving the file can be seen as follows
它识别出创建了一个新文件 temp/f1 但没有检测到该文件首先被删除。这可能是因为移动文件可以看到如下
- Deleting/Cutting the file f1
- Creating a folder called temp
- Creating/Pasting the file f1
- 删除/剪切文件 f1
- 创建一个名为 temp 的文件夹
- 创建/粘贴文件 f1
Then I came across the setUpdate(true)
that looks for updates to files that are already being tracked and will not stage new files. (Check java-doc for more info)
然后我遇到了setUpdate(true)
它会查找已被跟踪的文件的更新,并且不会暂存新文件。(查看 java-doc 了解更多信息)
So I had to change my code to two lines like so in order for git to recognize both files added and modified (which includes deletion):
所以我不得不将我的代码更改为两行,以便 git 识别添加和修改的文件(包括删除):
git.add().addFilepattern(".").call();
git.add().setUpdate(true).addFilepattern(".").call();
git status now gives the expected result:
git status 现在给出了预期的结果:
renamed: f1.hml -> temp/f1.html
回答by jbusick
It might be the wildcard, I just read the javadoc for the add command, looks like you send the name of a directory in order to add its contents not a wild card:
它可能是通配符,我刚刚阅读了 add 命令的 javadoc,看起来您发送目录的名称是为了添加其内容而不是通配符:
addFilepattern
public AddCommand addFilepattern(String filepattern)
Parameters: filepattern
- File to add content from. Also a leading directory
name (e.g. dir to add dir/file1
and dir/file2
) can be given to add all
files in the directory, recursively. Fileglobs (e.g. *.c
) are not yet
supported.
参数:filepattern
- 要从中添加内容的文件。还可以给出前导目录名称(例如要添加的目录dir/file1
和dir/file2
)以递归地添加目录中的所有文件。尚不支持Fileglobs(例如*.c
)。