是否可以从 JGit 读取补丁文件?

Is it possible to read a patch file from JGit?

我有一个 shell 生成补丁文件的脚本:

git diff --word-diff origin/$ghprbTargetBranch origin/ > diff1.txt

现在我需要从 JGIT 读取 diff1.txt 补丁文件以获取所有文件更改。 diff1.txt 如下所示:

diff --git a/src/objects/Account.object b/src/objects/Account.object
index 0bd43e4..f827031 100644
--- a/src/objects/Account.object
+++ b/src/objects/Account.object
@@ -133,6 +133,14 @@
        <fullName>CleanStatus</fullName>
        <trackFeedHistory>false</trackFeedHistory>
    </fields>
    {+<fields>+}
{+      <fullName>MatchAddress__c</fullName>+}
{+      <defaultValue>false</defaultValue>+}
{+      <externalId>false</externalId>+}
{+      <label>MatchAddress</label>+}
{+      <trackFeedHistory>false</trackFeedHistory>+}
{+      <type>Checkbox</type>+}
{+  </fields>+}
    <fields>
        <fullName>CustomerPriority__c</fullName>
        <externalId>false</externalId>
@@ -240,6 +248,17 @@
        <type>Number</type>
        <unique>false</unique>
    </fields>
    {+<fields>+}
{+      <fullName>Check1__c</fullName>+}
{+      <externalId>false</externalId>+}
{+      <label>check1</label>+}
{+      <precision>18</precision>+}
{+      <required>false</required>+}
{+      <scale>0</scale>+}
{+      <trackFeedHistory>false</trackFeedHistory>+}
{+      <type>Number</type>+}
{+      <unique>false</unique>+}
{+  </fields>+}
    <fields>
        <fullName>NumberofLocations__c</fullName>
        <externalId>false</externalId>

是否可以从 Java 读取补丁文件并仅获取正在添加的行?

输出

<fields>
    <fullName>MatchAddress__c</fullName>
    <defaultValue>false</defaultValue>
    <externalId>false</externalId>
    <label>MatchAddress</label>
    <trackFeedHistory>false</trackFeedHistory>
    <type>Checkbox</type>
</fields>

and the next field Check1__C

看了JGIT的源码,发现JGit是怎么打补丁的,下面是JGit读取补丁打补丁的方式,我只是想读一个补丁文件。

public static void main(String[] args) throws IOException, PatchFormatException, PatchApplyException {

    for (String arg : args) {
        System.out.println(arg);
    }
    // args[0] - patch file path
    String patchFilePath = "C:\JenkinsNew\workspace\ValidateJob\diffPatch.patch";

    Patch patch = new Patch();
    patch.parse(new FileInputStream(new File(patchFilePath)));
    if (!patch.getErrors().isEmpty())
        throw new PatchFormatException(patch.getErrors());
    for (FileHeader fh : patch.getFiles()) {
        File file = getFile(fh.getOldPath(), false, "");
        List<String> apply = apply(file, fh);
        for (String s : apply) {
            System.out.println(s);
        }
    }
}

private static File getFile(String path, boolean create, String workSpacePath)
        throws PatchApplyException {
    // args[1] : the workspace
    File f = new File("C:\JenkinsNew\workspace\ValidateJob", path);
    if (create)
        try {
            File parent = f.getParentFile();
            FileUtils.mkdirs(parent, true);
            FileUtils.createNewFile(f);
        } catch (IOException e) {
            throw new PatchApplyException(MessageFormat.format(JGitText.get().createNewFileFailed, f), e);
        }
    return f;
}

private static List<String> apply(File f, FileHeader fh)
        throws IOException, PatchApplyException {
    RawText rt = new RawText(f);
    List<String> oldLines = new ArrayList<>(rt.size());
    for (int i = 0; i < rt.size(); i++)
        oldLines.add(rt.getString(i));
    List<String> newLines = new ArrayList<>(oldLines);
    List<String> newLineToBeAdded = new ArrayList<>();
    for (HunkHeader hh : fh.getHunks()) {

        byte[] b = new byte[hh.getEndOffset() - hh.getStartOffset()];
        System.arraycopy(hh.getBuffer(), hh.getStartOffset(), b, 0, b.length);
        RawText hrt = new RawText(b);

        List<String> hunkLines = new ArrayList<>(hrt.size());
        for (int i = 0; i < hrt.size(); i++)
            hunkLines.add(hrt.getString(i));
        int pos = 0;
        for (int j = 1; j < hunkLines.size(); j++) {
            String hunkLine = hunkLines.get(j);
            switch (hunkLine.charAt(0)) {
                case '-':
                    if (hh.getNewStartLine() == 0) {
                        newLines.clear();
                    } else {
                        if (!(newLines.get(hh.getNewStartLine() - 1 + pos).replaceAll("([\r\n])", "")).equals(hunkLine.substring(1))) {
                            throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, hh));
                        }
                        newLines.remove(hh.getNewStartLine() - 1 + pos);
                    }
                    break;
                case '+':
                    newLines.add(hh.getNewStartLine() - 1 + pos,hunkLine.substring(1));
                    newLineToBeAdded.add(hunkLine.substring(1));
                    pos++;
                    break;
            }
        }
    }
    return newLineToBeAdded;
}