/** * Push a candidate blob onto the generator's traversal stack. * <p> * Candidates should be pushed in history order from oldest-to-newest. * Applications should push the starting commit first, then the index * revision (if the index is interesting), and finally the working tree copy * (if the working tree is interesting). * * @param description * description of the blob revision, such as "Working Tree". * @param contents * contents of the file. * @return {@code this} * @throws java.io.IOException * the repository cannot be read. */ public BlameGenerator push(String description, byte[] contents) throws IOException { return push(description, new RawText(contents)); }
/** * Main method * * @param args * two filenames specifying the contents to be diffed */ public static void main(String[] args) { if (args.length != 2) { System.err.println(JGitText.get().need2Arguments); System.exit(1); } try { RawText a = new RawText(new java.io.File(args[0])); RawText b = new RawText(new java.io.File(args[1])); EditList r = INSTANCE.diff(RawTextComparator.DEFAULT, a, b); System.out.println(r.toString()); } catch (Exception e) { e.printStackTrace(); } } }
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$ } }
private RawText getRawText(File inTree) throws IOException, FileNotFoundException { RawText rawText; WorkingTreeOptions workingTreeOptions = getRepository().getConfig() .get(WorkingTreeOptions.KEY); AutoCRLF autoCRLF = workingTreeOptions.getAutoCRLF(); switch (autoCRLF) { case FALSE: case INPUT: // Git used the repo format on checkout, but other tools // may change the format to CRLF. We ignore that here. rawText = new RawText(inTree); break; case TRUE: try (AutoLFInputStream in = new AutoLFInputStream( new FileInputStream(inTree), true)) { // Canonicalization should lead to same or shorter length // (CRLF to LF), so the file size on disk is an upper size bound rawText = new RawText(toByteArray(in, (int) inTree.length())); } break; default: throw new IllegalArgumentException( "Unknown autocrlf option " + autoCRLF); //$NON-NLS-1$ } return rawText; }
/** * Returns a git-style diff between the two unix strings. * * Output has no trailing newlines. * * 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); }
void loadText(ObjectReader reader) throws IOException { ObjectLoader ldr = LfsFactory.getInstance().applySmudgeFilter(sourceRepository, reader.open(sourceBlob, Constants.OBJ_BLOB), LfsFactory.getAttributesForPath(sourceRepository, sourcePath.getPath(), sourceCommit) .get(Constants.ATTR_DIFF)); sourceText = new RawText(ldr.getCachedBytes(Integer.MAX_VALUE)); }
private RawText getRawText(ObjectId id, Attributes attributes) throws IOException, BinaryBlobException { if (id.equals(ObjectId.zeroId())) return new RawText(new byte[] {}); ObjectLoader loader = LfsFactory.getInstance().applySmudgeFilter( getRepository(), reader.open(id, OBJ_BLOB), attributes.get(Constants.ATTR_MERGE)); int threshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD; return RawText.load(loader, threshold); }
/** * Returns a RawText representation of a file in the passed repository. Used in creating diffs. * * @param repo - The repository to pull the change. * @param path - The path to the file. * @return - RawText representation of the file. * @throws IOException - failed. */ public RawText raw(final Repository repo, final String path) throws IOException { val file = new File(repo.getWorkTree().getAbsolutePath() + "/" + path); return new RawText(FileUtils.readFileToByteArray(file)); }
/** * Returns the RawText of a file specified by its path. * * @param path - File path. * @return - RawText. * @throws IOException - failed. */ public RawText rawText(final String path) throws IOException { val file = new File(git.getRepository().getWorkTree().getAbsolutePath() + "/" + path); return new RawText(FileUtils.readFileToByteArray(file)); }
resultPath); c.sourceBlob = id.toObjectId(); c.sourceText = new RawText(ldr.getCachedBytes(Integer.MAX_VALUE)); c.regionList = new Region(0, 0, c.sourceText.size()); remaining = c.sourceText.size();
private static 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$ } }
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$ } }
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$ } }
void loadText(ObjectReader reader) throws IOException { ObjectLoader ldr = LfsFactory.getInstance().applySmudgeFilter(sourceRepository, reader.open(sourceBlob, Constants.OBJ_BLOB), LfsFactory.getAttributesForPath(sourceRepository, sourcePath.getPath(), sourceCommit) .get(Constants.ATTR_DIFF)); sourceText = new RawText(ldr.getCachedBytes(Integer.MAX_VALUE)); }
private RawText getRawText(ObjectId id, Attributes attributes) throws IOException, BinaryBlobException { if (id.equals(ObjectId.zeroId())) return new RawText(new byte[] {}); ObjectLoader loader = LfsFactory.getInstance().applySmudgeFilter( getRepository(), reader.open(id, OBJ_BLOB), attributes.get(Constants.ATTR_MERGE)); int threshold = PackConfig.DEFAULT_BIG_FILE_THRESHOLD; return RawText.load(loader, threshold); }