加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – jGit – 如何将所有文件添加到暂存区域

发布时间:2020-12-14 05:02:35 所属栏目:Java 来源:网络整理
导读:我用很多方法尝试用j Git克隆一个repo(它的工作). 然后,我在存储库中写一些存档,并尝试添加所有(一个git add *,git add -A或类似的东西)..但它不起作用.文件简单不会添加到暂存区域. 我的代码是这样的: FileRepositoryBuilder builder = new FileRepository
我用很多方法尝试用j Git克隆一个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 repo: AddCommandTest.java中的 AddCommand的测试

您将看到,为了添加所有文件,模式“*”从不使用,而是“.”是.
它被用于名为… testAddWholeRepo()的测试函数(!)

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

例外:

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

相当明确:您需要在非裸机中添加文件.

请参阅test method testCloneRepository()与您自己的克隆进行比较,看看是否有差异.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读