簡體   English   中英

Jgit:Bare Repository沒有工作樹,也沒有索引

[英]Jgit:Bare Repository has neither a working tree, nor an index

我在E中創建了一個名為gitrepo的目錄,完整路徑為(E:\\ gitrepo),然后用以下代碼在其中克隆了一個存儲庫

Git git=Git.cloneRepository()
                .setURI("samplelink.git")
                .setDirectory(new File("/E:/gitrepo"))
                .call();

然后我使用此代碼打開一個存儲庫

public Repository openRepository() throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();

    Repository repository = builder.setGitDir(new File("/E:/gitrepo"))
            .readEnvironment() // scan environment GIT_* variables
            .findGitDir() // scan up the file system tree
            .build();
     log.info("Repository directory is {}", repository.getDirectory());

    return repository;
}

一切正常,直到這里,然后我嘗試在此本地存儲庫中添加文件

Repository repo = openRepository();
            Git git = new Git(repo);
            File myfile = new File(repo.getDirectory()/*.getParent()*/, "testfile");
            if (!myfile.createNewFile()) {
                throw new IOException("Could not create file " + myfile);
            }
            log.info("file created at{}", myfile.getPath());
            git.add().addFilepattern("testfile").call();

然后我在這條線上有例外

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

這是例外

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:1147)
    at org.eclipse.jgit.dircache.DirCache.lock(DirCache.java:294)
    at org.eclipse.jgit.lib.Repository.lockDirCache(Repository.java:1205)
    at org.eclipse.jgit.api.AddCommand.call(AddCommand.java:149)
    at com.km.GitAddFile.addFile(GitAddFile.java:26)

雖然我在E:\\gitrepo創建的文件代碼已通過此命令檢查gitrepo是非裸存儲庫

/e/gitrepo (master)
$ git rev-parse --is-bare-repository 

及其返回的false

請幫助我如何解決此異常

使用FileRepositoryBuilder打開Git倉庫很棘手。 這是一個內部類。 其方法setGitDir(File)定義存儲庫元數據( .git文件夾)的位置。 換句話說,它用於構建Git裸存儲庫。 您可以通過調用Repository#isBare()來證明這一點:

Repository repository = builder.setGitDir(new File("/E:/gitrepo"))
        .readEnvironment() // scan environment GIT_* variables
        .findGitDir() // scan up the file system tree
        .build();
repository.isBare(); // returns true

您應該用Git#open(File)代替這種用法:

try (Git git = Git.open(new File("/E:/gitrepo"))) {
    // Do sth ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM