Example usage for org.eclipse.jgit.lib ReflogEntry getNewId

List of usage examples for org.eclipse.jgit.lib ReflogEntry getNewId

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib ReflogEntry getNewId.

Prototype

ObjectId getNewId();

Source Link

Document

Get the commit id after the change

Usage

From source file:org.enterprisedomain.classmaker.impl.ContributionImpl.java

License:Apache License

/**
 * <!-- begin-user-doc --> <!-- end-user-doc -->
 * // w  ww.j a v  a  2 s. c  o m
 * @generated NOT
 */
public String initialize(boolean commit) {
    @SuppressWarnings("unchecked")
    SCMOperator<Git> operator = (SCMOperator<Git>) getWorkspace().getSCMRegistry().get(getProjectName());
    setName(getProjectName());
    try {
        Git git = operator.getRepositorySCM();
        // if (git == null)
        // return "";
        String currentBranch = git.getRepository().getBranch();

        ListBranchCommand listBranches = git.branchList();
        List<Ref> branches = listBranches.call();
        Iterator<Ref> it = branches.iterator();
        Ref branch = null;
        long timestamp = -1;
        String commitId = "";
        do {
            Version version = null;
            if (it.hasNext()) {
                branch = it.next();
                String[] name = branch.getName().split("/"); //$NON-NLS-1$
                try {
                    version = operator.decodeVersion(name[name.length - 1]);
                    ReflogCommand reflog = git.reflog();
                    reflog.setRef(branch.getName().toString());
                    Collection<ReflogEntry> refs = reflog.call();
                    for (ReflogEntry ref : refs)
                        if (ref.getNewId().equals(branch.getObjectId())) {
                            timestamp = operator.decodeTimestamp(ref.getComment());
                            if (timestamp == -1)
                                timestamp = operator.decodeTimestamp(version.getQualifier());
                        }
                } catch (IllegalArgumentException e) {
                    continue;
                }
            }
            if (version != null && !getRevisions().containsKey(version)) {
                Revision newRevision = newBareRevision(version);
                newRevision.setTimestamp(timestamp);
                newRevision.setProject(this);
                doNewRevision(newRevision);
                commitId = newRevision.initialize(commit);
            }
        } while (it.hasNext());
        if (!getRevisions().isEmpty() && getVersion().equals(Version.emptyVersion))
            setVersion(ListUtil.getLast(getRevisions()).getKey());
        else if (!getVersion().equals(Version.emptyVersion))
            checkout(getVersion(), timestamp);
        if (currentBranch.equals(SCMOperator.MASTER_BRANCH))
            checkout(getVersion(), timestamp);
        getWorkspace().getResourceSet().eAdapters().add(resourceAdapter);
        addResourceChangeListener(getResourceReloadListener());
        return commitId;
    } catch (Exception e) {
        ClassMakerPlugin.getInstance().getLog().log(ClassMakerPlugin.createErrorStatus(e));
        return null;
    } finally {
        try {
            operator.ungetRepositorySCM();
        } catch (Exception e) {
            ClassMakerPlugin.getInstance().getLog().log(ClassMakerPlugin.createErrorStatus(e));
        }
    }

}