Example usage for org.eclipse.jgit.lib AbbreviatedObjectId fromObjectId

List of usage examples for org.eclipse.jgit.lib AbbreviatedObjectId fromObjectId

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib AbbreviatedObjectId fromObjectId.

Prototype

public static final AbbreviatedObjectId fromObjectId(AnyObjectId id) 

Source Link

Document

Convert an AbbreviatedObjectId from an org.eclipse.jgit.lib.AnyObjectId .

Usage

From source file:com.madgag.agit.diff.LineContextDiffer.java

License:Open Source License

private byte[] open(ObjectReader reader, FileMode mode, AbbreviatedObjectId id) throws IOException {
    if (mode == FileMode.MISSING)
        return new byte[] {};

    if (mode.getObjectType() != Constants.OBJ_BLOB)
        return new byte[] {};

    if (!id.isComplete()) {
        Collection<ObjectId> ids = reader.resolve(id);
        if (ids.size() == 1)
            id = AbbreviatedObjectId.fromObjectId(ids.iterator().next());
        else if (ids.size() == 0)
            throw new MissingObjectException(id, Constants.OBJ_BLOB);
        else// www.j  a v  a  2  s . c om
            throw new AmbiguousObjectException(id, ids);
    }

    ObjectLoader ldr = reader.open(id.toObjectId());
    return ldr.getCachedBytes(bigFileThreshold);
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCache.java

License:Open Source License

/**
 * Scans given {@code repo} and build list of commits between two given
 * RevCommit objectId's. Each commit contains list of changed resources
 *
 * @param repo//from  w  ww .  ja va  2s . com
 *            repository that should be scanned
 * @param srcId
 *            RevCommit id that git history traverse will start from
 * @param dstId
 *            RevCommit id that git history traverse will end
 * @param pathFilter
 *            path filter definition or {@code null} when all paths should
 *            be included
 * @return list of {@link Commit} object's between {@code srcId} and
 *         {@code dstId}
 * @throws IOException
 */
public static List<Commit> build(Repository repo, ObjectId srcId, ObjectId dstId, TreeFilter pathFilter)
        throws IOException {
    if (dstId.equals(srcId))
        return new ArrayList<Commit>(0);

    final RevWalk rw = new RevWalk(repo);

    final RevFlag localFlag = rw.newFlag("local"); //$NON-NLS-1$
    final RevFlag remoteFlag = rw.newFlag("remote"); //$NON-NLS-1$
    final RevFlagSet allFlags = new RevFlagSet();
    allFlags.add(localFlag);
    allFlags.add(remoteFlag);
    rw.carry(allFlags);

    RevCommit srcCommit = rw.parseCommit(srcId);
    srcCommit.add(localFlag);
    rw.markStart(srcCommit);
    srcCommit = null; // free not needed resources

    RevCommit dstCommit = rw.parseCommit(dstId);
    dstCommit.add(remoteFlag);
    rw.markStart(dstCommit);
    dstCommit = null; // free not needed resources

    if (pathFilter != null)
        rw.setTreeFilter(pathFilter);

    List<Commit> result = new ArrayList<Commit>();
    for (RevCommit revCommit : rw) {
        if (revCommit.hasAll(allFlags))
            break;

        Commit commit = new Commit();
        commit.shortMessage = revCommit.getShortMessage();
        commit.commitId = AbbreviatedObjectId.fromObjectId(revCommit);
        commit.authorName = revCommit.getAuthorIdent().getName();
        commit.committerName = revCommit.getCommitterIdent().getName();
        commit.commitDate = revCommit.getAuthorIdent().getWhen();

        RevCommit actualCommit, parentCommit;
        if (revCommit.has(localFlag)) {
            actualCommit = revCommit;
            parentCommit = getParentCommit(revCommit);
            commit.direction = RIGHT;
        } else if (revCommit.has(remoteFlag)) {
            actualCommit = getParentCommit(revCommit);
            parentCommit = revCommit;
            commit.direction = LEFT;
        } else
            throw new GitCommitsModelDirectionException();

        commit.children = getChangedObjects(repo, actualCommit, parentCommit, pathFilter, commit.direction);

        if (commit.children != null)
            result.add(commit);
    }
    rw.dispose();

    return result;
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCache.java

License:Open Source License

private static Map<String, Change> getChangedObjects(Repository repo, RevCommit parentCommit,
        RevCommit remoteCommit, TreeFilter pathFilter, final int direction) throws IOException {
    final TreeWalk tw = new TreeWalk(repo);
    addTreeFilter(tw, parentCommit);//ww  w. j  a va2  s.  co m
    addTreeFilter(tw, remoteCommit);

    tw.setRecursive(true);
    if (pathFilter == null)
        tw.setFilter(ANY_DIFF);
    else
        tw.setFilter(AndTreeFilter.create(ANY_DIFF, pathFilter));

    final int localTreeId = direction == LEFT ? 1 : 0;
    final int remoteTreeId = direction == LEFT ? 0 : 1;
    final Map<String, Change> result = new HashMap<String, GitCommitsModelCache.Change>();
    final AbbreviatedObjectId actualCommit = getAbbreviatedObjectId(parentCommit);
    final AbbreviatedObjectId remoteCommitAbb = getAbbreviatedObjectId(remoteCommit);

    MutableObjectId idBuf = new MutableObjectId();
    while (tw.next()) {
        Change change = new Change();
        change.commitId = actualCommit;
        change.remoteCommitId = remoteCommitAbb;
        change.name = tw.getNameString();
        tw.getObjectId(idBuf, localTreeId);
        change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
        tw.getObjectId(idBuf, remoteTreeId);
        change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);

        calculateAndSetChangeKind(direction, change);

        result.put(tw.getPathString(), change);
    }
    tw.release();

    return result.size() > 0 ? result : null;
}

From source file:org.eclipse.egit.core.synchronize.GitCommitsModelCache.java

License:Open Source License

private static AbbreviatedObjectId getAbbreviatedObjectId(RevCommit commit) {
    if (commit != null)
        return AbbreviatedObjectId.fromObjectId(commit);
    else//w w w  . ja v a2 s  . c  o  m
        return ZERO_ID;
}

From source file:org.eclipse.egit.core.synchronize.GitSyncCache.java

License:Open Source License

/**
 * Create mapping for given repository and returns object associated with
 * this repository. Any other mapping will be overwritten.
 *
 * @param repo/*w  w  w.j a va 2 s .  c  o  m*/
 * @param remoteTree
 * @param baseTree
 * @return new mapping object associated with given {@link Repository}
 */
private GitSyncObjectCache put(Repository repo, ObjectId baseTree, ObjectId remoteTree) {
    ThreeWayDiffEntry entry = new ThreeWayDiffEntry();
    entry.baseId = AbbreviatedObjectId.fromObjectId(baseTree);
    entry.remoteId = AbbreviatedObjectId.fromObjectId(remoteTree);
    GitSyncObjectCache objectCache = new GitSyncObjectCache("", entry); //$NON-NLS-1$
    cache.put(repo.getDirectory(), objectCache);

    return objectCache;
}

From source file:org.eclipse.egit.core.synchronize.StagedChangeCache.java

License:Open Source License

/**
 * @param repo// w  w  w .  ja  v a2s.c o  m
 *            repository which should be scanned
 * @return list of changes in git staging area
 */
public static Map<String, Change> build(Repository repo) {
    TreeWalk tw = new TreeWalk(repo);
    try {
        tw.addTree(new DirCacheIterator(repo.readDirCache()));
        ObjectId headId = repo.resolve(HEAD);
        RevCommit headCommit;
        if (headId != null)
            headCommit = new RevWalk(repo).parseCommit(headId);
        else
            headCommit = null;

        AbbreviatedObjectId commitId;
        if (headCommit != null) {
            tw.addTree(headCommit.getTree());
            commitId = AbbreviatedObjectId.fromObjectId(headCommit);
        } else {
            tw.addTree(new EmptyTreeIterator());
            commitId = AbbreviatedObjectId.fromObjectId(zeroId());
        }

        tw.setRecursive(true);
        headCommit = null;

        MutableObjectId idBuf = new MutableObjectId();
        Map<String, Change> result = new HashMap<String, Change>();
        while (tw.next()) {
            if (!shouldIncludeEntry(tw))
                continue;

            Change change = new Change();
            change.name = tw.getNameString();
            change.remoteCommitId = commitId;

            tw.getObjectId(idBuf, 0);
            change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
            tw.getObjectId(idBuf, 1);
            change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);

            calculateAndSetChangeKind(RIGHT, change);

            result.put(tw.getPathString(), change);
        }
        tw.release();

        return result;
    } catch (IOException e) {
        Activator.error(e.getMessage(), e);
        return new HashMap<String, Change>(0);
    }
}

From source file:org.eclipse.egit.core.synchronize.ThreeWayDiffEntry.java

License:Open Source License

/**
 * Converts the TreeWalk into TreeWayDiffEntry headers.
 *
 * @param walk/*  w ww .ja  va  2s.c  om*/
 *            the TreeWalk to walk through. Must have exactly three trees in
 *            this order: local, base and remote and can't be recursive.
 * @return headers describing the changed file.
 * @throws IOException
 *             the repository cannot be accessed.
 * @throws IllegalArgumentException
 *             when {@code walk} doen't have exactly three trees, or when
 *             {@code walk} is recursive
 */
public static List<ThreeWayDiffEntry> scan(TreeWalk walk) throws IOException {
    if (walk.getTreeCount() != 3 && walk.getTreeCount() != 4)
        throw new IllegalArgumentException("TreeWalk need to have three or four trees"); //$NON-NLS-1$
    if (walk.isRecursive())
        throw new IllegalArgumentException("TreeWalk shouldn't be recursive."); //$NON-NLS-1$

    List<ThreeWayDiffEntry> r = new ArrayList<ThreeWayDiffEntry>();
    MutableObjectId idBuf = new MutableObjectId();
    while (walk.next()) {
        ThreeWayDiffEntry e = new ThreeWayDiffEntry();

        walk.getObjectId(idBuf, 0);
        e.localId = AbbreviatedObjectId.fromObjectId(idBuf);

        walk.getObjectId(idBuf, 1);
        e.baseId = AbbreviatedObjectId.fromObjectId(idBuf);

        walk.getObjectId(idBuf, 2);
        e.remoteId = AbbreviatedObjectId.fromObjectId(idBuf);

        boolean localSameAsBase = e.localId.equals(e.baseId);
        if (!A_ZERO.equals(e.localId) && localSameAsBase && e.baseId.equals(e.remoteId))
            continue;

        e.path = walk.getPathString();
        boolean localIsMissing = walk.getFileMode(0) == FileMode.MISSING;
        boolean baseIsMissing = walk.getFileMode(1) == FileMode.MISSING;
        boolean remoteIsMissing = walk.getFileMode(2) == FileMode.MISSING;

        if (localIsMissing || baseIsMissing || remoteIsMissing) {
            if (!localIsMissing && baseIsMissing && remoteIsMissing) {
                e.direction = Direction.OUTGOING;
                e.changeType = ChangeType.ADD;
            } else if (localIsMissing && baseIsMissing && !remoteIsMissing) {
                e.direction = Direction.INCOMING;
                e.changeType = ChangeType.ADD;
            } else if (!localIsMissing && !baseIsMissing && remoteIsMissing) {
                e.direction = Direction.INCOMING;
                e.changeType = ChangeType.DELETE;
            } else if (localIsMissing && !baseIsMissing && !remoteIsMissing) {
                e.direction = Direction.OUTGOING;
                e.changeType = ChangeType.DELETE;
            } else {
                e.direction = Direction.CONFLICTING;
                e.changeType = ChangeType.MODIFY;
            }
        } else {
            if (localSameAsBase && !e.localId.equals(e.remoteId))
                e.direction = Direction.INCOMING;
            else if (e.remoteId.equals(e.baseId) && !e.remoteId.equals(e.localId))
                e.direction = Direction.OUTGOING;
            else
                e.direction = Direction.CONFLICTING;

            e.changeType = ChangeType.MODIFY;
        }

        r.add(e);
        if (walk.isSubtree()) {
            e.isTree = true;
            walk.enterSubtree();
        }
    }

    return r;
}

From source file:org.eclipse.egit.core.synchronize.WorkingTreeChangeCache.java

License:Open Source License

/**
 * @param repo/*from w  ww.  j  a  v  a2s . c om*/
 *            with should be scanned
 * @return list of changes in working tree
 */
public static Map<String, Change> build(Repository repo) {
    TreeWalk tw = new TreeWalk(repo);
    try {
        int fileNth = tw.addTree(new FileTreeIterator(repo));
        int cacheNth = tw.addTree(new DirCacheIterator(repo.readDirCache()));
        tw.setFilter(new IndexDiffFilter(cacheNth, fileNth));
        tw.setRecursive(true);

        Map<String, Change> result = new HashMap<String, Change>();
        MutableObjectId idBuf = new MutableObjectId();
        while (tw.next()) {
            Change change = new Change();
            change.name = tw.getNameString();
            tw.getObjectId(idBuf, 0);
            change.objectId = AbbreviatedObjectId.fromObjectId(idBuf);
            tw.getObjectId(idBuf, 1);
            change.remoteObjectId = AbbreviatedObjectId.fromObjectId(idBuf);
            calculateAndSetChangeKind(RIGHT, change);

            result.put(tw.getPathString(), change);
        }
        tw.release();

        return result;
    } catch (IOException e) {
        Activator.error(e.getMessage(), e);
        return new HashMap<String, GitCommitsModelCache.Change>(0);
    }
}

From source file:org.eclipse.egit.ui.internal.push.SimplePushRefWizard.java

License:Open Source License

/**
 * Creates a new simple push wizard which can be used to push out a certain
 * object./* w  ww  .j  av a 2 s  .c o  m*/
 *
 * @param repo
 *            the repository the object belongs to
 * @param objectId
 *            the object that should be pushed.
 * @param title
 *            the wizard title
 * @throws URISyntaxException
 */
public SimplePushRefWizard(Repository repo, ObjectId objectId, String title) throws URISyntaxException {
    this(repo, objectId, AbbreviatedObjectId.fromObjectId(objectId).name(), title);
}

From source file:org.eclipse.egit.ui.internal.synchronize.mapping.GitChangeSetLabelProvider.java

License:Open Source License

private String getAbbreviatedId(GitModelCommit commit) {
    RevCommit remoteCommit = commit.getBaseCommit();
    ObjectReader reader = commit.getRepository().newObjectReader();
    ObjectId commitId = remoteCommit.getId();
    AbbreviatedObjectId shortId;//from   w ww  .j  av  a2  s .c  om
    try {
        shortId = reader.abbreviate(commitId, 6);
    } catch (IOException e) {
        shortId = AbbreviatedObjectId.fromObjectId(ObjectId.zeroId());
        Activator.logError(e.getMessage(), e);
    } finally {
        reader.release();
    }
    return shortId.name();
}