List of usage examples for org.eclipse.jgit.diff MyersDiff INSTANCE
DiffAlgorithm INSTANCE
To view the source code for org.eclipse.jgit.diff MyersDiff INSTANCE.
Click Source Link
From source file:com.diffplug.gradle.spotless.DiffMessageFormatter.java
License:Apache License
/** * Returns a git-style diff between the two unix strings. * * Output has no trailing newlines.// w w w .j a v a 2 s .c o m * * Boolean args determine whether whitespace or line endings will be visible. */ private static String diffWhitespaceLineEndings(String dirty, String clean, boolean whitespace, boolean lineEndings) throws IOException { dirty = visibleWhitespaceLineEndings(dirty, whitespace, lineEndings); clean = visibleWhitespaceLineEndings(clean, whitespace, lineEndings); RawText a = new RawText(dirty.getBytes(StandardCharsets.UTF_8)); RawText b = new RawText(clean.getBytes(StandardCharsets.UTF_8)); EditList edits = new EditList(); edits.addAll(MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try (DiffFormatter formatter = new DiffFormatter(out)) { formatter.format(edits, a, b); } String formatted = out.toString(StandardCharsets.UTF_8.name()); // we don't need the diff to show this, since we display newlines ourselves formatted = formatted.replace("\\ No newline at end of file\n", ""); return NEWLINE_MATCHER.trimTrailingFrom(formatted); }
From source file:com.google.gerrit.server.patch.IntraLineLoader.java
License:Apache License
static IntraLineDiff compute(Text aText, Text bText, List<Edit> edits) throws Exception { combineLineEdits(edits, aText, bText); for (int i = 0; i < edits.size(); i++) { Edit e = edits.get(i);//from w w w. j a v a2 s . c om if (e.getType() == Edit.Type.REPLACE) { CharText a = new CharText(aText, e.getBeginA(), e.getEndA()); CharText b = new CharText(bText, e.getBeginB(), e.getEndB()); CharTextComparator cmp = new CharTextComparator(); List<Edit> wordEdits = MyersDiff.INSTANCE.diff(cmp, a, b); // Combine edits that are really close together. If they are // just a few characters apart we tend to get better results // by joining them together and taking the whole span. // for (int j = 0; j < wordEdits.size() - 1;) { Edit c = wordEdits.get(j); Edit n = wordEdits.get(j + 1); if (n.getBeginA() - c.getEndA() <= 5 || n.getBeginB() - c.getEndB() <= 5) { int ab = c.getBeginA(); int ae = n.getEndA(); int bb = c.getBeginB(); int be = n.getEndB(); if (canCoalesce(a, c.getEndA(), n.getBeginA()) && canCoalesce(b, c.getEndB(), n.getBeginB())) { wordEdits.set(j, new Edit(ab, ae, bb, be)); wordEdits.remove(j + 1); continue; } } j++; } // Apply some simple rules to fix up some of the edits. Our // logic above, along with our per-character difference tends // to produce some crazy stuff. // for (int j = 0; j < wordEdits.size(); j++) { Edit c = wordEdits.get(j); int ab = c.getBeginA(); int ae = c.getEndA(); int bb = c.getBeginB(); int be = c.getEndB(); // Sometimes the diff generator produces an INSERT or DELETE // right up against a REPLACE, but we only find this after // we've also played some shifting games on the prior edit. // If that happened to us, coalesce them together so we can // correct this mess for the user. If we don't we wind up // with silly stuff like "es" -> "es = Addresses". // if (1 < j) { Edit p = wordEdits.get(j - 1); if (p.getEndA() == ab || p.getEndB() == bb) { if (p.getEndA() == ab && p.getBeginA() < p.getEndA()) { ab = p.getBeginA(); } if (p.getEndB() == bb && p.getBeginB() < p.getEndB()) { bb = p.getBeginB(); } wordEdits.remove(--j); } } // We sometimes collapsed an edit together in a strange way, // such that the edges of each text is identical. Fix by // by dropping out that incorrectly replaced region. // while (ab < ae && bb < be && cmp.equals(a, ab, b, bb)) { ab++; bb++; } while (ab < ae && bb < be && cmp.equals(a, ae - 1, b, be - 1)) { ae--; be--; } // The leading part of an edit and its trailing part in the same // text might be identical. Slide down that edit and use the tail // rather than the leading bit. If however the edit is only on a // whitespace block try to shift it to the left margin, assuming // that it is an indentation change. // boolean aShift = true; if (ab < ae && isOnlyWhitespace(a, ab, ae)) { int lf = findLF(wordEdits, j, a, ab); if (lf < ab && a.charAt(lf) == '\n') { int nb = lf + 1; int p = 0; while (p < ae - ab) { if (cmp.equals(a, ab + p, a, ab + p)) { p++; } else { break; } } if (p == ae - ab) { ab = nb; ae = nb + p; aShift = false; } } } if (aShift) { while (0 < ab && ab < ae && a.charAt(ab - 1) != '\n' && cmp.equals(a, ab - 1, a, ae - 1)) { ab--; ae--; } if (!a.isLineStart(ab) || !a.contains(ab, ae, '\n')) { while (ab < ae && ae < a.size() && cmp.equals(a, ab, a, ae)) { ab++; ae++; if (a.charAt(ae - 1) == '\n') { break; } } } } boolean bShift = true; if (bb < be && isOnlyWhitespace(b, bb, be)) { int lf = findLF(wordEdits, j, b, bb); if (lf < bb && b.charAt(lf) == '\n') { int nb = lf + 1; int p = 0; while (p < be - bb) { if (cmp.equals(b, bb + p, b, bb + p)) { p++; } else { break; } } if (p == be - bb) { bb = nb; be = nb + p; bShift = false; } } } if (bShift) { while (0 < bb && bb < be && b.charAt(bb - 1) != '\n' && cmp.equals(b, bb - 1, b, be - 1)) { bb--; be--; } if (!b.isLineStart(bb) || !b.contains(bb, be, '\n')) { while (bb < be && be < b.size() && cmp.equals(b, bb, b, be)) { bb++; be++; if (b.charAt(be - 1) == '\n') { break; } } } } // If most of a line was modified except the LF was common, make // the LF part of the modification region. This is easier to read. // if (ab < ae // && (ab == 0 || a.charAt(ab - 1) == '\n') // && ae < a.size() && a.charAt(ae - 1) != '\n' && a.charAt(ae) == '\n') { ae++; } if (bb < be // && (bb == 0 || b.charAt(bb - 1) == '\n') // && be < b.size() && b.charAt(be - 1) != '\n' && b.charAt(be) == '\n') { be++; } wordEdits.set(j, new Edit(ab, ae, bb, be)); } edits.set(i, new ReplaceEdit(e, wordEdits)); } } return new IntraLineDiff(edits); }
From source file:com.googlesource.gerrit.plugins.serverconfig.UnifiedDiffer.java
License:Apache License
public String diff(RawText v0, RawText v1) throws IOException { DiffAlgorithm algorithm = MyersDiff.INSTANCE; EditList editList = algorithm.diff(RawTextComparator.DEFAULT, v0, v1); ByteArrayOutputStream os = new ByteArrayOutputStream(); try (DiffFormatter formatter = new DiffFormatter(os)) { formatter.format(editList, v0, v1); }//from w ww. ja v a 2 s . c o m return os.toString(CHARSET_NAME); }
From source file:com.madgag.agit.diff.LineContextDiffer.java
License:Open Source License
/** * Format a patch script for one file entry. * //from w w w . j a va2s . co m * @param ent * the entry to be formatted. * @throws IOException * a file's content cannot be read, or the output stream cannot * be written to. */ public List<Hunk> format(DiffEntry ent) throws IOException { //writeDiffHeader(out, ent); if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) { // writeGitLinkDiffText(out, ent); return emptyList(); } else { byte[] aRaw, bRaw; try { aRaw = open(objectReader, ent.getOldMode(), ent.getOldId()); bRaw = open(objectReader, ent.getNewMode(), ent.getNewId()); } finally { // objectReader.release(); } if (RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) { //out.write(encodeASCII("Binary files differ\n")); return emptyList(); } else { RawText a = new RawText(aRaw); RawText b = new RawText(bRaw); return formatEdits(a, b, MyersDiff.INSTANCE.diff(DEFAULT, a, b)); } } }
From source file:org.eclipse.egit.ui.internal.history.FileDiff.java
License:Open Source License
private void outputEclipseDiff(final StringBuilder d, final Repository db, final ObjectReader reader, final DiffFormatter diffFmt) throws IOException { if (!(getBlobs().length == 2)) throw new UnsupportedOperationException( "Not supported yet if the number of parents is different from one"); //$NON-NLS-1$ String projectRelativePath = getProjectRelativePath(db, getPath()); d.append("diff --git ").append(projectRelativePath).append(" ") //$NON-NLS-1$ //$NON-NLS-2$ .append(projectRelativePath).append("\n"); //$NON-NLS-1$ final ObjectId id1 = getBlobs()[0]; final ObjectId id2 = getBlobs()[1]; final FileMode mode1 = getModes()[0]; final FileMode mode2 = getModes()[1]; if (id1.equals(ObjectId.zeroId())) { d.append("new file mode " + mode2).append("\n"); //$NON-NLS-1$//$NON-NLS-2$ } else if (id2.equals(ObjectId.zeroId())) { d.append("deleted file mode " + mode1).append("\n"); //$NON-NLS-1$//$NON-NLS-2$ } else if (!mode1.equals(mode2)) { d.append("old mode " + mode1); //$NON-NLS-1$ d.append("new mode " + mode2).append("\n"); //$NON-NLS-1$//$NON-NLS-2$ }/*w w w.j a v a2s .c o m*/ d.append("index ").append(reader.abbreviate(id1).name()). //$NON-NLS-1$ append("..").append(reader.abbreviate(id2).name()). //$NON-NLS-1$ append(mode1.equals(mode2) ? " " + mode1 : "").append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if (id1.equals(ObjectId.zeroId())) d.append("--- /dev/null\n"); //$NON-NLS-1$ else { d.append("--- "); //$NON-NLS-1$ d.append(getProjectRelativePath(db, getPath())); d.append("\n"); //$NON-NLS-1$ } if (id2.equals(ObjectId.zeroId())) d.append("+++ /dev/null\n"); //$NON-NLS-1$ else { d.append("+++ "); //$NON-NLS-1$ d.append(getProjectRelativePath(db, getPath())); d.append("\n"); //$NON-NLS-1$ } final RawText a = getRawText(id1, reader); final RawText b = getRawText(id2, reader); EditList editList = MyersDiff.INSTANCE.diff(RawTextComparator.DEFAULT, a, b); diffFmt.format(editList, a, b); }
From source file:org.mytake.foundation.transcript.TranscriptMatch.java
static List<Edit> edits(List<? extends Word> a, List<? extends Word> b) { return MyersDiff.INSTANCE.diff(new WordTimeMatcher(), new ListSequence(a), new ListSequence(b)); }