繁体   English   中英

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

[英]jGit - how to add all files to staging area

我尝试了很多方法来用 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)

一种简单的调试方法是查看JGit存储库中的AddCommand测试: 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()与您自己的克隆进行比较,看看是否有任何差异。

我有一种情况,我必须将文件 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

可能是通配符,我刚刚阅读了 add 命令的 javadoc,看起来您发送目录的名称是为了添加其内容而不是通配符:

addFilepattern

public AddCommand addFilepattern(String filepattern)

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

只是为了记下我在使用 File.separatorChar 时遇到的问题(根据您的操作系统,它会给您“/”或“\”)来更改目录,但实际上 jgit 只使用“/”并且会执行如果您使用 separatorChar 自己工作,它将无法在 Windows 上工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM