Example usage for org.eclipse.jgit.lib Constants HEAD

List of usage examples for org.eclipse.jgit.lib Constants HEAD

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants HEAD.

Prototype

String HEAD

To view the source code for org.eclipse.jgit.lib Constants HEAD.

Click Source Link

Document

Special name for the "HEAD" symbolic-ref.

Usage

From source file:boa.datagen.scm.GitConnector.java

License:Apache License

@Override
public String getLastCommitId() {
    if (lastCommitId == null) {
        revwalk.reset();//from  w  ww  .ja  va 2s  . c  o m

        try {
            revwalk.markStart(revwalk.parseCommit(repository.resolve(Constants.HEAD)));
            revwalk.sort(RevSort.COMMIT_TIME_DESC);
            lastCommitId = revwalk.next().getId().toString();
        } catch (final Exception e) {
            if (debug)
                System.err.println("Git Error getting last commit for " + path + ". " + e.getMessage());
        }
    }
    return lastCommitId;
}

From source file:boa.datagen.scm.GitConnector.java

License:Apache License

@Override
protected void setRevisions() {
    try {/*from  w ww. jav a 2  s.c  o m*/
        revwalk.reset();
        revwalk.markStart(revwalk.parseCommit(repository.resolve(Constants.HEAD)));
        revwalk.sort(RevSort.TOPO, true);
        revwalk.sort(RevSort.COMMIT_TIME_DESC, true);
        revwalk.sort(RevSort.REVERSE, true);

        revisions.clear();
        revisionMap = new HashMap<String, Integer>();

        for (final RevCommit rc : revwalk) {
            final GitCommit gc = new GitCommit(repository, this);

            gc.setId(rc.getName());
            gc.setAuthor(rc.getAuthorIdent().getName());
            gc.setCommitter(rc.getCommitterIdent().getName());
            gc.setDate(new Date(((long) rc.getCommitTime()) * 1000));
            gc.setMessage(rc.getFullMessage());

            gc.getChangeFiles(this.revisionMap, rc);

            revisionMap.put(gc.id, revisions.size());
            revisions.add(gc);
        }
    } catch (final IOException e) {
        if (debug)
            System.err.println("Git Error getting parsing HEAD commit for " + path + ". " + e.getMessage());
    }
}

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

public SCMRepository info() {
    RevWalk rw = null;/*from   ww w.  jav a 2 s . co  m*/
    Git git = null;
    try {
        git = Git.open(new File(path));
        AnyObjectId headId = git.getRepository().resolve(Constants.HEAD);

        rw = new RevWalk(git.getRepository());
        RevCommit root = rw.parseCommit(headId);
        rw.sort(RevSort.REVERSE);
        rw.markStart(root);
        RevCommit lastCommit = rw.next();

        String origin = git.getRepository().getConfig().getString("remote", "origin", "url");

        return new SCMRepository(this, origin, path, headId.getName(), lastCommit.getName());
    } catch (Exception e) {
        throw new RuntimeException("error when info " + path, e);
    } finally {
        if (rw != null)
            rw.release();
        if (git != null)
            git.close();
    }

}

From source file:br.com.metricminer2.scm.GitRepository.java

License:Apache License

public ChangeSet getHead() {
    Git git = null;/* w w  w .  j a  v  a 2s.c o m*/
    try {
        git = Git.open(new File(path));
        ObjectId head = git.getRepository().resolve(Constants.HEAD);

        RevWalk revWalk = new RevWalk(git.getRepository());
        RevCommit r = revWalk.parseCommit(head);
        return new ChangeSet(r.getName(), convertToDate(r));

    } catch (Exception e) {
        throw new RuntimeException("error in getHead() for " + path, e);
    } finally {
        if (git != null)
            git.close();
    }

}

From source file:co.turnus.versioning.impl.GitVersioner.java

License:Open Source License

@Override
public Version getVersion(File file) {
    if (file == null) {
        return new SimpleFileVersioner().getVersion(file);
    }/*  ww w . j a v  a  2  s .c  o m*/

    Repository repo = searchNearestRepository(file);
    if (repo != null) {
        try {
            ObjectId revId = repo.resolve(Constants.HEAD);
            Date mdofifDate = getLastModifiedDate(file, repo, revId);
            String revision = getRevision(revId);

            Version v = VersioningFactory.eINSTANCE.createVersion();
            v.setVersioningDate(new Date());
            v.setLastModificationDate(mdofifDate);
            v.setAttribute("versioner", NAME);
            v.setId(revision);
            return v;

        } catch (Exception e) {
        }
    }

    return new SimpleFileVersioner().getVersion(file);
}

From source file:co.turnus.versioning.impl.GitVersioner.java

License:Open Source License

private Repository searchNearestRepository(File file) {
    if (file == null) {
        return null;
    } else if (file.exists()) {
        try {//from w  ww. j a va2 s. c  o m
            Repository repo = new FileRepositoryBuilder().findGitDir(file.getCanonicalFile()).build();
            ObjectId revision = repo.resolve(Constants.HEAD);
            if (revision != null) {
                return repo;
            }
        } catch (Exception e) {
        }
    }
    return searchNearestRepository(file.getParentFile());
}

From source file:com.beck.ep.team.git.GitListBuilder.java

License:BSD License

private int addTree(int mode, Shell shell, Repository rep, RevWalk rw, TreeWalk tw) throws Exception {
    int index = -1;
    switch (mode) {
    case GitModeMenuMgr.MODE_STAGING:
        tw.addTree(new DirCacheIterator(rep.readDirCache()));
        break;//from   w  w w .  j  a va 2s .  c o  m
    case GitModeMenuMgr.MODE_OTHERS://choose a GIT ref
        CompareTargetSelectionDialog dialog = new CompareTargetSelectionDialog(shell, rep, null);
        if (dialog.open() != CompareTargetSelectionDialog.OK) {
            return -2;
        }
        ObjectId obId = rep.getRef(dialog.getRefName()).getObjectId();//"master"
        tw.addTree(rw.parseCommit(obId).getTree());
        break;
    case GitModeMenuMgr.MODE_BRANCH_HEAD:
        Ref head = rep.getRef(Constants.HEAD);
        RevCommit rc = rw.parseCommit(head.getObjectId());
        tw.addTree(rc.getTree());
        break;
    default://compare to working area
        index = tw.addTree(new FileTreeIterator(rep));
        break;
    }
    return index;
}

From source file:com.buildautomation.jgit.api.GetFileAttributes.java

License:Apache License

private static RevTree getTree(Repository repository) throws IOException {
    ObjectId lastCommitId = repository.resolve(Constants.HEAD);

    // a RevWalk allows to walk over commits based on some filtering
    try (RevWalk revWalk = new RevWalk(repository)) {
        RevCommit commit = revWalk.parseCommit(lastCommitId);

        System.out.println("Time of commit (seconds since epoch): " + commit.getCommitTime());

        // and using commit's tree find the path
        RevTree tree = commit.getTree();
        System.out.println("Having tree: " + tree);
        return tree;
    }//from w  w w . j  ava  2s .  c  o m
}

From source file:com.buildautomation.jgit.api.ReadFileFromCommit.java

License:Apache License

public static void readFileFromCommit() throws IOException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // find the HEAD
        ObjectId lastCommitId = repository.resolve(Constants.HEAD);

        // a RevWalk allows to walk over commits based on some filtering that is defined
        try (RevWalk revWalk = new RevWalk(repository)) {
            RevCommit commit = revWalk.parseCommit(lastCommitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            System.out.println("Having tree: " + tree);

            // now try to find a specific file
            try (TreeWalk treeWalk = new TreeWalk(repository)) {
                treeWalk.addTree(tree);/*w  w w. jav  a2  s. co m*/
                treeWalk.setRecursive(true);
                treeWalk.setFilter(PathFilter.create("README.md"));
                if (!treeWalk.next()) {
                    throw new IllegalStateException("Did not find expected file 'README.md'");
                }

                ObjectId objectId = treeWalk.getObjectId(0);
                ObjectLoader loader = repository.open(objectId);

                // and then one can the loader to read the file
                loader.copyTo(System.out);
            }

            revWalk.dispose();
        }
    }
}

From source file:com.centurylink.mdw.dataaccess.file.VersionControlGit.java

License:Apache License

public String getCommit() throws IOException {
    ObjectId head = localRepo.resolve(Constants.HEAD);
    if (head != null)
        return head.getName();
    else/* www.  j  a  v a 2s.co m*/
        return null;
}