Example usage for org.eclipse.jgit.lib ObjectId name

List of usage examples for org.eclipse.jgit.lib ObjectId name

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ObjectId name.

Prototype

public final String name() 

Source Link

Document

name.

Usage

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public boolean exists(String arg0) {
    try {//  w ww.  j a  v  a2  s  . c o  m
        AnyObjectId id = repository.resolve(Constants.HEAD);
        RevCommit commit = new RevWalk(repository).parseCommit(id);
        LOGGER.debug("Looking up file {} in HEAD revision", arg0);
        TreeWalk treeWalk = TreeWalk.forPath(repository, arg0, new AnyObjectId[] { commit.getTree() });
        if (treeWalk == null) {
            return false;
        }
        ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1);
        LOGGER.debug("File {} received commit id {} at commit", arg0, objectId.name());
        return !objectId.equals(ObjectId.zeroId());
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.openengsb.connector.git.internal.GitServiceImpl.java

License:Apache License

@Override
public boolean exists(String arg0, CommitRef arg1) {
    try {//from w  w  w.  j  a  v a  2s .  c  o m
        AnyObjectId id = repository.resolve(arg1.getStringRepresentation());
        RevCommit commit = new RevWalk(repository).parseCommit(id);
        LOGGER.debug("Looking up file {} in revision {}", arg0, arg1.getStringRepresentation());
        TreeWalk treeWalk = TreeWalk.forPath(repository, arg0, new AnyObjectId[] { commit.getTree() });
        if (treeWalk == null) {
            return false;
        }
        ObjectId objectId = treeWalk.getObjectId(treeWalk.getTreeCount() - 1);
        LOGGER.debug("File {} received commit id {} at commit", arg0, objectId.name());
        return !objectId.equals(ObjectId.zeroId());
    } catch (Exception e) {
        throw new ScmException(e);
    }
}

From source file:org.test.RewriteGitHistory.java

License:Apache License

/**
 * In this case the svn repository has been cloned but there are no seperate
 * branches. Master points at the root with all the directories contained
 * beneath it./*  w ww . j a v  a2s.  c o  m*/
 * 
 * What we want to accomplish is to create new commits for each 'branch'
 * that will contain only their contents.
 * 
 * We do not erase or modify the original commits.
 * 
 * We locate all of the commits that contain the includePath.
 * 
 * Then we locate the first commit that included it. We create new commits for each of the existing commits
 * but we modify the tree to only contain the matched tree of each commit.
 * @throws NoHeadException 
 * @throws WrongRepositoryStateException 
 * @throws ConcurrentRefUpdateException 
 * @throws NoMessageException 
 * @throws NoFilepatternException 
 * 
 * 
 */
public void extractBranch(String sourceBranchName, String newBranchName, String includePath)
        throws JGitInternalException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
        AmbiguousObjectException, IOException, NoHeadException, NoMessageException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, NoFilepatternException {

    Ref branchId = git.checkout().setName(sourceBranchName).call();

    RevWalk rwalk = new RevWalk(repository);

    RevCommit tip = rwalk.lookupCommit(branchId.getObjectId());

    rwalk.markStart(tip);

    rwalk.setTreeFilter(PathFilter.create(includePath));

    Iterator<RevCommit> commitIter = rwalk.iterator();

    List<CommitBuilder> newBranchCommits = new ArrayList<CommitBuilder>();

    RevCommit branchPoint = null;

    while (commitIter.hasNext()) {

        RevCommit commit = (RevCommit) commitIter.next();

        printCommit(commit);

        final TreeWalk walk = new TreeWalk(repository);
        walk.setRecursive(false); //only look at the top level entries in the commit tree
        walk.addTree(commit.getTree().getId());

        // match all of the paths on this commit
        List<ObjectId> matchedPaths = new ArrayList<ObjectId>();

        while (walk.next()) {
            final FileMode mode = walk.getFileMode(0);
            //            if (mode == FileMode.TREE)
            //               log.info("0");
            //            log.info(mode.toString());
            //            log.info(" ");

            if (mode == FileMode.TREE) {

                String path = walk.getPathString();

                if (path.startsWith(includePath)) {
                    log.info("matched " + Constants.typeString(mode.getObjectType()) + " sha1: "
                            + walk.getObjectId(0).name() + " path: " + walk.getPathString());

                    matchedPaths.add(walk.getObjectId(0));

                    branchPoint = commit;
                } else {
                    log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: "
                            + walk.getObjectId(0).name() + " path: " + walk.getPathString());
                }
            } else {
                log.debug("excluded " + Constants.typeString(mode.getObjectType()) + " sha1: "
                        + walk.getObjectId(0).name() + " path: " + walk.getPathString());
            }

        }

        if (matchedPaths.size() > 1) {
            // create a tree to hold the tree
            log.warn("multiple matches found for path = " + includePath + " commit = " + commit.getId().name());
        } else if (matchedPaths.size() == 1) {

            PersonIdent author = commitFilter.filterPersonIdent(commit.getAuthorIdent());

            PersonIdent committer = commitFilter.filterPersonIdent(commit.getAuthorIdent());

            CommitBuilder commitBuilder = new CommitBuilder();

            // set parent id later once we have the full path
            //            commitBuilder.setParentId(newBaseId);
            commitBuilder.setTreeId(matchedPaths.get(0));
            commitBuilder.setAuthor(author);
            commitBuilder.setCommitter(committer);
            commitBuilder.setEncoding(commit.getEncoding());
            commitBuilder.setMessage(commitFilter.filterCommitMessage(commit.getFullMessage()));

            newBranchCommits.add(0, commitBuilder);
        } else {
            // do nothing
        }

    }

    // done with the commits.
    ObjectId parent = branchPoint;

    if (branchPoint.getParentCount() > 0)
        parent = branchPoint.getParent(0);

    Ref branch = git.checkout().setName(newBranchName).setCreateBranch(true).setStartPoint(parent.name())
            .call();

    // first step is that we need to delete all of the files that do not match the pattern
    RmCommand rmCommand = new RmCommand(repository);

    File workTree = git.getRepository().getWorkTree();

    // only get the top level files and directories

    File[] topLevelFiles = workTree.listFiles();

    for (File file : topLevelFiles) {

        if (!file.getName().startsWith(includePath)) {
            log.info("removing file = " + file.getAbsolutePath());
            rmCommand.addFilepattern(file.getAbsolutePath());
        }
    }

    rmCommand.call();

    RevCommit ref = git.commit().setAuthor(selfCommitIdent).setCommitter(selfCommitIdent)
            .setMessage("Delete uneeded content from newly extracted branch '" + newBranchName + "'").call();

    ObjectId parentId = ref.getId();

    for (CommitBuilder commitBuilder : newBranchCommits) {

        commitBuilder.setParentId(parentId);

        parentId = executeCommit(commitBuilder);

    }

}

From source file:org.uberfire.java.nio.fs.jgit.JGitSubdirectoryCloneTest.java

License:Apache License

private void updateBranch(final String targetBranchName, final org.eclipse.jgit.api.Git git,
        final ObjectId commitId) throws Exception {
    git.branchCreate().setName(targetBranchName).setStartPoint(commitId.name()).setForce(true).call();
}

From source file:svnserver.repository.git.cache.CacheRevision.java

License:GNU General Public License

private static Kryo createKryo() {
    final Kryo kryo = new Kryo();
    kryo.register(ObjectId.class, new Serializer<ObjectId>() {

        @Override/*  ww w.ja  v a2s . com*/
        public void write(@NotNull Kryo kryo, @NotNull Output output, @Nullable ObjectId object) {
            output.writeString(object != null ? object.name() : null);
        }

        @Override
        public ObjectId read(Kryo kryo, Input input, Class<ObjectId> type) {
            final String id = input.readString();
            return id != null ? ObjectId.fromString(id) : null;
        }
    });
    return kryo;
}

From source file:svnserver.repository.git.GitRepository.java

License:GNU General Public License

@NotNull
public GitRevision getRevision(@NotNull ObjectId revisionId) throws SVNException {
    lock.readLock().lock();//from  w  ww.  jav  a 2  s.  com
    try {
        final GitRevision revision = revisionByHash.get(revisionId);
        if (revision == null) {
            throw new SVNException(SVNErrorMessage.create(SVNErrorCode.FS_NO_SUCH_REVISION,
                    "No such revision " + revisionId.name()));
        }
        return revision;
    } finally {
        lock.readLock().unlock();
    }
}

From source file:svnserver.repository.git.push.GitPushNative.java

License:GNU General Public License

@Override
public boolean push(@NotNull Repository repository, @NotNull ObjectId commitId, @NotNull String branch,
        @NotNull User userInfo) throws SVNException, IOException {
    try {/*from ww  w.  j a  va 2s  . c  om*/
        repository.getDirectory();
        final ProcessBuilder processBuilder = new ProcessBuilder("git", "push", "--porcelain", "--quiet", ".",
                commitId.name() + ":" + branch).directory(repository.getDirectory()).redirectErrorStream(true);
        processBuilder.environment().put("LANG", "en_US.utf8");
        userInfo.updateEnvironment(processBuilder.environment());
        context.sure(UserDB.class).updateEnvironment(processBuilder.environment(), userInfo);
        final Process process = processBuilder.start();
        final StringBuilder resultBuilder = new StringBuilder();
        final StringBuilder hookBuilder = new StringBuilder();
        try (final BufferedReader stdout = new BufferedReader(
                new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
            while (true) {
                final String line = stdout.readLine();
                if (line == null) {
                    break;
                }
                if (line.startsWith(HOOK_MESSAGE_PREFIX)) {
                    if (hookBuilder.length() > 0)
                        hookBuilder.append('\n');
                    hookBuilder.append(line.substring(HOOK_MESSAGE_PREFIX.length() + 1));
                }
                if (line.startsWith(SYSTEM_MESSAGE_PREFIX)) {
                    // System message like:
                    // !   2d1ed4dcc45bef07f6dfffabe7d3ff53aa147705:refs/heads/local   [remote rejected] (pre-receive hook declined)
                    // !   75cad4dcb5f6982a1f2df073157f3aa2083ae272:refs/heads/local   [rejected] (non-fast-forward)
                    if (resultBuilder.length() > 0)
                        resultBuilder.append('\n');
                    resultBuilder.append(line.substring(SYSTEM_MESSAGE_PREFIX.length() + 1));
                }
            }
        }
        int exitCode = process.waitFor();
        if (exitCode == 0) {
            return true;
        }
        final String resultMessage = resultBuilder.toString();
        if (resultMessage.contains("non-fast-forward")) {
            return false;
        }
        if (resultMessage.contains("hook")) {
            final String hookMessage = hookBuilder.toString();
            log.warn("Push rejected by hook:\n{}", hookMessage);
            throw new SVNException(SVNErrorMessage.create(SVNErrorCode.REPOS_HOOK_FAILURE,
                    "Commit blocked by hook with output:\n" + hookMessage));
        }
        log.error("Unknown git push result:\n{}", resultMessage);
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, resultMessage));
    } catch (InterruptedException e) {
        e.printStackTrace();
        throw new SVNException(SVNErrorMessage.create(SVNErrorCode.IO_WRITE_ERROR, e));
    }
}