jgit addFilePattern not working for my absolute path to a file

六眼飞鱼酱① 提交于 2019-12-11 15:17:01

问题


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.


回答1:


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) { ... }


来源:https://stackoverflow.com/questions/45913082/jgit-addfilepattern-not-working-for-my-absolute-path-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!