Example usage for org.eclipse.jgit.patch HunkHeader getBuffer

List of usage examples for org.eclipse.jgit.patch HunkHeader getBuffer

Introduction

In this page you can find the example usage for org.eclipse.jgit.patch HunkHeader getBuffer.

Prototype

public byte[] getBuffer() 

Source Link

Document

Get the byte array holding this hunk's patch script.

Usage

From source file:MySmartApply.java

License:Eclipse Distribution License

/**
 * @param f// w w w  .j a v a 2 s  .c  o m
 * @param fh
 * @throws IOException
 * @throws PatchApplyException
 */
private void apply(File f, FileHeader fh) throws IOException, PatchApplyException {

    if (failed_files.contains(f))
        return;

    RawText rt = new RawText(f);

    List<String> oldLines = new ArrayList<String>(rt.size());

    for (int i = 0; i < rt.size(); i++)
        oldLines.add(rt.getString(i));

    List<String> newLines = new ArrayList<String>(oldLines);

    for (HunkHeader hh : fh.getHunks()) {
        StringBuilder hunk = new StringBuilder();

        for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++)
            hunk.append((char) hh.getBuffer()[j]);

        RawText hrt = new RawText(hunk.toString().getBytes());

        List<String> hunkLines = new ArrayList<String>(hrt.size());

        for (int i = 0; i < hrt.size(); i++) {
            String line = hrt.getString(i);

            line = removeInvalidChars(line);

            hunkLines.add(line);
        }

        int pos = 0;
        for (int j = 1; j < hunkLines.size(); j++) {

            String hunkLine = hunkLines.get(j);

            // We need the comparison only in case of removing a line or do nothing with it.
            String newLine = null;
            if (hunkLine.charAt(0) == ' ' || hunkLine.charAt(0) == '-')
                newLine = removeInvalidChars(newLines.get(hh.getNewStartLine() - 1 + pos));

            switch (hunkLine.charAt(0)) {

            case ' ':
                if (!newLine.equals(hunkLine.substring(1))) {
                    failed_files.add(f);
                    if (listener != null)
                        listener.failed();
                    return;
                }
                pos++;
                break;
            case '-':
                if (!newLine.equals(hunkLine.substring(1))) {
                    failed_files.add(f);
                    if (listener != null)
                        listener.failed();
                    return;
                }
                newLines.remove(hh.getNewStartLine() - 1 + pos);
                break;
            case '+':
                newLines.add(hh.getNewStartLine() - 1 + pos, hunkLine.substring(1));
                pos++;
                break;
            }
        }
    }

    if (!isNoNewlineAtEndOfFile(fh))
        newLines.add(""); //$NON-NLS-1$
    if (!rt.isMissingNewlineAtEnd())
        oldLines.add(""); //$NON-NLS-1$
    if (!isChanged(oldLines, newLines))
        return; // don't touch the file
    StringBuilder sb = new StringBuilder();
    for (String l : newLines) {
        // don't bother handling line endings - if it was windows, the \r is
        // still there!
        sb.append(l).append('\n');
    }
    sb.deleteCharAt(sb.length() - 1);
    FileWriter fw = new FileWriter(f);
    fw.write(sb.toString());
    fw.close();

    if (listener != null)
        listener.done();
}

From source file:MySmartApply.java

License:Eclipse Distribution License

private boolean isNoNewlineAtEndOfFile(FileHeader fh) {

    // ... ///*from  w w w.  j a  v a2 s  .  c o m*/

    if (fh.getHunks().size() == 0)
        return false;

    // ... //

    HunkHeader lastHunk = fh.getHunks().get(fh.getHunks().size() - 1);
    RawText lhrt = new RawText(lastHunk.getBuffer());
    return lhrt.getString(lhrt.size() - 1).equals("\\ No newline at end of file"); //$NON-NLS-1$
}

From source file:de.codesourcery.gittimelapse.TextFile.java

License:Apache License

public void forwardsPatchAndAlign(Patch patch) throws IOException, PatchApplyException {
    if (patch.getFiles().size() != 1) {
        throw new IllegalArgumentException("Patch needs to contain exactly 1 FileHeader");
    }//from w  ww .  ja  va  2  s. c o m
    final FileHeader fh = patch.getFiles().get(0);

    final List<String> oldLines = new ArrayList<String>(rawText.size());
    for (int i = 0; i < rawText.size(); i++) {
        oldLines.add(rawText.getString(i));
    }

    final List<String> newLines = new ArrayList<String>(oldLines);
    for (final HunkHeader hunkHeader : fh.getHunks()) {
        final StringBuilder hunk = new StringBuilder();
        for (int j = hunkHeader.getStartOffset(); j < hunkHeader.getEndOffset(); j++) {
            hunk.append((char) hunkHeader.getBuffer()[j]);
        }

        final RawText hunkRawText = new RawText(hunk.toString().getBytes());

        final List<String> hunkLines = new ArrayList<String>(hunkRawText.size());
        for (int i = 0; i < hunkRawText.size(); i++) {
            hunkLines.add(hunkRawText.getString(i));
        }

        int pos = 0;
        for (int j = 1; j < hunkLines.size(); j++) {
            final String hunkLine = hunkLines.get(j);
            switch (hunkLine.charAt(0)) {
            case ' ': // context line
                pos++;
                break;
            case '-': // line removed
                changesByLine.put(hunkHeader.getNewStartLine() - 1 + pos, ChangeType.DELETED);
                pos++;
                break;
            case '+': // line added
                changesByLine.put(hunkHeader.getNewStartLine() - 1 + pos, ChangeType.ADDED);
                newLines.add(hunkHeader.getNewStartLine() - 1 + pos, "               "); // JTextPane uses a colored background so we need to have SOMETHING on this line
                pos++;
                break;
            }
        }
    }
    if (!isNoNewlineAtEndOfFile(fh)) {
        newLines.add(""); //$NON-NLS-1$
    }
    if (!rawText.isMissingNewlineAtEnd()) {
        oldLines.add(""); //$NON-NLS-1$
    }
    final StringBuilder sb = new StringBuilder();
    for (final String l : newLines) {
        // don't bother handling line endings - if it was windows, the \r is
        // still there!
        sb.append(l).append('\n');
    }
    sb.deleteCharAt(sb.length() - 1);
    setText(sb.toString());
}

From source file:de.codesourcery.gittimelapse.TextFile.java

License:Apache License

private static boolean isNoNewlineAtEndOfFile(FileHeader fh) {
    final HunkHeader lastHunk = fh.getHunks().get(fh.getHunks().size() - 1);
    final RawText lhrt = new RawText(lastHunk.getBuffer());
    return lhrt.getString(lhrt.size() - 1).equals("\\ No newline at end of file"); //$NON-NLS-1$
}

From source file:de.codesourcery.gittimelapse.TextFile.java

License:Apache License

public void backwardsPatchAndAlign(Patch patch) throws IOException, PatchApplyException {
    if (patch.getFiles().size() != 1) {
        throw new IllegalArgumentException("Patch needs to contain exactly one FileHeader");
    }/*from  w  w  w. ja v  a2 s  .co m*/

    final FileHeader fh = patch.getFiles().get(0);
    final List<String> oldLines = new ArrayList<String>(rawText.size());
    for (int i = 0; i < rawText.size(); i++) {
        oldLines.add(rawText.getString(i));
    }

    final List<String> newLines = new ArrayList<String>(oldLines);
    for (final HunkHeader hunkHeader : fh.getHunks()) {
        final StringBuilder hunk = new StringBuilder();
        for (int j = hunkHeader.getStartOffset(); j < hunkHeader.getEndOffset(); j++) {
            hunk.append((char) hunkHeader.getBuffer()[j]);
        }

        final RawText hunkRawText = new RawText(hunk.toString().getBytes());

        final List<String> hunkLines = new ArrayList<String>(hunkRawText.size());
        for (int i = 0; i < hunkRawText.size(); i++) {
            hunkLines.add(hunkRawText.getString(i));
        }

        int pos = 0;
        for (int j = 1; j < hunkLines.size(); j++) {
            final String hunkLine = hunkLines.get(j);
            switch (hunkLine.charAt(0)) {
            case ' ':
                pos++;
                break;
            case '+':
                changesByLine.put(hunkHeader.getNewStartLine() - 1 + pos, ChangeType.DELETED);
                newLines.add(hunkHeader.getNewStartLine() - 1 + pos, "               ");
                pos++;
                break;
            case '-':
                changesByLine.put(hunkHeader.getNewStartLine() - 1 + pos, ChangeType.ADDED);
                pos++;
                break;
            }
        }
    }
    if (!isNoNewlineAtEndOfFile(fh)) {
        newLines.add(""); //$NON-NLS-1$
    }
    if (!rawText.isMissingNewlineAtEnd()) {
        oldLines.add(""); //$NON-NLS-1$
    }

    final StringBuilder sb = new StringBuilder();
    for (final String l : newLines) {
        // don't bother handling line endings - if it was windows, the \r is
        // still there!
        sb.append(l).append('\n');
    }
    sb.deleteCharAt(sb.length() - 1);
    setText(sb.toString());
}

From source file:de.codesourcery.gittimelapse.TextFile.java

License:Apache License

public void backwardsPatch(Patch patch) throws IOException, PatchApplyException {
    if (patch.getFiles().size() != 1) {
        throw new IllegalArgumentException("Patch needs to contain exactly one FileHeader");
    }/*from  www . j  a va 2s.  co m*/

    final FileHeader fh = patch.getFiles().get(0);

    final List<String> oldLines = new ArrayList<String>(rawText.size());
    for (int i = 0; i < rawText.size(); i++) {
        oldLines.add(rawText.getString(i));
    }
    final List<String> newLines = new ArrayList<String>(oldLines);
    for (final HunkHeader hh : fh.getHunks()) {
        final StringBuilder hunk = new StringBuilder();
        for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++) {
            hunk.append((char) hh.getBuffer()[j]);
        }
        final RawText hrt = new RawText(hunk.toString().getBytes());
        final List<String> hunkLines = new ArrayList<String>(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++) {
            final String hunkLine = hunkLines.get(j);
            switch (hunkLine.charAt(0)) {
            case ' ':
                if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(hunkLine.substring(1))) {
                    throw new PatchApplyException(MessageFormat.format(JGitText.get().patchApplyException, hh));
                }
                pos++;
                break;
            case '-':
                if (!newLines.get(hh.getNewStartLine() - 1 + pos).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));
                changesByLine.put(hh.getNewStartLine() - 1 + pos, ChangeType.ADDED);
                pos++;
                break;
            }
        }
    }
    if (!isNoNewlineAtEndOfFile(fh)) {
        newLines.add(""); //$NON-NLS-1$
    }

    if (!rawText.isMissingNewlineAtEnd()) {
        oldLines.add(""); //$NON-NLS-1$
    }

    final StringBuilder sb = new StringBuilder();
    for (final String l : newLines) {
        // don't bother handling line endings - if it was windows, the \r is
        // still there!
        sb.append(l).append('\n');
    }
    sb.deleteCharAt(sb.length() - 1);
    setText(sb.toString());
}

From source file:de.codesourcery.gittimelapse.TextFile.java

License:Apache License

public void forwardsPatch(Patch patch) throws IOException, PatchApplyException {
    if (patch.getFiles().size() != 1) {
        throw new IllegalArgumentException("Patch needs to contain exactly one FileHeader");
    }/*from www . j a v  a 2 s. c o m*/

    final FileHeader fh = patch.getFiles().get(0);

    final List<String> oldLines = new ArrayList<String>(rawText.size());
    for (int i = 0; i < rawText.size(); i++) {
        oldLines.add(rawText.getString(i));
    }
    final List<String> newLines = new ArrayList<String>(oldLines);
    for (final HunkHeader hh : fh.getHunks()) {
        final StringBuilder hunk = new StringBuilder();
        for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++) {
            hunk.append((char) hh.getBuffer()[j]);
        }
        final RawText hrt = new RawText(hunk.toString().getBytes());
        final List<String> hunkLines = new ArrayList<String>(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++) {
            final String hunkLine = hunkLines.get(j);
            switch (hunkLine.charAt(0)) {
            case ' ':
                pos++;
                break;
            case '-':
                changesByLine.put(hh.getNewStartLine() - 1 + pos, ChangeType.DELETED);
                pos++;
                break;
            case '+':
                break;
            }
        }
    }
    if (!isNoNewlineAtEndOfFile(fh)) {
        newLines.add(""); //$NON-NLS-1$
    }

    if (!rawText.isMissingNewlineAtEnd()) {
        oldLines.add(""); //$NON-NLS-1$
    }

    final StringBuilder sb = new StringBuilder();
    for (final String l : newLines) {
        // don't bother handling line endings - if it was windows, the \r is
        // still there!
        sb.append(l).append('\n');
    }
    sb.deleteCharAt(sb.length() - 1);
    setText(sb.toString());
}

From source file:org.eclipse.orion.server.git.patch.ApplyCommand.java

License:Open Source License

private void apply(File f, FileHeader fh, ApplyResult r) throws IOException {
    RawText rt = new RawText(f);
    List<String> oldLines = new ArrayList<String>(rt.size());
    for (int i = 0; i < rt.size(); i++) {
        oldLines.add(rt.getString(i));/*from  w w  w.  j a v a2 s  . co m*/
    }
    List<String> newLines = new ArrayList<String>(oldLines);
    HUNKS: for (HunkHeader hh : fh.getHunks()) {
        StringBuilder hunk = new StringBuilder();
        for (int j = hh.getStartOffset(); j < hh.getEndOffset(); j++) {
            hunk.append((char) hh.getBuffer()[j]);
        }
        RawText hrt = new RawText(hunk.toString().getBytes());
        List<String> hunkLines = new ArrayList<String>(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 (HunkControlChar.valueOf(hunkLine.charAt(0))) {
            case CONTEXT:
                if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(hunkLine.substring(1))) {
                    r.addApplyError(new ApplyError(hh, HunkControlChar.CONTEXT));
                    continue HUNKS;
                }
                pos++;
                break;
            case REMOVE:
                if (!newLines.get(hh.getNewStartLine() - 1 + pos).equals(hunkLine.substring(1))) {
                    r.addApplyError(new ApplyError(hh, HunkControlChar.REMOVE));
                    continue HUNKS;
                }
                newLines.remove(hh.getNewStartLine() - 1 + pos);
                break;
            case ADD:
                newLines.add(hh.getNewStartLine() - 1 + pos, hunkLine.substring(1));
                pos++;
                break;
            }
        }
    }
    if (!isChanged(oldLines, newLines))
        return; // don't touch the file

    StringBuilder sb = new StringBuilder();
    for (String l : newLines) {
        sb.append(l).append("\n"); // TODO: proper line ending
    }
    if (isNoNewlineAtEndOfFile(fh))
        sb.deleteCharAt(sb.length() - 1);
    FileWriter fw = new FileWriter(f);
    fw.write(sb.toString());
    fw.close();
}

From source file:org.eclipse.orion.server.git.patch.ApplyCommand.java

License:Open Source License

private boolean isNoNewlineAtEndOfFile(FileHeader fh) {
    HunkHeader lastHunk = fh.getHunks().get(fh.getHunks().size() - 1);
    RawText lhrt = new RawText(lastHunk.getBuffer());
    return lhrt.getString(lhrt.size() - 1).equals("\\ No newline at end of file"); //$NON-NLS-1$
}