Mihai8
Mihai8

Reputation: 3147

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?

Upvotes: 0

Views: 262

Answers (1)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 20985

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, i.e. there must not be a leading '/'.

Upvotes: 2

Related Questions