jGit - 如何将所有文件添加到暂存区

21
我尝试了很多方法使用 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();
我做错了什么? 谢谢。
在尝试使用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)
4个回答

25

调试的一个简单方式是查看AddCommand的测试,这些测试可以在JGit存储库中找到:AddCommandTest.java

您将看到,在添加所有文件时,从未使用模式“*”,而是使用模式“.”。
并且它在名为...testAddWholeRepo()的测试函数中被使用(!)

git.add().addFilepattern(".").call();

异常:

Exception in thread "main" org.eclipse.jgit.errors.NoWorkTreeException: 
Bare Repository has neither a working tree, nor an index

很明确:您需要在非裸仓库中添加文件。

请查看测试方法testCloneRepository(),将其与您自己的克隆进行比较,看看是否有任何区别。


但我将其克隆为非裸库...我不明白我应该做什么... - caarlos0
哈哈,它起作用了。我只需要使用Git git = cloneCmd.call(),并使用这个git实例来管理事情...谢谢@vonc - caarlos0
git.add().addFilepattern(".").call(); 没有将已删除的文件暂存。 - swaheed
1
@swaheed 我明白了,确实是这样的:https://github.com/eclipse/jgit/blob/2ab42b74d9803d8239ff4f9eda1cf153666a4079/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java#L676-L679 - VonC
1
@swaheed 你认为 git.add().addFilepattern("sub").setUpdate(true).call(); 更好吗?(https://github.com/eclipse/jgit/blob/2ab42b74d9803d8239ff4f9eda1cf153666a4079/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/AddCommandTest.java#L676-L679) - VonC

12

我有这样一个情况,需要将当前目录下的文件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

它认识到新文件temp/f1被创建,但没有检测到该文件先前已被删除。这可能是因为移动文件可以看作是:

  • 删除/剪切文件f1
  • 创建名为temp的文件夹
  • 创建/粘贴文件f1

然后我遇到了setUpdate(true),它寻找正在跟踪的文件更新并不会添加新文件。(查看java-doc以获取更多信息)

所以我必须将我的代码更改为两行代码,以便git能够识别新增和修改的文件(包括删除):

git.add().addFilepattern(".").call();
git.add().setUpdate(true).addFilepattern(".").call();

git status 现在会给出预期的结果:

renamed:    f1.hml -> temp/f1.html

遇到了完全相同的问题..谢谢!(但还没有解决..) - achetv

1

可能是通配符的问题,我刚刚阅读了add命令的javadoc,看起来你需要发送一个目录的名称才能添加其内容,而不是使用通配符:

addFilepattern

public AddCommand addFilepattern(String filepattern)

参数:filepattern - 要添加内容的文件。 也可以给出一个前导目录名称(例如,dir添加dir/file1dir/file2)以添加目录中的所有文件,递归地。 文件通配符(例如*.c)尚未支持。


0

关于一个问题的注释,我使用了File.separatorChar(根据您的操作系统,它将为您提供“/”或“\”)来更改目录,但实际上jgit仅使用“/”,如果您使用separatorChar,则在Windows上无法正常工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接