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:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testPull() throws Exception {
    File masterRepoDir = tmpDir.newFolder("master");
    File cloneRepoDir = tmpDir.newFolder("clone");

    Git masterGit = Git.init().setDirectory(masterRepoDir).call();
    Git cloneGit = Git.cloneRepository().setURI(masterRepoDir.getCanonicalPath()).setDirectory(cloneRepoDir)
            .call();/*w  ww.  ja  va 2  s.c  om*/
    GitRepositoryImpl cloneRepo = new GitRepositoryImpl(cloneGit);

    File testFile = new File(masterRepoDir, "test");
    testFile.createNewFile();

    masterGit.add().addFilepattern("test").call();
    masterGit.commit().setMessage("Test message").call();

    cloneRepo.pull();

    List<RevCommit> commits = IterableUtils.toList(cloneGit.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());

    List<String> committedFiles = getCommittedFiles(cloneGit.getRepository(), commits.get(0));

    assertNotNull(committedFiles);
    assertEquals(1, committedFiles.size());
    assertEquals("test", committedFiles.get(0));
}

From source file:org.eclipse.egit.core.test.op.CommitOperationTest.java

License:Open Source License

@Test
public void testCommitAll() throws Exception {
    IFile file1 = testUtils.addFileToProject(project.getProject(), "sub/a.txt", "some text");
    testUtils.addFileToProject(project.getProject(), "sub/b.txt", "some text");

    resources.add(project.getProject().getFolder("sub"));
    new AddToIndexOperation(resources).execute(null);
    CommitOperation commitOperation = new CommitOperation(null, null, null, TestUtils.AUTHOR,
            TestUtils.COMMITTER, "first commit");
    commitOperation.setCommitAll(true);//from   www. jav  a  2s  . c om
    commitOperation.setRepos(new Repository[] { repository });
    commitOperation.execute(null);

    Git git = new Git(repository);
    Iterator<RevCommit> commits = git.log().call().iterator();
    RevCommit firstCommit = commits.next();
    assertTrue(firstCommit.getCommitTime() > 0);

    assertEquals("first commit", firstCommit.getFullMessage());

    testUtils.changeContentOfFile(project.getProject(), file1, "changed text");

    commitOperation = new CommitOperation(null, null, null, TestUtils.AUTHOR, TestUtils.COMMITTER,
            "second commit");
    commitOperation.setCommitAll(true);
    commitOperation.setRepos(new Repository[] { repository });
    commitOperation.execute(null);

    git = new Git(repository);
    commits = git.log().call().iterator();
    RevCommit secondCommit = commits.next();
    assertTrue(secondCommit.getCommitTime() > 0);

    assertEquals("second commit", secondCommit.getFullMessage());
    secondCommit.getParent(0).equals(firstCommit);
    assertEquals("The Author", secondCommit.getAuthorIdent().getName());
    assertEquals("The.author@some.com", secondCommit.getAuthorIdent().getEmailAddress());
    assertEquals("The Commiter", secondCommit.getCommitterIdent().getName());
    assertEquals("The.committer@some.com", secondCommit.getCommitterIdent().getEmailAddress());
}

From source file:org.eclipse.egit.internal.relengtools.GetBugsOperation.java

License:Open Source License

private void getBugNumbersForProject(IProject project, IProgressMonitor monitor, Set<Integer> set)
        throws Exception {

    final RepositoryMapping rm = RepositoryMapping.getMapping(project);
    final Repository repository = rm.getRepository();

    final RevCommit previousCommit = ShowInfoHandler.getCommitForTag(repository,
            getProjectTag(project).getName());
    final RevCommit latestCommit = ShowInfoHandler.getLatestCommitFor(rm, repository, project);

    final Git git = new Git(repository);
    final LogCommand log = git.log();
    log.addRange(previousCommit, latestCommit);
    for (final RevCommit commit : log.call()) {
        findBugNumber(commit.getFullMessage(), set);
    }/*from w ww  . j  a va2  s  .  c o  m*/
}

From source file:org.eclipse.egit.internal.relengtools.ShowInfoHandler.java

License:Open Source License

public static void showLogBetween(final Repository repo, final RevCommit oldCommit, final RevCommit newCommit)
        throws MissingObjectException, IncorrectObjectTypeException, NoHeadException, Exception {
    final Git git = new Git(repo);
    final LogCommand command = git.log();
    command.addRange(oldCommit, newCommit);
    System.out.println("\nCommits:");
    for (final RevCommit rc : command.call()) {
        System.out.println(rc);/*from w w  w  . ja v  a2 s  .co m*/
        System.out.print("Tags: ");
        System.out.println(getTagsForCommit(repo, rc));
        System.out.println(rc.getShortMessage());
    }
}

From source file:org.eclipse.mylyn.internal.git.core.GitConnector.java

License:Open Source License

@Override
public List<ChangeSet> getChangeSets(ScmRepository repository, IProgressMonitor monitor) throws CoreException {
    List<ChangeSet> changeSets = new ArrayList<ChangeSet>();

    try {/*from  w ww  .j  a v  a2s .  c om*/
        Git git = new Git(((GitRepository) repository).getRepository());
        Iterable<RevCommit> revs = git.log().call();
        for (RevCommit r : revs) {
            changeSets.add(changeSet(r, repository, new ArrayList<Change>()));
        }
    } catch (NoHeadException e) {
    }

    return changeSets;
}

From source file:org.eclipse.oomph.gitbash.repository.ListFilesAction.java

License:Open Source License

private File checkHistory(Repository repository, IProgressMonitor monitor) throws Exception {
    Git git = new Git(repository);

    int commitCount = getCommitCount(git);
    monitor.beginTask("Listing all files", commitCount);

    Map<String, Set<String>> namesByExtension = new HashMap<String, Set<String>>();

    for (RevCommit commit : git.log().call()) {
        TreeWalk walk = new TreeWalk(repository);
        walk.setRecursive(true);//  w  w w .ja  va2s.  c o m
        walk.addTree(commit.getTree());

        while (walk.next()) {
            checkCancelation(monitor);

            String name = walk.getNameString();
            String extension;

            int lastDot = name.lastIndexOf('.');
            if (lastDot == -1) {
                extension = "";
            } else {
                extension = name.substring(lastDot + 1);
                name = name.substring(0, lastDot);
            }

            Set<String> names = namesByExtension.get(extension);
            if (names == null) {
                names = new HashSet<String>();
                namesByExtension.put(extension, names);
            }

            names.add(name);
        }

        try {
            walk.close();
        } catch (Throwable ex) {
            try {
                Method method = walk.getClass().getMethod("release");
                method.invoke(walk);
            } catch (Throwable ignore) {
                //$FALL-THROUGH$
            }
        }

        monitor.worked(1);
    }

    final File file = new File("files-in-" + repository.getWorkTree().getName() + ".txt");
    PrintStream stream = new PrintStream(file);

    try {
        for (String extension : sort(namesByExtension.keySet())) {
            List<String> names = sort(namesByExtension.get(extension));
            System.out.println(extension + "\t" + names.size());

            stream.println(extension + "\t" + names.size());
            for (String name : names) {
                stream.println("\t" + name);
            }
        }
    } finally {
        stream.close();
    }

    return file;
}

From source file:org.eclipse.oomph.gitbash.repository.ListFilesAction.java

License:Open Source License

private int getCommitCount(Git git) throws GitAPIException, NoHeadException {
    int commitCount = 0;
    for (@SuppressWarnings("unused")
    RevCommit commit : git.log().call()) {
        ++commitCount;// ww  w .  ja  v a 2s.c  om
    }

    return commitCount;
}

From source file:org.eclipse.orion.server.gerritfs.GerritListFile.java

License:Open Source License

private void lastCommit(Git git, String path, AnyObjectId revId, HashMap<String, Object> jsonObject) {
    HashMap<String, Object> latestCommitObj = new HashMap<String, Object>();
    HashMap<String, String> authorObj = new HashMap<String, String>();
    HashMap<String, String> committerObj = new HashMap<String, String>();
    Iterable<RevCommit> log = null;
    try {/* w  ww.  j a va  2 s.  co  m*/
        if (path != null) {
            log = git.log().addPath(path).setMaxCount(1).call();
        } else if (revId != null) {
            log = git.log().add(revId).setMaxCount(1).call();
        }
        Iterator<RevCommit> it = log.iterator();
        while (it.hasNext()) {
            RevCommit rev = (RevCommit) it.next();
            PersonIdent committer = rev.getCommitterIdent();
            committerObj.put("Name", committer.getName());
            committerObj.put("Email", committer.getEmailAddress());
            committerObj.put("Date", committer.getWhen().toString());

            PersonIdent author = rev.getAuthorIdent();
            authorObj.put("Name", author.getName());
            String authorEmail = author.getEmailAddress();
            authorObj.put("Email", authorEmail);
            authorObj.put("Date", author.getWhen().toString());

            latestCommitObj.put("Author", authorObj);
            latestCommitObj.put("Committer", committerObj);
            latestCommitObj.put("Message", rev.getFullMessage());
            latestCommitObj.put("SHA1", rev.getId().getName());
            latestCommitObj.put("AvatarURL", getImageLink(authorEmail));

            jsonObject.put("LastCommit", latestCommitObj);
        }
    } catch (GitAPIException e) {
    } catch (MissingObjectException e) {
    } catch (IncorrectObjectTypeException e) {
    }
}

From source file:org.eclipse.orion.server.git.jobs.ListBranchesJob.java

License:Open Source License

@Override
protected IStatus performJob() {
    try {/*w  w w . j  a  v  a2 s.  co  m*/
        File gitDir = GitUtils.getGitDir(path);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);
        List<Ref> branchRefs = git.branchList().call();
        List<Branch> branches = new ArrayList<Branch>(branchRefs.size());
        for (Ref ref : branchRefs) {
            branches.add(new Branch(cloneLocation, db, ref));
        }
        Collections.sort(branches, Branch.COMPARATOR);
        JSONObject result = new JSONObject();
        JSONArray children = new JSONArray();
        int firstBranch = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
        int lastBranch = pageSize > 0 ? firstBranch + pageSize - 1 : branches.size() - 1;
        lastBranch = lastBranch > branches.size() - 1 ? branches.size() - 1 : lastBranch;
        if (pageNo > 1 && baseLocation != null) {
            String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
        }
        if (lastBranch < branches.size() - 1) {
            String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
        }
        for (int i = firstBranch; i <= lastBranch; i++) {
            Branch branch = branches.get(i);
            if (commitsSize == 0) {
                children.put(branch.toJSON());
            } else {
                String branchName = branch.getName(true, false);
                ObjectId toObjectId = db.resolve(branchName);
                Ref toRefId = db.getRef(branchName);
                if (toObjectId == null) {
                    String msg = NLS.bind("No ref or commit found: {0}", branchName);
                    return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null);
                }
                toObjectId = getCommitObjectId(db, toObjectId);

                Log log = null;
                // single commit is requested and we already know it, no need for LogCommand 
                if (commitsSize == 1 && toObjectId instanceof RevCommit) {
                    log = new Log(cloneLocation, db, Collections.singleton((RevCommit) toObjectId), null, null,
                            toRefId);
                } else {
                    LogCommand lc = git.log();
                    // set the commit range
                    lc.add(toObjectId);
                    lc.setMaxCount(this.commitsSize);
                    Iterable<RevCommit> commits = lc.call();
                    log = new Log(cloneLocation, db, commits, null, null, toRefId);
                }
                log.setPaging(1, commitsSize);
                children.put(branch.toJSON(log.toJSON()));
            }
        }
        result.put(ProtocolConstants.KEY_CHILDREN, children);
        result.put(ProtocolConstants.KEY_TYPE, Branch.TYPE);
        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when listing branches for {0}", path);
        return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    }
}

From source file:org.eclipse.orion.server.git.jobs.ListTagsJob.java

License:Open Source License

@Override
protected IStatus performJob() {
    try {/*  w w w  .j  a va2s  .co m*/
        // list all tags
        File gitDir = GitUtils.getGitDir(path);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);
        List<Ref> refs = git.tagList().call();
        JSONObject result = new JSONObject();
        List<Tag> tags = new ArrayList<Tag>();
        for (Ref ref : refs) {
            Tag tag = new Tag(cloneLocation, db, ref);
            tags.add(tag);
        }
        Collections.sort(tags, Tag.COMPARATOR);
        JSONArray children = new JSONArray();
        int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
        int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1;
        lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag;
        if (pageNo > 1 && baseLocation != null) {
            String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
        }
        if (lastTag < tags.size() - 1) {
            String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
            if (commitsSize > 0) {
                next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
            }
            result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
        }
        for (int i = firstTag; i <= lastTag; i++) {
            Tag tag = tags.get(i);
            if (this.commitsSize == 0) {
                children.put(tag.toJSON());
            } else {
                // add info about commits if requested
                LogCommand lc = git.log();
                String toCommitName = tag.getRevCommitName();
                ObjectId toCommitId = db.resolve(toCommitName);
                Ref toCommitRef = db.getRef(toCommitName);
                toCommitId = getCommitObjectId(db, toCommitId);
                lc.add(toCommitId);
                lc.setMaxCount(this.commitsSize);
                Iterable<RevCommit> commits = lc.call();
                Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef);
                log.setPaging(1, commitsSize);
                children.put(tag.toJSON(log.toJSON()));
            }
        }
        result.put(ProtocolConstants.KEY_CHILDREN, children);
        result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE);
        return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
        String msg = NLS.bind("An error occured when listing tags for {0}", path);
        return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    }
}