Example usage for org.eclipse.jgit.lib Constants OBJ_BLOB

List of usage examples for org.eclipse.jgit.lib Constants OBJ_BLOB

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants OBJ_BLOB.

Prototype

int OBJ_BLOB

To view the source code for org.eclipse.jgit.lib Constants OBJ_BLOB.

Click Source Link

Document

In-pack object type: blob.

Usage

From source file:org.eclipse.orion.server.git.objects.Commit.java

License:Open Source License

/**
 * Return body of the commit//w  w  w. j ava  2s  . co m
 *
 * @return body of the commit as an Object Stream
 * @throws IOException when reading the object failed
 */
public ObjectStream toObjectStream() throws IOException {
    final TreeWalk w = TreeWalk.forPath(db, pattern, revCommit.getTree());
    if (w == null) {
        return null;
    }
    ObjectId blobId = w.getObjectId(0);
    return db.open(blobId, Constants.OBJ_BLOB).openStream();
}

From source file:org.eclipse.orion.server.git.objects.Index.java

License:Open Source License

public ObjectStream toObjectStream() throws IOException {
    DirCache cache = db.readDirCache();/*w w w  .ja  v a2 s .  c om*/
    DirCacheEntry entry = cache.getEntry(pattern);
    if (entry == null) {
        return null;
    }
    ObjectId blobId = entry.getObjectId();
    return db.open(blobId, Constants.OBJ_BLOB).openStream();
}

From source file:org.eclipse.orion.server.git.servlets.GitCommitHandlerV1.java

License:Open Source License

private boolean handleGetCommitBody(HttpServletRequest request, HttpServletResponse response, Repository db,
        String ref, String pattern) throws AmbiguousObjectException, IOException, ServletException {
    ObjectId refId = db.resolve(ref);//  w  ww . ja  v a 2s.co m
    if (refId == null) {
        String msg = NLS.bind("Failed to generate commit log for ref {0}", ref);
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
    }

    RevWalk walk = new RevWalk(db);
    walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(Collections.singleton(pattern)),
            TreeFilter.ANY_DIFF));
    RevCommit commit = walk.parseCommit(refId);
    final TreeWalk w = TreeWalk.forPath(db, pattern, commit.getTree());
    if (w == null) {
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, null, null));
    }

    ObjectId blobId = w.getObjectId(0);
    ObjectStream stream = db.open(blobId, Constants.OBJ_BLOB).openStream();
    IOUtilities.pipe(stream, response.getOutputStream(), true, false);

    return true;
}

From source file:org.eclipse.orion.server.git.servlets.GitIndexHandlerV1.java

License:Open Source License

private boolean handleGet(HttpServletRequest request, HttpServletResponse response, Repository db,
        String pattern) throws CoreException, IOException, ServletException {
    DirCache cache = db.readDirCache();/*from w ww . j a  v  a2 s . c om*/
    DirCacheEntry entry = cache.getEntry(pattern);
    if (entry == null) {
        String msg = NLS.bind("{0} not found in index", pattern); //$NON-NLS-1$
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.OK, HttpServletResponse.SC_NOT_FOUND, msg, null));
    }
    ObjectId blobId = entry.getObjectId();
    ObjectStream stream = db.open(blobId, Constants.OBJ_BLOB).openStream();
    IOUtilities.pipe(stream, response.getOutputStream(), true, false);
    return true;
}

From source file:org.kercoin.magrit.core.dao.BuildDAOImpl.java

License:Open Source License

private ObjectId writeBlob(ObjectInserter db, byte[] bytes) throws IOException {
    return db.insert(Constants.OBJ_BLOB, bytes);
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static InputStream resolveInputStream(final Git git, final String treeRef, final String path) {
    checkNotNull("git", git);
    checkNotEmpty("treeRef", treeRef);
    checkNotEmpty("path", path);

    final String gitPath = fixPath(path);

    RevWalk rw = null;/*w  w  w  .  j  a  v  a  2  s.com*/
    TreeWalk tw = null;
    try {
        final ObjectId tree = git.getRepository().resolve(treeRef + "^{tree}");
        rw = new RevWalk(git.getRepository());
        tw = new TreeWalk(git.getRepository());
        tw.setFilter(createFromStrings(singleton(gitPath)));
        tw.reset(tree);
        while (tw.next()) {
            if (tw.isSubtree() && !gitPath.equals(tw.getPathString())) {
                tw.enterSubtree();
                continue;
            }
            final ObjectId entid = tw.getObjectId(0);
            final FileMode entmode = tw.getFileMode(0);
            final RevObject ro = rw.lookupAny(entid, entmode.getObjectType());
            rw.parseBody(ro);
            final ObjectLoader ldr = git.getRepository().open(ro.getId(), Constants.OBJ_BLOB);
            return ldr.openStream();
        }
    } catch (final Throwable t) {
        throw new NoSuchFileException("Can't find '" + gitPath + "' in tree '" + treeRef + "'");
    } finally {
        if (rw != null) {
            rw.dispose();
        }
        if (tw != null) {
            tw.release();
        }
    }
    throw new NoSuchFileException("");
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

/**
 * Creates an in-memory index of the issue change.
 *///from  www  .j a  v  a  2s .  c o m
private static DirCache createTemporaryIndex(final Git git, final ObjectId headId,
        final Map<String, File> content) {

    final DirCache inCoreIndex = DirCache.newInCore();
    final DirCacheBuilder dcBuilder = inCoreIndex.builder();
    final ObjectInserter inserter = git.getRepository().newObjectInserter();
    boolean hadFile = false;
    final Set<String> paths = new HashSet<String>(content.size());

    try {
        for (final Map.Entry<String, File> pathAndContent : content.entrySet()) {
            final String gPath = fixPath(pathAndContent.getKey());
            paths.add(gPath);
            if (pathAndContent.getValue() != null) {
                hadFile = true;
                final DirCacheEntry dcEntry = new DirCacheEntry(gPath);
                dcEntry.setLength(pathAndContent.getValue().length());
                dcEntry.setLastModified(pathAndContent.getValue().lastModified());
                dcEntry.setFileMode(REGULAR_FILE);

                final InputStream inputStream = new FileInputStream(pathAndContent.getValue());
                try {
                    final ObjectId objectId = inserter.insert(Constants.OBJ_BLOB,
                            pathAndContent.getValue().length(), inputStream);
                    dcEntry.setObjectId(objectId);
                } finally {
                    inputStream.close();
                }

                dcBuilder.add(dcEntry);
            }

            if (!hadFile) {
                final DirCacheEditor editor = inCoreIndex.editor();
                editor.add(new DirCacheEditor.DeleteTree(gPath));
                editor.finish();
            }
        }

        if (headId != null) {
            final TreeWalk treeWalk = new TreeWalk(git.getRepository());
            final int hIdx = treeWalk.addTree(new RevWalk(git.getRepository()).parseTree(headId));
            treeWalk.setRecursive(true);

            while (treeWalk.next()) {
                final String walkPath = treeWalk.getPathString();
                final CanonicalTreeParser hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);

                if (!paths.contains(walkPath)) {
                    // add entries from HEAD for all other paths
                    // create a new DirCacheEntry with data retrieved from HEAD
                    final DirCacheEntry dcEntry = new DirCacheEntry(walkPath);
                    dcEntry.setObjectId(hTree.getEntryObjectId());
                    dcEntry.setFileMode(hTree.getEntryFileMode());

                    // add to temporary in-core index
                    dcBuilder.add(dcEntry);
                }
            }
            treeWalk.release();
        }

        dcBuilder.finish();

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        inserter.release();
    }

    return inCoreIndex;
}

From source file:org.kuali.student.git.cleaner.RepositoryBlobRewriter.java

License:Educational Community License

@Override
protected boolean processCommitTree(RevCommit commit, GitTreeData tree)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {

    boolean recreateCommit = false;

    for (Map.Entry<ObjectId, String> entry : this.blobIdToReplacementContentMap.entrySet()) {

        ObjectId blobId = entry.getKey();

        List<String> currentPaths = GitRepositoryUtils.findPathsForBlobInCommit(getRepo(), commit.getId(),
                blobId);/* ww w. j  av a 2  s  . co m*/

        if (currentPaths.size() > 0) {

            recreateCommit = true;

            ObjectId newBlobId = inserter.insert(Constants.OBJ_BLOB, entry.getValue().getBytes());

            for (String path : currentPaths) {

                tree.addBlob(path, newBlobId);
            }

            inserter.release();
        }

    }

    return recreateCommit;
}

From source file:org.kuali.student.git.cleaner.RepositoryRemoveMPXRewriter.java

License:Educational Community License

@Override
public void validateArgs(List<String> args) throws Exception {

    if (args.size() != 2 && args.size() != 4) {
        log.error(//from w w w .j  a v  a2 s.c  om
                "USAGE: <source git repository meta directory> <blob content replacement>  [<branchRefSpec> <git command path>]");
        log.error("\t<git repo meta directory> : the path to the meta directory of the source git repository");
        log.error("\t<blob content replacement> : content to replace the matched blob with");
        log.error("\t<branchRefSpec> : git refspec from which to source the graph to be rewritten");
        log.error("\t<git command path> : the path to a native git ");
        throw new IllegalArgumentException("invalid arguments");
    }

    setRepo(GitRepositoryUtils.buildFileRepository(new File(args.get(0)).getAbsoluteFile(), false));

    String blobReplacmentContent = args.get(1);

    replacementContentBlobId = getRepo().newObjectInserter().insert(Constants.OBJ_BLOB,
            blobReplacmentContent.getBytes());

    if (args.size() >= 3)
        setBranchRefSpec(args.get(2).trim());

    if (args.size() == 4)
        setExternalGitCommandPath(args.get(3).trim());
}

From source file:org.kuali.student.git.cleaner.RewriteFusionPluginData.java

License:Educational Community License

@Override
protected boolean processCommitTree(RevCommit commit, GitTreeData tree)
        throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException {

    ObjectId fusionPluginDataBlobId = tree.find(getRepo(), "fusion-maven-plugin.dat");

    boolean changesToBeCommitted = false;

    if (fusionPluginDataBlobId != null) {

        if (commit.getFullMessage().contains("@71661"))
            log.info("found target commit");

        ObjectLoader loader = getRepo().newObjectReader().open(fusionPluginDataBlobId, Constants.OBJ_BLOB);

        // rewrite the data here
        List<ExternalModuleInfo> fusionData = ExternalModuleUtils
                .extractFusionMavenPluginData(loader.openStream());

        for (ExternalModuleInfo fusion : fusionData) {

            ObjectId commitId = fusion.getBranchHeadId();

            if (commitId == null) {
                log.warn("commit Id: " + commit.getId().name() + " is missing branch head for module: "
                        + fusion.getModuleName() + " branch: " + fusion.getBranchPath());
                continue;
            }// w  w w .j  a  va 2  s  .co m

            if (commitId.name().startsWith("8b608b677a5090080014374d11c0dba909"))
                log.info("target commit: " + commit.getId()
                        + " refers to external: 8b608b677a5090080014374d11c0dba909");

            // check where this originates from
            ObjectId newCommitId = this.translationService.translateObjectId(commitId);

            // will exist if the newCommitId from a previous rewite has been rewritted during the current rewrite
            ObjectId currentlyChangedId = this.originalCommitIdToNewCommitIdMap.get(newCommitId);

            if (currentlyChangedId != null)
                newCommitId = currentlyChangedId;

            if (newCommitId != null && !newCommitId.equals(commitId)) {

                fusion.setBranchHeadId(newCommitId);

                changesToBeCommitted = true;

                if (currentlyChangedId != null && !super.processedCommits.contains(newCommitId)) {
                    log.warn("repo is missing a commit for " + newCommitId);
                }
            } else {
                // make sure that the commitId is still valid if its been changed

                if (super.originalCommitIdToNewCommitIdMap.keySet().contains(commitId)
                        && !super.processedCommits.contains(commitId)) {
                    log.warn("repo is missing a commit for " + commitId);
                }

            }

        }

        if (changesToBeCommitted) {

            // save it into the tree
            String updatedFusionData = ExternalModuleUtils.createFusionMavenPluginDataFileString(fusionData);

            ObjectId updatedBlobId = inserter.insert(Constants.OBJ_BLOB, updatedFusionData.getBytes());

            tree.addBlob("fusion-maven-plugin.dat", updatedBlobId);

            return true;
        } else
            return false;

    } else
        return false;
}