[英]jGit - how to add all files to staging area
I tried in a lot of ways to clone a repo with jGit (it works).我尝试了很多方法来用 jGit 克隆一个 repo(它有效)。 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.然后,我在存储库中编写了一些存档,并尝试添加所有(一个
git add *
, git add -A
或类似的东西)..但它不起作用。 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("."):尝试使用 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
一种简单的调试方法是查看JGit存储库中的AddCommand测试:
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()
与您自己的克隆进行比较,看看是否有任何差异。
I had a situation where I had to move a file f1 from the current directory to another directory called 'temp'.我有一种情况,我必须将文件 f1 从当前目录移动到另一个名为“temp”的目录。 After moving the file, calling git.add().addFilePattern(".").call() acted in a weird way since git status gave the following result:
移动文件后,调用 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.它认识到创建了一个新文件 temp/f1 但没有检测到该文件首先被删除。 This was perhaps because moving the file can be seen as follows
这可能是因为移动文件可以看到如下
Then I came across the setUpdate(true)
that looks for updates to files that are already being tracked and will not stage new files.然后我遇到了
setUpdate(true)
,它查找已经被跟踪的文件的更新,并且不会暂存新文件。 (Check java-doc for more info) (查看 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
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.参数:
filepattern
- 要从中添加内容的文件。 Also a leading directory name (eg dir to add dir/file1
and dir/file2
) can be given to add all files in the directory , recursively.还可以给出一个前导目录名称(例如 dir 以添加
dir/file1
和dir/file2
)以递归地添加目录中的所有文件。 Fileglobs (eg *.c
) are not yet supported.尚不支持 Fileglob(例如
*.c
)。
只是为了记下我在使用 File.separatorChar 时遇到的问题(根据您的操作系统,它会给您“/”或“\”)来更改目录,但实际上 jgit 只使用“/”并且会执行如果您使用 separatorChar 自己工作,它将无法在 Windows 上工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.