Anu Chawla
Anu Chawla

Reputation: 445

jgit addFilePattern not working for my absolute path to a file

jgit version 4.6.0 , java 1.8 , windows 10

I have written a java program to push to a git repository where as I want to push specific files at a time instead of pushing all files.

git.add.addFilePattern(".").call(); works fine. but

git.add.addFilePattern("D:\\myGitRepo\\files\\file1.txt").call(); is not working.

"myGitRepo" folder contains the .git folder.

Upvotes: 0

Views: 2254

Answers (1)

Mincong Huang
Mincong Huang

Reputation: 5552

Summary

The Javadoc of method AddCommand#addFilepattern(String) says that: parameter filepattern is repository-relative path of file/directory to add (with / as separator). So you cannot use absolute path and cannot use backslash \. Indeed, you should:

  • Use repository-relative path
  • Use slash / to delimit directories on all platforms, even on Windows

Detail

When executing call() in AddCommand, JGit calls PathFilterGroup to create the file patterns, which calls PathFilter behind the screen. From PathFilter, we can see a more detailed definition about the file pattern:

/**
 * Create a new tree filter for a user supplied path.
 * <p>
 * Path strings are relative to the root of the repository. If the user's
 * input should be assumed relative to a subdirectory of the repository the
 * caller must prepend the subdirectory's path prior to creating the filter.
 * <p>
 * Path strings use '/' to delimit directories on all platforms.
 *
 * @param path
 *            the path to filter on. Must not be the empty string. All
 *            trailing '/' characters will be trimmed before string's length
 *            is checked or is used as part of the constructed filter.
 * @return a new filter for the requested path.
 * @throws IllegalArgumentException
 *             the path supplied was the empty string.
 */
public static PathFilter create(String path) { ... }

Upvotes: 4

Related Questions