Можно ли прочитать файл патча из JGit? - PullRequest
0 голосов
/ 26 сентября 2018

У меня есть сценарий оболочки, который генерирует файл исправления:

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

Теперь мне нужно прочитать файл исправления diff1.txt из JGIT, чтобы получить все изменения файла.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

1 Ответ

0 голосов
/ 28 сентября 2018

Изучив исходный код 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;
}
...