Example usage for org.eclipse.jgit.api Git log

List of usage examples for org.eclipse.jgit.api Git log

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git log.

Prototype

public LogCommand log() 

Source Link

Document

Return a command object to execute a Log command

Usage

From source file:util.Util.java

public static List<String> getLogAuthores(Git git) throws GitAPIException {
    List<String> logs = new ArrayList<>();
    Iterable<RevCommit> log = git.log().call();

    for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
        RevCommit rev = iterator.next();
        logs.add(rev.getAuthorIdent().getName());
    }//from   ww w.j a va2 s.c om

    System.out.println(logs.size());
    return logs;
}

From source file:util.Util.java

public static List<RevCommit> empilharRevs(Git git, Project project) {
    List<RevCommit> revs = new ArrayList<>();
    try {//from  w  w  w  .j  a  va2  s. c o  m
        Iterable<RevCommit> log = git.log().call();
        Iterator<RevCommit> iterator = log.iterator();
        RevCommit rev = iterator.next();
        revs.add(0, rev);
        rev = iterator.next();
        revs.add(0, rev);
        for (; iterator.hasNext();) {
            rev = iterator.next();
            revs.add(0, rev);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return revs;
}

From source file:wherehows.common.utils.GitUtil.java

License:Open Source License

/**
 * Fetch all commit metadata from the repo
 * @param repoDir repository directory//from  ww  w . j  a v a 2s.  c  o m
 * @return list of commit metadata
 * @throws IOException
 * @throws GitAPIException
 */
public static List<CommitMetadata> getRepoMetadata(String repoDir) throws IOException, GitAPIException {

    List<CommitMetadata> metadataList = new ArrayList<>();

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(new File(repoDir, ".git")).readEnvironment().findGitDir().build();

    // Current branch may not be master. Instead of hard coding determine the current branch
    String currentBranch = repository.getBranch();
    Ref head = repository.getRef("refs/heads/" + currentBranch); // current branch may not be "master"
    if (head == null) {
        return metadataList;
    }

    Git git = new Git(repository);

    RevWalk walk = new RevWalk(repository);
    RevCommit commit = walk.parseCommit(head.getObjectId());

    TreeWalk treeWalk = new TreeWalk(repository);
    treeWalk.addTree(commit.getTree());
    treeWalk.setRecursive(true);
    while (treeWalk.next()) {
        String filePath = treeWalk.getPathString();
        Iterable<RevCommit> commitLog = git.log().add(repository.resolve(Constants.HEAD)).addPath(filePath)
                .call();
        for (RevCommit r : commitLog) {
            CommitMetadata metadata = new CommitMetadata(r.getName());
            metadata.setFilePath(filePath);
            metadata.setFileName(FilenameUtils.getName(filePath));
            metadata.setMessage(r.getShortMessage().trim());
            // Difference between committer and author
            // refer to: http://git-scm.com/book/ch2-3.html
            PersonIdent committer = r.getCommitterIdent();
            PersonIdent author = r.getAuthorIdent();
            metadata.setAuthor(author.getName());
            metadata.setAuthorEmail(author.getEmailAddress());
            metadata.setCommitter(committer.getName());
            metadata.setCommitterEmail(committer.getEmailAddress());
            metadata.setCommitTime(committer.getWhen());
            metadataList.add(metadata);
        }
    }
    git.close();
    return metadataList;
}