Example usage for org.eclipse.jgit.lib Repository resolve

List of usage examples for org.eclipse.jgit.lib Repository resolve

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository resolve.

Prototype

@Nullable
public ObjectId resolve(String revstr)
        throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException, IOException 

Source Link

Document

Parse a git revision string and return an object id.

Usage

From source file:org.eclipse.ptp.internal.rdt.sync.git.core.JGitRepo.java

License:Open Source License

private boolean anyDiffInIndex() throws IOException {
    final Repository repo = getRepository();
    final TreeWalk treeWalk = new TreeWalk(repo);
    treeWalk.setRecursive(true); //recursive is required because a hash for a (sub)tree might not be available
    final ObjectId rev = repo.resolve("HEAD"); //$NON-NLS-1$
    if (rev != null) { //HEAD doesn't exist for a new repo, then we want to compare against empty tree
        treeWalk.addTree(new RevWalk(repo).parseTree(rev));
    }//  w w  w  . ja  v a  2s.c om
    treeWalk.addTree(new DirCacheIterator(repo.readDirCache()));
    treeWalk.setFilter(TreeFilter.ANY_DIFF);
    return treeWalk.next();
}

From source file:org.eclipse.releng.tools.git.GitCopyrightAdapter.java

License:Open Source License

public int getLastModifiedYear(IFile file, IProgressMonitor monitor) throws CoreException {
    try {/*from   ww  w.  j  a v a 2  s. c o m*/
        monitor.beginTask("Fetching logs from Git", 100); //$NON-NLS-1$
        final RepositoryMapping mapping = RepositoryMapping.getMapping(file);
        if (mapping != null) {
            final Repository repo = mapping.getRepository();
            if (repo != null) {
                RevWalk walk = null;
                try {
                    final ObjectId start = repo.resolve(Constants.HEAD);
                    walk = new RevWalk(repo);
                    walk.setTreeFilter(AndTreeFilter
                            .create(PathFilter.create(mapping.getRepoRelativePath(file)), TreeFilter.ANY_DIFF));
                    walk.markStart(walk.lookupCommit(start));
                    final RevCommit commit = walk.next();
                    if (commit != null) {
                        if (filterString != null
                                && commit.getFullMessage().toLowerCase().indexOf(filterString) != -1) {
                            // the last update was a copyright check in - ignore
                            return 0;
                        }

                        boolean isSWT = file.getProject().getName().startsWith("org.eclipse.swt"); //$NON-NLS-1$
                        String logComment = commit.getFullMessage();
                        if (isSWT && (logComment.indexOf("restore HEAD after accidental deletion") != -1 //$NON-NLS-1$
                                || logComment.indexOf("fix permission of files") != -1)) { //$NON-NLS-1$
                            // ignore commits with above comments
                            return 0;
                        }

                        boolean isPlatform = file.getProject().getName().equals("eclipse.platform"); //$NON-NLS-1$
                        if (isPlatform && (logComment
                                .indexOf("Merge in ant and update from origin/master") != -1 //$NON-NLS-1$
                                || logComment.indexOf(
                                        "Fixed bug 381684: Remove update from repository and map files") != -1 //$NON-NLS-1$
                                || logComment.indexOf("Restored 'org.eclipse.update.core'") != -1)) { //$NON-NLS-1$
                            // ignore commits with above comments
                            return 0;
                        }

                        final Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(0);
                        calendar.add(Calendar.SECOND, commit.getCommitTime());
                        return calendar.get(Calendar.YEAR);
                    }
                } catch (final IOException e) {
                    throw new CoreException(new Status(IStatus.ERROR, RelEngPlugin.ID, 0,
                            NLS.bind("An error occured when processing {0}", file.getName()), e));
                } finally {
                    if (walk != null)
                        walk.release();
                }
            }
        }
    } finally {
        monitor.done();
    }
    return -1;
}

From source file:org.eclipse.tycho.extras.buildtimestamp.jgit.JGitBuildTimestampProvider.java

License:Open Source License

public Date getTimestamp(MavenSession session, MavenProject project, MojoExecution execution)
        throws MojoExecutionException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder() //
            .readEnvironment() //
            .findGitDir(project.getBasedir()) //
            .setMustExist(true);// ww w .  j av  a  2s.c o m
    try {
        Repository repository = builder.build();
        try {
            RevWalk walk = new RevWalk(repository);
            try {
                String relPath = getRelPath(repository, project);
                if (relPath != null && relPath.length() > 0) {
                    walk.setTreeFilter(AndTreeFilter.create(new PathFilter(relPath, getIgnoreFilter(execution)),
                            TreeFilter.ANY_DIFF));
                }

                // TODO make sure working tree is clean

                ObjectId headId = repository.resolve(Constants.HEAD);
                walk.markStart(walk.parseCommit(headId));
                RevCommit commit = walk.next();
                return new Date(commit.getCommitTime() * 1000L);
            } finally {
                walk.release();
            }
        } finally {
            repository.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Could not determine git commit timestamp", e);
    }
}

From source file:org.eclipse.tycho.extras.sourceref.jgit.JGitSourceReferencesProvider.java

License:Open Source License

private ObjectId resolveHead(Repository repo) throws MojoExecutionException {
    ObjectId head;/*  www  .  java2  s  .  c o m*/
    try {
        head = repo.resolve(Constants.HEAD);
    } catch (AmbiguousObjectException e) {
        throw new MojoExecutionException("exception trying resolve HEAD", e);
    } catch (IOException e) {
        throw new MojoExecutionException("exception trying resolve HEAD", e);
    }
    return head;
}

From source file:org.efaps.cli.rest.ImportCICall.java

License:Apache License

/**
 * Gets the file information./*from w w  w. j  ava  2  s.  co m*/
 *
 * @param _file the _file
 * @return the file information
 */
protected String[] getFileInformation(final File _file) {
    final String[] ret = new String[2];

    try {
        final Repository repo = new FileRepository(evalGitDir(_file));

        final ObjectId lastCommitId = repo.resolve(Constants.HEAD);

        final PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
        final PlotWalk revWalk = new PlotWalk(repo);

        final RevCommit root = revWalk.parseCommit(lastCommitId);
        revWalk.markStart(root);
        revWalk.setTreeFilter(AndTreeFilter.create(
                PathFilter.create(_file.getPath().replaceFirst(repo.getWorkTree().getPath() + "/", "")),
                TreeFilter.ANY_DIFF));
        plotCommitList.source(revWalk);
        plotCommitList.fillTo(2);
        final PlotCommit<PlotLane> commit = plotCommitList.get(0);
        if (commit != null) {
            final PersonIdent authorIdent = commit.getAuthorIdent();
            final Date authorDate = authorIdent.getWhen();
            final TimeZone authorTimeZone = authorIdent.getTimeZone();
            final DateTime dateTime = new DateTime(authorDate.getTime(),
                    DateTimeZone.forTimeZone(authorTimeZone));
            ret[1] = dateTime.toString();
            ret[0] = commit.getId().getName();
        } else {
            ret[1] = new DateTime().toString();
            ret[0] = "UNKNOWN";
        }
    } catch (final RevisionSyntaxException | IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:org.efaps.eclipse.rest.RestClient.java

License:Apache License

protected String[] getFileInformation(final File _file) {
    final String[] ret = new String[2];

    try {//  ww  w. j  av a2s.co m
        final Repository repo = new FileRepository(evalGitDir(_file));

        final ObjectId lastCommitId = repo.resolve(Constants.HEAD);

        final PlotCommitList<PlotLane> plotCommitList = new PlotCommitList<PlotLane>();
        final PlotWalk revWalk = new PlotWalk(repo);

        final RevCommit root = revWalk.parseCommit(lastCommitId);
        revWalk.markStart(root);
        revWalk.setTreeFilter(AndTreeFilter.create(
                PathFilter.create(_file.getPath().replaceFirst(repo.getWorkTree().getPath() + "/", "")),
                TreeFilter.ANY_DIFF));
        plotCommitList.source(revWalk);
        plotCommitList.fillTo(2);
        final PlotCommit<PlotLane> commit = plotCommitList.get(0);
        if (commit != null) {
            final PersonIdent authorIdent = commit.getAuthorIdent();
            final Date authorDate = authorIdent.getWhen();
            final TimeZone authorTimeZone = authorIdent.getTimeZone();
            final DateTime dateTime = new DateTime(authorDate.getTime(),
                    DateTimeZone.forTimeZone(authorTimeZone));
            ret[1] = dateTime.toString();
            ret[0] = commit.getId().getName();
        } else {
            ret[1] = new DateTime().toString();
            ret[0] = "UNKNOWN";
        }
    } catch (RevisionSyntaxException | IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:org.eluder.coveralls.maven.plugin.domain.GitRepository.java

License:Open Source License

private Git.Head getHead(final Repository repository) throws IOException {
    ObjectId revision = repository.resolve(Constants.HEAD);
    RevCommit commit = new RevWalk(repository).parseCommit(revision);
    Git.Head head = new Git.Head(revision.getName(), commit.getAuthorIdent().getName(),
            commit.getAuthorIdent().getEmailAddress(), commit.getCommitterIdent().getName(),
            commit.getCommitterIdent().getEmailAddress(), commit.getFullMessage());
    return head;//w w w.jav a  2 s  . c om
}

From source file:org.exist.git.xquery.Cat.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*ww w .  j  a v a  2  s . c om*/
        String localPath = args[0].getStringValue();
        if (!(localPath.endsWith("/")))
            localPath += File.separator;

        String objPath = args[1].getStringValue();

        if (mySignature.getName() == CAT_WORKING_COPY) {
            Resource resource = new Resource(localPath + objPath);

            InputStream is = new ResourceInputStream(resource);
            ;

            Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
            return b64doc;

        }

        Git git = Git.open(new Resource(localPath), FS);
        Repository repository = git.getRepository();

        // find the HEAD
        ObjectId lastCommitId = repository.resolve(Constants.HEAD);
        // now we have to get the commit
        RevWalk revWalk = new RevWalk(repository);
        RevCommit commit = revWalk.parseCommit(lastCommitId);
        // and using commit's tree find the path
        RevTree tree = commit.getTree();
        TreeWalk treeWalk = new TreeWalk(repository);
        treeWalk.addTree(tree);
        treeWalk.setRecursive(true);
        treeWalk.setFilter(PathFilter.create(args[1].getStringValue()));
        if (!treeWalk.next()) {
            return Sequence.EMPTY_SEQUENCE;
        }
        ObjectId objectId = treeWalk.getObjectId(0);
        ObjectLoader loader = repository.open(objectId);

        // and then one can use either
        InputStream is = loader.openStream();

        Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
        return b64doc;
    } catch (Throwable e) {
        throw new XPathException(this, Module.EXGIT001, e);
    }
}

From source file:org.exist.git.xquery.Status.java

License:Open Source License

public void diff(final Repository repository, final String revstr,
        final WorkingTreeIterator initialWorkingTreeIterator, final StatusBuilder builder, final String folder,
        boolean recursive) throws IOException {

    RevTree tree = null;//from w  ww.j av  a  2  s.c  om

    ObjectId objectId = repository.resolve(revstr);
    if (objectId != null)
        tree = new RevWalk(repository).parseTree(objectId);
    else
        tree = null;

    PathFilter filter = folder == null || folder.isEmpty() ? null : PathFilter.create(folder);
    IndexDiffFilter indexDiffFilter;

    DirCache dirCache = repository.readDirCache();

    TreeWalk treeWalk = new TreeWalk(repository);
    //      TreeWalk treeWalk = TreeWalk.forPath(repository, folder, tree);
    treeWalk.setRecursive(recursive);
    // add the trees (tree, dirchache, workdir)
    if (tree != null)
        treeWalk.addTree(tree);
    else
        treeWalk.addTree(new EmptyTreeIterator());
    treeWalk.addTree(new DirCacheIterator(dirCache));
    treeWalk.addTree(initialWorkingTreeIterator);
    Collection<TreeFilter> filters = new ArrayList<TreeFilter>(4);

    if (filter != null)
        filters.add(filter);
    filters.add(new SkipWorkTreeFilter(INDEX));
    indexDiffFilter = new IndexDiffFilter(INDEX, WORKDIR);
    filters.add(indexDiffFilter);
    treeWalk.setFilter(AndTreeFilter.create(filters));
    if (filter != null) {
        while (treeWalk.next()) {
            if (filter.include(treeWalk)) {
                if (filter.isDone(treeWalk)) {
                    if (treeWalk.isSubtree()) {
                        treeWalk.enterSubtree();
                    }
                    break;
                } else if (treeWalk.isSubtree()) {
                    treeWalk.enterSubtree();
                }
            }
        }
    }

    while (treeWalk.next()) {
        AbstractTreeIterator treeIterator = treeWalk.getTree(TREE, AbstractTreeIterator.class);
        DirCacheIterator dirCacheIterator = treeWalk.getTree(INDEX, DirCacheIterator.class);
        WorkingTreeIterator workingTreeIterator = treeWalk.getTree(WORKDIR, WorkingTreeIterator.class);

        if (dirCacheIterator != null) {
            final DirCacheEntry dirCacheEntry = dirCacheIterator.getDirCacheEntry();
            if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
                builder.conflict(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        }

        if (treeIterator != null) {
            if (dirCacheIterator != null) {
                if (!treeIterator.idEqual(dirCacheIterator)
                        || treeIterator.getEntryRawMode() != dirCacheIterator.getEntryRawMode()) {
                    // in repo, in index, content diff => changed
                    builder.changed(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            } else {
                // in repo, not in index => removed
                builder.removed(treeWalk.getFileMode(2), treeWalk.getPathString());
                if (workingTreeIterator != null) //XXX: 2 statuses
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            }
        } else {
            if (dirCacheIterator != null) {
                // not in repo, in index => added
                builder.added(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                // not in repo, not in index => untracked
                if (workingTreeIterator != null && !workingTreeIterator.isEntryIgnored()) {
                    builder.untracked(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }

        if (dirCacheIterator != null) {
            if (workingTreeIterator == null) {
                // in index, not in workdir => missing
                builder.missing(treeWalk.getFileMode(2), treeWalk.getPathString());
                continue;
            } else {
                if (dirCacheIterator.getDirCacheEntry() == null) {
                    //XXX: null on collections - to fix
                    //                  builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());

                } else if (workingTreeIterator.isModified(dirCacheIterator.getDirCacheEntry(), true)) {
                    // in index, in workdir, content differs => modified
                    builder.modified(treeWalk.getFileMode(2), treeWalk.getPathString());
                    continue;
                }
            }
        }
        builder.unchanged(treeWalk.getFileMode(2), treeWalk.getPathString());
    }

    //      for (String path : indexDiffFilter.getUntrackedFolders()) {
    //         builder.untrackedFolders(path);
    //      }
    //      
    for (String path : indexDiffFilter.getIgnoredPaths()) {
        //XXX: to fix FileMode
        builder.ignored(FileMode.REGULAR_FILE, path);
    }
}

From source file:org.flowerplatform.web.git.GitUtils.java

License:Open Source License

public RevCommit getHeadCommit(Repository repository) {
    RevCommit headCommit = null;//from   w ww . j a v a 2 s . c om
    try {
        ObjectId parentId = repository.resolve(Constants.HEAD);
        if (parentId != null) {
            headCommit = new RevWalk(repository).parseCommit(parentId);
        }
    } catch (IOException e) {
        return null;
    }
    return headCommit;
}