简体   繁体   中英

Invalid path exception with DirCacheEditor in JGit

I test a piece of code that is

  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();

where path is an absolute path for directory where is repository and fileName is the file that I want to add it. However, that code throws an exception with message "Invalid path".

What value should path have so that this exception will no longer appear?

Paths in JGit must always be given relative to the repository's root directory. Also the path separator is '/' on all platforms.

Hence your code should look like this.

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

to result in a path like this: path/to/file.ext

Also note that most JGit APIs require relative paths, ie there must not be a leading '/'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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