List of usage examples for org.eclipse.jgit.diff RawText RawText
public RawText(File file) throws IOException
From source file:MySmartApply.java
License:Eclipse Distribution License
/** * @param f/* w w w. j ava 2s . 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) { // ... //// ww w.j ava2s . co 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:MyDiffFormatter.java
License:Eclipse Distribution License
public FormatResult getFormatResult(DiffEntry ent) throws IOException { final FormatResult res = new FormatResult(); ByteArrayOutputStream buf = new ByteArrayOutputStream(); final EditList editList; final FileHeader.PatchType type; formatHeader(buf, ent);//from ww w . j av a 2s .c o m if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) { formatOldNewPaths(buf, ent); writeGitLinkDiffText(buf, ent); editList = new EditList(); type = PatchType.UNIFIED; } else if (ent.getOldId() == null || ent.getNewId() == null) { // Content not changed (e.g. only mode, pure rename) editList = new EditList(); type = PatchType.UNIFIED; } else { assertHaveRepository(); byte[] aRaw = open(OLD, ent); byte[] bRaw = open(NEW, ent); if (aRaw == BINARY || bRaw == BINARY // || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) { formatOldNewPaths(buf, ent); buf.write(encodeASCII("Binary files differ\n")); //$NON-NLS-1$ editList = new EditList(); type = PatchType.BINARY; } else { res.a = new RawText(aRaw); res.b = new RawText(bRaw); editList = diff(res.a, res.b); type = PatchType.UNIFIED; switch (ent.getChangeType()) { case RENAME: case COPY: if (!editList.isEmpty()) formatOldNewPaths(buf, ent); break; default: formatOldNewPaths(buf, ent); break; } } } res.header = new FileHeader(buf.toByteArray(), editList, type); return res; }
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./*from w ww. j a v a 2 s .co 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.github.checkstyle.parser.JgitUtils.java
License:Open Source License
/** * Generates the differences between the contents of the 2 files. * * @param baseFile/* w ww .j a va2 s . c o m*/ * The base file to examine. * @param patchFile * The patch file to examine. * @return The iterator containing the differences. * @throws IOException * if Exceptions occur while reading the file. */ public static Iterator<JgitDifference> getDifferences(File baseFile, File patchFile) throws IOException { if (diffAlgorithm == null) { diffAlgorithm = DiffAlgorithm.getAlgorithm(SupportedAlgorithm.HISTOGRAM); } final RawText baseFileRaw = new RawText(baseFile); final RawText patchFileRaw = new RawText(patchFile); return new JgitDifferenceIterator(diffAlgorithm.diff(RawTextComparator.DEFAULT, baseFileRaw, patchFileRaw), baseFileRaw, patchFileRaw); }
From source file:com.google.collide.server.shared.merge.JGitMerger.java
License:Open Source License
public static MergeResult merge(String base, String child, String parent) { // Jgit Merge org.eclipse.jgit.merge.MergeResult<RawText> jgitMergeResult = mergeAlgorithm.merge( RawTextComparator.DEFAULT, new RawText(base.getBytes()), new RawText(child.getBytes()), new RawText(parent.getBytes())); return formatMerge(jgitMergeResult); }
From source file:com.google.gerrit.server.patch.PatchListLoader.java
License:Apache License
private PatchListEntry newCommitMessage(final RawTextComparator cmp, final ObjectReader reader, final RevCommit aCommit, final RevCommit bCommit) throws IOException { StringBuilder hdr = new StringBuilder(); hdr.append("diff --git"); if (aCommit != null) { hdr.append(" a/").append(Patch.COMMIT_MSG); } else {/* w w w . j av a 2s . c om*/ hdr.append(" ").append(FileHeader.DEV_NULL); } hdr.append(" b/").append(Patch.COMMIT_MSG); hdr.append("\n"); if (aCommit != null) { hdr.append("--- a/").append(Patch.COMMIT_MSG).append("\n"); } else { hdr.append("--- ").append(FileHeader.DEV_NULL).append("\n"); } hdr.append("+++ b/").append(Patch.COMMIT_MSG).append("\n"); Text aText = aCommit != null ? Text.forCommit(reader, aCommit) : Text.EMPTY; Text bText = Text.forCommit(reader, bCommit); byte[] rawHdr = hdr.toString().getBytes("UTF-8"); RawText aRawText = new RawText(aText.getContent()); RawText bRawText = new RawText(bText.getContent()); EditList edits = new HistogramDiff().diff(cmp, aRawText, bRawText); FileHeader fh = new FileHeader(rawHdr, edits, PatchType.UNIFIED); return new PatchListEntry(fh, edits); }
From source file:com.google.gitiles.DiffServletTest.java
License:Apache License
@Test public void diffFileNoParentsText() throws Exception { String contents = "foo\ncontents\n"; RevCommit c = repo.update("master", repo.commit().add("foo", contents)); FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); req.setPathInfo("/test/+diff/" + c.name() + "^!/foo"); req.setQueryString("format=TEXT"); FakeHttpServletResponse res = new FakeHttpServletResponse(); servlet.service(req, res);/* w ww . ja v a2 s . c om*/ Patch p = parsePatch(res.getActualBody()); FileHeader f = getOnlyElement(p.getFiles()); assertEquals(ChangeType.ADD, f.getChangeType()); assertEquals(DiffEntry.DEV_NULL, f.getPath(Side.OLD)); assertEquals("foo", f.getPath(Side.NEW)); RawText rt = new RawText(contents.getBytes(UTF_8)); Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList()); assertEquals(Type.INSERT, e.getType()); assertEquals(contents, rt.getString(e.getBeginB(), e.getEndB(), false)); }
From source file:com.google.gitiles.DiffServletTest.java
License:Apache License
@Test public void diffFileOneParentText() throws Exception { String contents1 = "foo\n"; String contents2 = "foo\ncontents\n"; RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1)); RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2)); FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); req.setPathInfo("/test/+diff/" + c2.name() + "^!/foo"); req.setQueryString("format=TEXT"); FakeHttpServletResponse res = new FakeHttpServletResponse(); servlet.service(req, res);/*from w w w.ja va2 s .c o m*/ Patch p = parsePatch(res.getActualBody()); FileHeader f = getOnlyElement(p.getFiles()); assertEquals(ChangeType.MODIFY, f.getChangeType()); assertEquals("foo", f.getPath(Side.OLD)); assertEquals("foo", f.getPath(Side.NEW)); RawText rt2 = new RawText(contents2.getBytes(UTF_8)); Edit e = getOnlyElement(getOnlyElement(f.getHunks()).toEditList()); assertEquals(Type.INSERT, e.getType()); assertEquals("contents\n", rt2.getString(e.getBeginB(), e.getEndB(), false)); }
From source file:com.googlesource.gerrit.plugins.serverconfig.ServerConfigServlet.java
License:Apache License
private static String diff(File oldFile, File newFile) throws IOException { RawText oldContext = new RawText(oldFile); RawText newContext = new RawText(newFile); UnifiedDiffer differ = new UnifiedDiffer(); return differ.diff(oldContext, newContext); }