Example usage for org.eclipse.jgit.diff RawText isMissingNewlineAtEnd

List of usage examples for org.eclipse.jgit.diff RawText isMissingNewlineAtEnd

Introduction

In this page you can find the example usage for org.eclipse.jgit.diff RawText isMissingNewlineAtEnd.

Prototype

public boolean isMissingNewlineAtEnd() 

Source Link

Document

Determine if the file ends with a LF ('\n').

Usage

From source file:MySmartApply.java

License:Eclipse Distribution License

/**
 * @param f//  ww  w.j  a v  a 2 s .co 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:org.eclipse.egit.core.op.CreatePatchOperation.java

License:Open Source License

/**
 * Updates prefixes to workspace paths//w w w  .  jav a 2s .co  m
 *
 * @param sb
 * @param diffFmt
 */
public void updateWorkspacePatchPrefixes(StringBuilder sb, DiffFormatter diffFmt) {
    RawText rt = new RawText(sb.toString().getBytes());

    final String oldPrefix = diffFmt.getOldPrefix();
    final String newPrefix = diffFmt.getNewPrefix();

    StringBuilder newSb = new StringBuilder();
    final Pattern diffPattern = Pattern.compile("^diff --git (" + oldPrefix + "(.+)) (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    final Pattern oldPattern = Pattern.compile("^--- (" + oldPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$
    final Pattern newPattern = Pattern.compile("^\\+\\+\\+ (" + newPrefix + "(.+))$"); //$NON-NLS-1$ //$NON-NLS-2$

    int i = 0;
    while (i < rt.size()) {
        String line = rt.getString(i);

        Matcher diffMatcher = diffPattern.matcher(line);
        Matcher oldMatcher = oldPattern.matcher(line);
        Matcher newMatcher = newPattern.matcher(line);
        if (diffMatcher.find()) {
            String group = diffMatcher.group(2); // old path
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(diffMatcher.group(1), newPath.toString());
            group = diffMatcher.group(4); // new path
            newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(diffMatcher.group(3), newPath.toString());
        } else if (oldMatcher.find()) {
            String group = oldMatcher.group(2);
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(oldMatcher.group(1), newPath.toString());
        } else if (newMatcher.find()) {
            String group = newMatcher.group(2);
            IProject project = getProject(group);
            IPath newPath = computeWorkspacePath(new Path(group), project);
            line = line.replaceAll(newMatcher.group(1), newPath.toString());
        }
        newSb.append(line);

        i++;
        if (i < rt.size() || !rt.isMissingNewlineAtEnd())
            newSb.append(rt.getLineDelimiter());
    }
    // reset sb to newSb
    sb.setLength(0);
    sb.append(newSb);
}

From source file:playRepository.FileDiff.java

License:Apache License

private boolean checkEndOfLineMissing(final RawText text, final int line) {
    return line + 1 == text.size() && text.isMissingNewlineAtEnd();
}