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

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

Introduction

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

Prototype

public static boolean isBinary(InputStream raw) throws IOException 

Source Link

Document

Determine heuristically whether the bytes contained in a stream represents binary (as opposed to text) content.

Usage

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   w w  w .ja  v  a 2 s  .  co 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.google.gitiles.BlobSoyData.java

License:Open Source License

public Map<String, Object> toSoyData(String path, ObjectId blobId) throws MissingObjectException, IOException {
    Map<String, Object> data = Maps.newHashMapWithExpectedSize(4);
    data.put("sha", ObjectId.toString(blobId));

    ObjectLoader loader = walk.getObjectReader().open(blobId, Constants.OBJ_BLOB);
    String content;//from  w  ww  . jav  a  2s  . co  m
    try {
        byte[] raw = loader.getCachedBytes(MAX_FILE_SIZE);
        content = !RawText.isBinary(raw) ? RawParseUtils.decode(raw) : null;
    } catch (LargeObjectException.OutOfMemory e) {
        throw e;
    } catch (LargeObjectException e) {
        content = null;
    }

    data.put("data", content);
    if (content != null) {
        data.put("lang", guessPrettifyLang(path, content));
    } else if (content == null) {
        data.put("size", Long.toString(loader.getSize()));
    }
    if (path != null && view.getRevision().getPeeledType() == OBJ_COMMIT) {
        data.put("logUrl", GitilesView.log().copyFrom(view).toUrl());
    }
    return data;
}

From source file:com.googlesource.gerrit.plugins.findowners.OwnersValidator.java

License:Apache License

@VisibleForTesting
List<CommitValidationMessage> performValidation(RevCommit c, RevWalk revWalk, String ownersFileName,
        boolean verbose) throws IOException {
    // Collect all messages from all files.
    List<CommitValidationMessage> messages = new LinkedList<>();
    // Collect all email addresses from all files and check each address only once.
    Map<String, Set<String>> email2lines = new HashMap<>();
    Map<String, ObjectId> content = getChangedOwners(c, revWalk, ownersFileName);
    for (String path : content.keySet()) {
        ObjectLoader ol = revWalk.getObjectReader().open(content.get(path));
        try (InputStream in = ol.openStream()) {
            if (RawText.isBinary(in)) {
                add(messages, path + " is a binary file", true); // OWNERS files cannot be binary
                continue;
            }//from   w  ww.jav  a2 s. c o  m
        }
        checkFile(messages, email2lines, path, ol, verbose);
    }
    checkEmails(messages, emails, email2lines, verbose);
    return messages;
}

From source file:com.googlesource.gerrit.plugins.uploadvalidator.BlockedKeywordValidator.java

License:Apache License

@VisibleForTesting
List<CommitValidationMessage> performValidation(Repository repo, RevCommit c, RevWalk revWalk,
        ImmutableCollection<Pattern> blockedKeywordPartterns, PluginConfig cfg)
        throws IOException, ExecutionException {
    List<CommitValidationMessage> messages = new LinkedList<>();
    checkCommitMessageForBlockedKeywords(blockedKeywordPartterns, messages, c.getFullMessage());
    Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c, revWalk);
    for (String path : content.keySet()) {
        ObjectLoader ol = revWalk.getObjectReader().open(content.get(path));
        try (InputStream in = ol.openStream()) {
            if (RawText.isBinary(in) || contentTypeUtil.isBlacklistedBinaryContentType(ol, path, cfg)) {
                continue;
            }//from   w  w w  .j a  v a 2 s .  c o m
        }
        checkFileForBlockedKeywords(blockedKeywordPartterns, messages, path, ol);
    }
    return messages;
}

From source file:com.googlesource.gerrit.plugins.uploadvalidator.InvalidLineEndingValidator.java

License:Apache License

@VisibleForTesting
List<CommitValidationMessage> performValidation(Repository repo, RevCommit c, RevWalk revWalk, PluginConfig cfg)
        throws IOException, ExecutionException {
    List<CommitValidationMessage> messages = new LinkedList<>();
    Map<String, ObjectId> content = CommitUtils.getChangedContent(repo, c, revWalk);
    for (String path : content.keySet()) {
        ObjectLoader ol = revWalk.getObjectReader().open(content.get(path));
        try (InputStream in = ol.openStream()) {
            if (RawText.isBinary(in) || contentTypeUtil.isBlacklistedBinaryContentType(ol, path, cfg)) {
                continue;
            }/*from  ww  w  . j  a va 2s.  co  m*/
        }
        try (InputStreamReader isr = new InputStreamReader(ol.openStream(), StandardCharsets.UTF_8)) {
            if (doesInputStreanContainCR(isr)) {
                messages.add(new CommitValidationMessage(
                        "found carriage return (CR) character in file: " + path, true));
            }
        }
    }
    return messages;
}

From source file:com.googlesource.gerrit.plugins.xdocs.XDocLoader.java

License:Apache License

private String getHtml(String formatterName, StringFormatter f, Repository repo, ObjectLoader loader,
        Project.NameKey project, String path, ObjectId revId)
        throws MethodNotAllowedException, IOException, GitAPIException {
    byte[] bytes = loader.getBytes(Integer.MAX_VALUE);
    boolean isBinary = RawText.isBinary(bytes);
    if (formatterName.equals(Formatters.RAW_FORMATTER) && isBinary) {
        throw new MethodNotAllowedException();
    }//  ww w .  j  ava2s  .  co m
    String raw = new String(bytes, UTF_8);
    String abbrRevId = getAbbrRevId(repo, revId);
    if (!isBinary) {
        raw = replaceMacros(repo, project, revId, abbrRevId, raw);
    }
    return f.format(project.get(), path, revId.getName(), abbrRevId, getFormatterConfig(formatterName), raw);
}

From source file:com.madgag.agit.BlobViewFragment.java

License:Open Source License

@Override
public Loader<BlobView> onCreateLoader(int id, Bundle b) {
    return new AsyncLoader<BlobView>(getActivity()) {
        public BlobView loadInBackground() {
            Bundle args = getArguments();
            try {
                Repository repo = new FileRepository(args.getString(GITDIR));
                ObjectId revision = repo.resolve(args.getString(UNTIL_REVS));
                RevWalk revWalk = new RevWalk(repo);
                RevCommit commit = revWalk.parseCommit(revision);
                TreeWalk treeWalk = TreeWalk.forPath(repo, args.getString(PATH), commit.getTree());
                ObjectId blobId = treeWalk.getObjectId(0);

                ObjectLoader objectLoader = revWalk.getObjectReader().open(blobId, Constants.OBJ_BLOB);
                ObjectStream binaryTestStream = objectLoader.openStream();
                boolean blobIsBinary = RawText.isBinary(binaryTestStream);
                binaryTestStream.close();
                Log.d(TAG, "blobIsBinary=" + blobIsBinary);
                return blobIsBinary ? new BinaryBlobView(objectLoader, treeWalk.getNameString())
                        : new TextBlobView(objectLoader);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }/*from ww  w.j  a v  a2s. co m*/
        }

    };
}

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 v  a2s .c  o 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:com.tasktop.c2c.server.scm.service.GitBrowseUtil.java

License:Open Source License

public static Blob getBlob(Repository r, String revision, String path)
        throws IOException, EntityNotFoundException {

    if (path.startsWith("/")) {
        path = path.substring(1);/* ww  w  . j  a v a  2s .  co m*/
    }

    String id = resolve(r, r.resolve(revision), path);
    if (id == null) {
        throw new EntityNotFoundException();
    }
    ObjectId objectId = ObjectId.fromString(id);

    ObjectLoader loader = r.getObjectDatabase().open(objectId, Constants.OBJ_BLOB);

    Blob b = new Blob(id);

    if (loader.isLarge()) {
        b.setLarge(true);
        InputStream is = null;
        IOException ioex = null;
        try {
            is = loader.openStream();
            b.setBinary(RawText.isBinary(is));
        } catch (IOException ex) {
            ioex = ex;
        } finally {
            if (is != null) {
                is.close();
            }
            if (ioex != null) {
                throw ioex;
            }
        }

    } else {
        byte[] raw = loader.getBytes();

        boolean binary = RawText.isBinary(raw);

        if (binary) {
            b.setBinary(true);
            b.setLines(Collections.<String>emptyList());
        } else {

            RawText rt = new RawText(raw);
            List<String> lines = new ArrayList<String>(rt.size());

            for (int i = 0; i < rt.size(); i++) {
                lines.add(rt.getString(i));
            }

            b.setLines(lines);
        }
    }

    return b;
}

From source file:de.fau.osr.util.FlatSource.java

License:Open Source License

public static FlatSource flatten(byte[] input) {
    // file is binary, ignore all contents
    if (RawText.isBinary(input))
        return new FlatSource(new byte[0], new IntList());

    IntList realLines = new IntList();
    ByteBuffer to = ByteBuffer.allocate(input.length * 2), from = ByteBuffer.wrap(input);
    boolean atLineStart = true, lastWord = false, lastSpace = false;
    while (from.hasRemaining()) {
        if (atLineStart) {
            realLines.add(to.position());
        }//from   w  ww. ja va 2  s  . co  m
        byte cur = from.get();
        if (cur == '\n') {
            atLineStart = true;
            to.put(cur);
        } else if (cur == ' ' || cur == '\t') {
            if (!atLineStart && !lastSpace)
                to.put((byte) '\n');
            to.put(cur);
            lastSpace = true;
            lastWord = false;
            atLineStart = false;
        } else {
            if (!atLineStart && !lastWord)
                to.put((byte) '\n');
            to.put(cur);
            lastSpace = false;
            lastWord = true;
            atLineStart = false;
        }
    }
    byte[] out = new byte[to.position()];
    to.position(0);
    to.get(out);
    return new FlatSource(out, realLines);//new FlatSource(new byte[])
}