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

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

Introduction

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

Prototype

@SuppressWarnings("nls")
@Override
public String toString() 

Source Link

Usage

From source file:jbyoshi.gitupdate.processor.FastForward.java

License:Apache License

private static boolean tryFastForward(Repository repo, Ref ref, Ref target, Report report)
        throws GitAPIException, IOException {
    if (ref == null || target == null) {
        return false;
    }/*from w ww  . j a v  a2s .  co m*/
    target = repo.peel(target);
    if (!ref.equals(repo.getRef(Constants.HEAD).getTarget())) {
        try (RevWalk revWalk = new RevWalk(repo)) {
            ObjectId targetId = target.getPeeledObjectId();
            if (targetId == null) {
                targetId = target.getObjectId();
            }

            RevCommit targetCommit = revWalk.lookupCommit(targetId);
            ObjectId sourceId = ref.getObjectId();
            RevCommit sourceCommit = revWalk.lookupCommit(sourceId);
            if (revWalk.isMergedInto(sourceCommit, targetCommit)) {
                RefUpdate refUpdate = repo.updateRef(ref.getName());
                refUpdate.setNewObjectId(targetCommit);
                refUpdate.setRefLogMessage("Fast forward", false);
                refUpdate.setExpectedOldObjectId(sourceId);
                Result rc = refUpdate.update();
                switch (rc) {
                case NEW:
                case FAST_FORWARD:
                    report.newChild(ref.getName() + " -> " + target.getName()).modified();
                    return true;
                case REJECTED:
                case LOCK_FAILURE:
                    report.newErrorChild(new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD,
                            refUpdate.getRef(), rc));
                    break;
                case NO_CHANGE:
                    break;
                default:
                    report.newErrorChild(new JGitInternalException(MessageFormat
                            .format(JGitText.get().updatingRefFailed, ref.getName(), targetId.toString(), rc)));
                    break;
                }
            }
            return false;
        }
    }
    try {
        MergeResult result = Git.wrap(repo).merge().setFastForward(MergeCommand.FastForwardMode.FF_ONLY)
                .include(target.getTarget()).call();
        if (result.getMergeStatus() == MergeResult.MergeStatus.ALREADY_UP_TO_DATE) {
            // Ignore
        } else if (result.getMergeStatus() == MergeResult.MergeStatus.FAST_FORWARD) {
            report.newChild("Fast-forwarded " + ref.getName() + " to " + target.getName()).modified();
            return true;
        } else {
            report.newChild("Fast-forward failed: status " + result.getMergeStatus()).error();
        }
    } catch (NoHeadException e) {
        // Ignore
    }
    return false;
}

From source file:org.apache.maven.scm.provider.git.jgit.command.info.JGitInfoCommand.java

License:Apache License

@Override
protected ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fileSet,
        CommandParameters parameters) throws ScmException {
    Git git = null;//  w ww  .j a v a 2  s  .co m
    try {
        File basedir = fileSet.getBasedir();

        git = Git.open(basedir);

        ObjectId objectId = git.getRepository().resolve("HEAD");

        InfoItem infoItem = new InfoItem();
        infoItem.setRevision(StringUtils.trim(objectId.name()));
        infoItem.setURL(basedir.getPath());

        return new InfoScmResult(Collections.singletonList(infoItem),
                new ScmResult("JGit.resolve(HEAD)", "", objectId.toString(), true));
    } catch (Exception e) {
        throw new ScmException("JGit resolve failure!", e);
    } finally {
        JGitUtils.closeRepo(git);
    }
}

From source file:org.jfrog.bamboo.release.scm.git.ResetCommand.java

License:Eclipse Distribution License

/**
 * Executes the {@code Reset} command. Each instance of this class should only be used for one invocation of the
 * command. Don't call this method twice on an instance.
 *
 * @return the Ref after reset//w  w  w.java 2 s .  c  o m
 */
@Override
public Ref call() throws GitAPIException {
    checkCallable();

    Ref r;
    RevCommit commit;

    try {
        boolean merging = false;
        if (repo.getRepositoryState().equals(RepositoryState.MERGING)
                || repo.getRepositoryState().equals(RepositoryState.MERGING_RESOLVED)) {
            merging = true;
        }

        // resolve the ref to a commit
        final ObjectId commitId;
        try {
            commitId = repo.resolve(ref);
        } catch (IOException e) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotRead, ref), e);
        }
        RevWalk rw = new RevWalk(repo);
        try {
            commit = rw.parseCommit(commitId);
        } catch (IOException e) {
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().cannotReadCommit, commitId.toString()), e);
        } finally {
            rw.release();
        }

        // write the ref
        final RefUpdate ru = repo.updateRef(Constants.HEAD);
        ru.setNewObjectId(commitId);

        String refName = Repository.shortenRefName(ref);
        String message = "reset --" //$NON-NLS-1$
                + mode.toString().toLowerCase() + " " + refName; //$NON-NLS-1$
        ru.setRefLogMessage(message, false);
        if (ru.forceUpdate() == RefUpdate.Result.LOCK_FAILURE) {
            throw new JGitInternalException(MessageFormat.format(JGitText.get().cannotLock, ru.getName()));
        }

        switch (mode) {
        case HARD:
            checkoutIndex(commit);
            break;
        case MIXED:
            resetIndex(commit);
            break;
        case SOFT: // do nothing, only the ref was changed
            break;
        }

        if (mode != ResetType.SOFT && merging) {
            resetMerge();
        }

        setCallable(false);
        r = ru.getRef();
    } catch (IOException e) {
        throw new JGitInternalException("Error while executing reset command");
    }

    return r;
}

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

License:Apache License

public static void commit(final Git git, final String branchName, final String name, final String email,
        final String message, final TimeZone timeZone, final Date when, final Map<String, File> content) {

    final PersonIdent author = buildPersonIdent(git, name, email, timeZone, when);

    try {/*from  w  w w.j a  va  2s .  c o m*/
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");
            final DirCache index = createTemporaryIndex(git, headId, content);
            final ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            final CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage(message);
            //headId can be null if the repository has no commit yet
            if (headId != null) {
                commit.setParentId(headId);
            }
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            final ObjectId commitId = odi.insert(commit);
            odi.flush();

            final RevWalk revWalk = new RevWalk(git.getRepository());
            try {
                final RevCommit revCommit = revWalk.parseCommit(commitId);
                final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                if (headId == null) {
                    ru.setExpectedOldObjectId(ObjectId.zeroId());
                } else {
                    ru.setExpectedOldObjectId(headId);
                }
                ru.setNewObjectId(commitId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                final RefUpdate.Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            Constants.HEAD, commitId.toString(), rc));
                }

            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
}

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

License:Apache License

private static void updateHead(final Git git, final StringBuilder refLogMessage, final ObjectId newHeadId,
        final ObjectId oldHeadID) throws java.io.IOException, ConcurrentRefUpdateException {
    RefUpdate refUpdate = git.getRepository().updateRef(Constants.HEAD);
    refUpdate.setNewObjectId(newHeadId);
    refUpdate.setRefLogMessage(refLogMessage.toString(), false);
    refUpdate.setExpectedOldObjectId(oldHeadID);
    RefUpdate.Result rc = refUpdate.update();
    switch (rc) {
    case NEW:/*from w  w w . ja v a2  s  .  co  m*/
    case FAST_FORWARD:
        return;
    case REJECTED:
    case LOCK_FAILURE:
        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, refUpdate.getRef(), rc);
    default:
        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD,
                newHeadId.toString(), rc));
    }
}

From source file:org.moxie.utils.JGitUtils.java

License:Apache License

public static void updateGhPages(File repositoryFolder, File sourceFolder, boolean obliterate) {
    String ghpages = "refs/heads/gh-pages";
    try {/*from w  w w  . j  ava  2s .com*/
        File gitDir = FileKey.resolve(repositoryFolder, FS.DETECTED);
        Repository repository = new FileRepository(gitDir);

        ObjectId objectId = repository.resolve(ghpages);
        if (objectId == null) {
            JGitUtils.createOrphanBranch(repository, "gh-pages", null);
        }

        System.out.println("Updating gh-pages branch...");
        ObjectId headId = repository.resolve(ghpages + "^{commit}");
        ObjectInserter odi = repository.newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            DirCache index = createIndex(repository, headId, sourceFolder, obliterate);
            ObjectId indexTreeId = index.writeTree(odi);

            // Create a commit object
            PersonIdent author = new PersonIdent("Moxie", "moxie@localhost");
            CommitBuilder commit = new CommitBuilder();
            commit.setAuthor(author);
            commit.setCommitter(author);
            commit.setEncoding(Constants.CHARACTER_ENCODING);
            commit.setMessage("updated pages");
            commit.setParentId(headId);
            commit.setTreeId(indexTreeId);

            // Insert the commit into the repository
            ObjectId commitId = odi.insert(commit);
            odi.flush();

            RevWalk revWalk = new RevWalk(repository);
            try {
                RevCommit revCommit = revWalk.parseCommit(commitId);
                RefUpdate ru = repository.updateRef(ghpages);
                ru.setNewObjectId(commitId);
                ru.setExpectedOldObjectId(headId);
                ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                Result rc = ru.forceUpdate();
                switch (rc) {
                case NEW:
                case FORCED:
                case FAST_FORWARD:
                    break;
                case REJECTED:
                case LOCK_FAILURE:
                    throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
                default:
                    throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                            ghpages, commitId.toString(), rc));
                }
            } finally {
                revWalk.release();
            }
        } finally {
            odi.release();
        }
        System.out.println("gh-pages updated.");
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:org.n52.wps.repository.git.GitAlgorithmRepository.java

License:Open Source License

private void rollbackFromFailedMerge(Git git, ObjectId old) throws GitAPIException {
    // old objectId is the one before merge so we should be safe to have the right rollback id
    logger.warn("Doing a `reset --hard {}' from faild merge. Pull and merge manually!", old);
    git.reset().setMode(ResetCommand.ResetType.HARD).setRef(old.toString()).call();
}

From source file:org.uberfire.java.nio.fs.jgit.util.commands.SimpleRefUpdateCommand.java

License:Apache License

private void forceUpdate(final RefUpdate ru, final ObjectId id)
        throws java.io.IOException, ConcurrentRefUpdateException {
    final RefUpdate.Result rc = ru.forceUpdate();
    switch (rc) {
    case NEW://from   ww  w  . j  av  a 2  s. co  m
    case FORCED:
    case FAST_FORWARD:
        break;
    case REJECTED:
    case LOCK_FAILURE:
        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(), rc);
    default:
        throw new JGitInternalException(
                MessageFormat.format(JGitText.get().updatingRefFailed, Constants.HEAD, id.toString(), rc));
    }
}

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

License:Apache License

public static boolean commit(final Git git, final String branchName, final CommitInfo commitInfo,
        final boolean amend, final ObjectId _originId, final CommitContent content) {
    boolean hadEffecitiveCommit = true;
    final PersonIdent author = buildPersonIdent(git, commitInfo.getName(), commitInfo.getEmail(),
            commitInfo.getTimeZone(), commitInfo.getWhen());

    try {// w ww. j  ava  2 s. c  om
        final ObjectInserter odi = git.getRepository().newObjectInserter();
        try {
            // Create the in-memory index of the new/updated issue.
            final ObjectId headId = git.getRepository().resolve(branchName + "^{commit}");

            final ObjectId originId;
            if (_originId == null) {
                originId = git.getRepository().resolve(branchName + "^{commit}");
            } else {
                originId = _originId;
            }

            final DirCache index;
            if (content instanceof DefaultCommitContent) {
                index = createTemporaryIndex(git, originId, (DefaultCommitContent) content);
            } else if (content instanceof MoveCommitContent) {
                index = createTemporaryIndex(git, originId, (MoveCommitContent) content);
            } else if (content instanceof CopyCommitContent) {
                index = createTemporaryIndex(git, originId, (CopyCommitContent) content);
            } else if (content instanceof RevertCommitContent) {
                index = createTemporaryIndex(git, originId);
            } else {
                index = null;
            }

            if (index != null) {
                final ObjectId indexTreeId = index.writeTree(odi);

                // Create a commit object
                final CommitBuilder commit = new CommitBuilder();
                commit.setAuthor(author);
                commit.setCommitter(author);
                commit.setEncoding(Constants.CHARACTER_ENCODING);
                commit.setMessage(commitInfo.getMessage());
                //headId can be null if the repository has no commit yet
                if (headId != null) {
                    if (amend) {
                        final List<ObjectId> parents = new LinkedList<ObjectId>();
                        final RevCommit previousCommit = new RevWalk(git.getRepository()).parseCommit(headId);
                        final RevCommit[] p = previousCommit.getParents();
                        for (final RevCommit revCommit : p) {
                            parents.add(0, revCommit.getId());
                        }
                        commit.setParentIds(parents);
                    } else {
                        commit.setParentId(headId);
                    }
                }
                commit.setTreeId(indexTreeId);

                // Insert the commit into the repository
                final ObjectId commitId = odi.insert(commit);
                odi.flush();

                final RevWalk revWalk = new RevWalk(git.getRepository());
                try {
                    final RevCommit revCommit = revWalk.parseCommit(commitId);
                    final RefUpdate ru = git.getRepository().updateRef("refs/heads/" + branchName);
                    if (headId == null) {
                        ru.setExpectedOldObjectId(ObjectId.zeroId());
                    } else {
                        ru.setExpectedOldObjectId(headId);
                    }
                    ru.setNewObjectId(commitId);
                    ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
                    final RefUpdate.Result rc = ru.forceUpdate();
                    switch (rc) {
                    case NEW:
                    case FORCED:
                    case FAST_FORWARD:
                        break;
                    case REJECTED:
                    case LOCK_FAILURE:
                        throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD, ru.getRef(),
                                rc);
                    default:
                        throw new JGitInternalException(MessageFormat.format(JGitText.get().updatingRefFailed,
                                Constants.HEAD, commitId.toString(), rc));
                    }

                } finally {
                    revWalk.release();
                }
            } else {
                hadEffecitiveCommit = false;
            }
        } finally {
            odi.release();
        }
    } catch (final Throwable t) {
        throw new RuntimeException(t);
    }
    return hadEffecitiveCommit;
}

From source file:pl.project13.jgit.DescribeCommand.java

License:Open Source License

/** Checks if the given object id resolved to a tag object */
private boolean isTagId(ObjectId objectId) {
    return objectId.toString().startsWith("tag ");
}