JGit中DirCacheEditor的路径异常无效

问题描述 投票:0回答:1

我测试了一段代码

  DirCache index = repository.lockDirCache();
  DirCacheEditor editor = index.editor();
  editor.add(new DirCacheEditor.PathEdit(path + File.separator + fileName) {
      @Override
      public void apply(DirCacheEntry entry) {
          entry.setFileMode(FileMode.REGULAR_FILE);
      }
  });
  editor.finish();

其中path是目录的绝对路径,其中是存储库,fileName是我想要添加它的文件。但是,该代码会抛出“无效路径”消息的异常。

path应该有什么价值才能让这个例外不再出现?

java jgit
1个回答
2
投票

必须始终相对于存储库的根目录给出JGit中的路径。所有平台上的路径分隔符都是'/'。

因此,您的代码应如下所示。

String path = "path/to";
String fileName = "file.ext";
...
new PathEdit(path + "/" + fileName)

导致这样的路径:path/to/file.ext

另请注意,大多数JGit API需要相对路径,即不得有前导'/'。

© www.soinside.com 2019 - 2024. All rights reserved.