【问题标题】:jgit addFilePattern not working for my absolute path to a filejgit addFilePattern 不适用于我的文件绝对路径
【发布时间】:2018-02-05 08:58:46
【问题描述】:

jgit 版本 4.6.0 , 爪哇 1.8 , 窗户 10

我编写了一个 java 程序来推送到 git 存储库,因为我想一次推送特定文件而不是推送所有文件。

git.add.addFilePattern(".").call(); 工作正常。 但是

git.add.addFilePattern("D:\\myGitRepo\\files\\file1.txt").call(); 不工作。

“myGitRepo”文件夹包含 .git 文件夹。

【问题讨论】:

    标签: java git jgit


    【解决方案1】:

    总结

    方法AddCommand#addFilepattern(String) 的Javadoc 说:参数filepattern 是要添加的文件/目录的存储库相对路径(以/ 作为分隔符)。所以不能使用绝对路径,也不能使用反斜杠\。事实上,你应该:

    • 使用存储库相对路径
    • 使用斜线 / 分隔所有平台上的目录,即使在 Windows 上也是如此

    详情

    AddCommand中执行call()时,JGit调用PathFilterGroup创建文件模式,在屏幕后面调用PathFilter。从PathFilter,我们可以看到更详细的文件模式定义:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      相关资源
      最近更新 更多