我测试了一段代码
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
应该有什么价值才能让这个例外不再出现?
必须始终相对于存储库的根目录给出JGit中的路径。所有平台上的路径分隔符都是'/'。
因此,您的代码应如下所示。
String path = "path/to";
String fileName = "file.ext";
...
new PathEdit(path + "/" + fileName)
导致这样的路径:path/to/file.ext
另请注意,大多数JGit API需要相对路径,即不得有前导'/'。