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:com.google.gct.idea.appengine.synchronization.SampleSyncTaskTest.java

License:Apache License

@Ignore
@Test//  ww  w. j a  va2 s .c om
public void testSync_multipleCommits() throws GitAPIException, IOException {
    // Sync files from mock Git Hub repo to mock local Android sample template repo
    SampleSyncTask sampleSyncTask = new SampleSyncTask(mockAndroidRepoPath, mockGitHubRepoPath);
    sampleSyncTask.run();

    // Add 2 files to mock github repo
    RevCommit commit1 = addFileToMockGitHubRepo("a.txt", "Adding a.txt");
    Assert.assertNotNull(commit1);
    RevCommit commit2 = addFileToMockGitHubRepo("b.txt", "Adding b.txt");
    Assert.assertNotNull(commit2);

    // Delete a file from github repo
    RevCommit commit3 = removeFileFromMockGitHubRepo("a.txt", "Removing a.txt");
    Assert.assertNotNull(commit3);

    // Sync files from mock Git Hub repo to mock local Android sample template repo
    sampleSyncTask.run();

    // Check that last 3 commits in mock Android repo
    Git mockAndroidRepo = Git.open(new File(mockAndroidRepoPath));
    Iterable<RevCommit> logs = mockAndroidRepo.log().call();
    Assert.assertNotNull(logs);
    ArrayList<RevCommit> logsList = Lists.newArrayList(logs);

    Assert.assertTrue(logsList.size() >= 3);
    Assert.assertEquals(commit3.getCommitTime(), logsList.get(0).getCommitTime());
    Assert.assertEquals(commit3.getFullMessage(), logsList.get(0).getFullMessage());
    Assert.assertEquals(commit2.getCommitTime(), logsList.get(1).getCommitTime());
    Assert.assertEquals(commit2.getFullMessage(), logsList.get(1).getFullMessage());
    Assert.assertEquals(commit1.getCommitTime(), logsList.get(2).getCommitTime());
    Assert.assertEquals(commit1.getFullMessage(), logsList.get(2).getFullMessage());
}

From source file:com.googlesource.gerrit.plugins.smartreviewers.SmartReviewers.java

License:Apache License

/**
 * Fill a map of all the possible reviewers based on the last reviews they made
 * on the modified file.//from  w w w .  java2  s . c om
 *
 * @param entry {@link PatchListEntry}
 * @param reviewers the reviewer hash table to fill
 */
void getReviewersFromLastReviews(final PatchListEntry entry, Map<Account, Integer> reviewers) {
    Git git = new Git(repo);
    Iterable<RevCommit> commits;

    // Get the last 10 commits impacting the file
    try {
        commits = git.log().addPath(entry.getNewName()).setMaxCount(10).call();
    } catch (Exception ex) {
        log.error("Couldn't execute log for file {}", entry.getNewName(), ex);
        return;
    }

    // For each commit...
    for (RevCommit commit : commits) {
        List<String> change_ids = commit.getFooterLines("Change-Id");

        if (change_ids.size() != 1) {
            continue;
        }

        // Get the related changes
        List<Change> changes;
        try {
            Change.Key key = new Change.Key(change_ids.get(0));
            changes = reviewDb.changes().byKey(key).toList();
        } catch (Exception ex) {
            log.error("Couldn't get changes related to change-id {}", change_ids.get(0), ex);
            continue;
        }

        // For each related change, add accounts who reviewed the file before
        for (Change change : changes) {
            try {
                Set<Id> authors = new HashSet<Id>();
                List<ChangeMessage> messages = reviewDb.changeMessages().byChange(change.getId()).toList();
                for (ChangeMessage message : messages) {
                    authors.add(message.getAuthor());
                }

                List<PatchSetApproval> psas = reviewDb.patchSetApprovals().byChange(change.getId()).toList();
                for (PatchSetApproval psa : psas) {
                    if (psa.getValue() == 0) {
                        continue;
                    }
                    Account account = reviewDb.accounts().get(psa.getAccountId());
                    if (!authors.isEmpty() && !authors.contains(psa.getAccountId())) {
                        continue;
                    }
                    if (!psa.getLabel().contains("Code-Review")) {
                        continue;
                    }
                    addAccount(account, reviewers, weightLastReviews);
                }
            } catch (OrmException e) {
                log.error("getReviewersFromLastReviews() failed");
                e.printStackTrace();
            }
        }
    }
}

From source file:com.madgag.agit.BranchViewer.java

License:Open Source License

private List<RevCommit> commitListForRepo() {
    Git git = new Git(repository);
    try {//from w  w w  .  j a  va2 s  .co m
        Ref branch = branch();
        Log.d(TAG, "Calculating commitListForRepo based on " + branch + " branch.getObjectId()="
                + branch.getObjectId());
        Iterable<RevCommit> logWaa = git.log().add(branch.getObjectId()).call();
        List<RevCommit> sampleRevCommits = newArrayList(logWaa);

        Log.d(TAG, "Found " + sampleRevCommits.size() + " commits");

        return sampleRevCommits;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.maiereni.sling.sources.git.GitProjectDownloader.java

License:Apache License

private void update(final Git git) throws Exception {
    logger.debug("Fetch from remote");
    FetchResult fr = git.fetch().call(); // .setRemote(url)
    Collection<Ref> refs = fr.getAdvertisedRefs();
    Iterable<RevCommit> logs = git.log().call();
    for (RevCommit rev : logs) {
        PersonIdent authorIdent = rev.getAuthorIdent();
        Date date = authorIdent.getWhen();
        String authName = authorIdent.getName();
        logger.debug("Commit at " + SDF.format(date) + " by " + authName + ": " + rev.getId().name() + " text: "
                + rev.getShortMessage());
    }// ww w.jav  a  2 s .  c o m
    List<Note> notes = git.notesList().call();
    for (Ref ref : refs) {
        if (ref.getName().equals("HEAD")) {
            git.rebase().setUpstream(ref.getObjectId()).call();
            logger.debug("Rebase on HEAD");
            for (Note note : notes) {
                if (note.getName().equals(ref.getObjectId().getName())) {
                    logger.debug("Found note: " + note + " for commit " + ref.getName());
                    // displaying the contents of the note is done via a simple blob-read
                    ObjectLoader loader = git.getRepository().open(note.getData());
                    loader.copyTo(System.out);
                }
            }
        }
    }
}

From source file:com.microsoft.gittf.core.tasks.CloneTaskTest.java

License:Open Source License

@Test
public void testShallowCloneFilesAndFolders() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//from w  ww .  j  a va  2s .com

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);
    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);
    final boolean defaultDeep = repository.getConfig().getInt(ConfigurationConstants.CONFIGURATION_SECTION,
            ConfigurationConstants.GENERAL_SUBSECTION, ConfigurationConstants.DEPTH,
            GitTFConstants.GIT_TF_SHALLOW_DEPTH) > 1;

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Load Git Repo
    Git git = new Git(repository);
    git.checkout().setName("master").call(); //$NON-NLS-1$

    // Verify Changeset 1
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file0.txt"), //$NON-NLS-1$
            "$/project/folder2/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file0.txt", //$NON-NLS-1$
            1));

    // Verify Changeset 2
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file1.txt"), //$NON-NLS-1$
            "$/project/folder2/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
            2));

    // Verify Changeset 3
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file2.txt"), //$NON-NLS-1$
            "$/project/folder2/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file2.txt", //$NON-NLS-1$
            3));

    // Verify Git Repo configuration
    GitTFConfiguration gitRepoServerConfig = GitTFConfiguration.loadFrom(repository);

    assertEquals(gitRepoServerConfig.getServerURI(), projectCollectionURI);
    assertEquals(gitRepoServerConfig.getServerPath(), tfsPath);
    assertEquals(gitRepoServerConfig.getDeep(), defaultDeep);

    // Verify the number of commits
    Iterable<RevCommit> commits = git.log().call();

    assertNotNull(commits);

    int commitCounter = 0;
    for (RevCommit commit : commits) {
        assertEquals(commit.getFullMessage(), "comment"); //$NON-NLS-1$

        PersonIdent ownwer = commit.getAuthorIdent();
        assertEquals(ownwer.getEmailAddress(), "ownerName"); //$NON-NLS-1$
        assertEquals(ownwer.getName(), "ownerDisplayName"); //$NON-NLS-1$

        PersonIdent committer = commit.getCommitterIdent();
        assertEquals(committer.getEmailAddress(), "committerName"); //$NON-NLS-1$
        assertEquals(committer.getName(), "committerDisplayName"); //$NON-NLS-1$

        commitCounter++;
    }

    assertEquals(commitCounter, 1);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(1, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.CloneTaskTest.java

License:Open Source License

@Test
public void testDeepCloneFilesAndFoldersSimple() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/folder/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file0.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file0.txt", 1); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file1.txt", 2); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file1.txt", 2); //$NON-NLS-1$

    mockVersionControlService.AddFile("$/project/folder/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder2/file2.txt", 3); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/folder/nestedFolder/file2.txt", 3); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);//  w ww .j  a va 2 s.c  om

    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName1", //$NON-NLS-1$
            "ownerName1", //$NON-NLS-1$
            "committerDisplayName1", //$NON-NLS-1$
            "committerName1", //$NON-NLS-1$
            "comment1", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 1);

    changesetProperties = new MockChangesetProperties("ownerDisplayName2", //$NON-NLS-1$
            "ownerName2", //$NON-NLS-1$
            "committerDisplayName2", //$NON-NLS-1$
            "committerName2", //$NON-NLS-1$
            "comment2", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 2);

    changesetProperties = new MockChangesetProperties("ownerDisplayName3", //$NON-NLS-1$
            "ownerName3", //$NON-NLS-1$
            "committerDisplayName3", //$NON-NLS-1$
            "committerName3", //$NON-NLS-1$
            "comment3", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 3);

    final Repository repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);
    final boolean defaultDeep = repository.getConfig().getInt(ConfigurationConstants.CONFIGURATION_SECTION,
            ConfigurationConstants.GENERAL_SUBSECTION, ConfigurationConstants.DEPTH,
            GitTFConstants.GIT_TF_SHALLOW_DEPTH) > 1;

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    cloneTask.setDepth(10);

    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    // Load Git Repo
    Git git = new Git(repository);
    git.checkout().setName("master").call(); //$NON-NLS-1$

    // Verify Changeset 1
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file0.txt"), //$NON-NLS-1$
            "$/project/folder2/file0.txt", //$NON-NLS-1$
            1));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file0.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file0.txt", //$NON-NLS-1$
            1));

    // Verify Changeset 2
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file1.txt"), //$NON-NLS-1$
            "$/project/folder2/file1.txt", //$NON-NLS-1$
            2));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file1.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file1.txt", //$NON-NLS-1$
            2));

    // Verify Changeset 3
    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(new File(gitRepositoryPath, "folder2/file2.txt"), //$NON-NLS-1$
            "$/project/folder2/file2.txt", //$NON-NLS-1$
            3));

    assertTrue(mockVersionControlService.verifyFileContent(
            new File(gitRepositoryPath, "folder/nestedFolder/file2.txt"), //$NON-NLS-1$
            "$/project/folder/nestedFolder/file2.txt", //$NON-NLS-1$
            3));

    // Verify Git Repo configuration
    GitTFConfiguration gitRepoServerConfig = GitTFConfiguration.loadFrom(repository);

    assertEquals(gitRepoServerConfig.getServerURI(), projectCollectionURI);
    assertEquals(gitRepoServerConfig.getServerPath(), tfsPath);
    assertEquals(gitRepoServerConfig.getDeep(), defaultDeep);

    // Verify the number of commits
    Iterable<RevCommit> commits = git.log().call();

    assertNotNull(commits);

    int commitCounter = 3;
    for (RevCommit commit : commits) {
        assertEquals(commit.getFullMessage(), "comment" + Integer.toString(commitCounter)); //$NON-NLS-1$

        PersonIdent ownwer = commit.getAuthorIdent();
        assertEquals(ownwer.getEmailAddress(), "ownerName" + Integer.toString(commitCounter)); //$NON-NLS-1$
        assertEquals(ownwer.getName(), "ownerDisplayName" + Integer.toString(commitCounter)); //$NON-NLS-1$

        PersonIdent committer = commit.getCommitterIdent();
        assertEquals(committer.getEmailAddress(), "committerName" + Integer.toString(commitCounter)); //$NON-NLS-1$
        assertEquals(committer.getName(), "committerDisplayName" + Integer.toString(commitCounter)); //$NON-NLS-1$

        commitCounter--;
    }

    assertEquals(commitCounter, 0);

    // Verify the tags
    List<Ref> tags = git.tagList().call();
    assertEquals(3, tags.size());
}

From source file:com.microsoft.gittf.core.tasks.pendDiff.CheckinAnalysisChangeCollectionTest.java

License:Open Source License

private void initRepository() throws Exception {
    URI projectCollectionURI = new URI("http://fakeCollection:8080/tfs/DefaultCollection"); //$NON-NLS-1$
    String tfsPath = "$/project"; //$NON-NLS-1$
    String gitRepositoryPath = Util.getRepositoryFile(getName()).getAbsolutePath();

    final MockVersionControlService mockVersionControlService = new MockVersionControlService();

    mockVersionControlService.AddFile("$/project/root/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/file3.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file1.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file2.txt", 1); //$NON-NLS-1$
    mockVersionControlService.AddFile("$/project/root/parent/child/grandChild/greatGrandChild/file3.txt", 1); //$NON-NLS-1$

    Calendar date = Calendar.getInstance();
    date.set(2012, 11, 12, 18, 15);/* w w w .j a  va  2 s .c  o  m*/
    MockChangesetProperties changesetProperties = new MockChangesetProperties("ownerDisplayName", //$NON-NLS-1$
            "ownerName", //$NON-NLS-1$
            "committerDisplayName", //$NON-NLS-1$
            "committerName", //$NON-NLS-1$
            "comment", //$NON-NLS-1$
            date);

    mockVersionControlService.updateChangesetInformation(changesetProperties, 1);

    repository = RepositoryUtil.createNewRepository(gitRepositoryPath, false);

    CloneTask cloneTask = new CloneTask(projectCollectionURI, mockVersionControlService, tfsPath, repository);
    TaskStatus cloneTaskStatus = cloneTask.run(new NullTaskProgressMonitor());

    // Verify task completed without errors
    assertTrue(cloneTaskStatus.isOK());

    Git git = new Git(repository);
    initialCommit = git.log().call().iterator().next();

    // Verify the first commit exists
    assertFalse(ObjectId.zeroId().equals(initialCommit));
}

From source file:com.osbitools.ws.shared.prj.utils.GitUtils.java

License:Open Source License

public static ArrayList<RevCommit> getLogByFileName(Git git, String fname) throws WsSrvException {
    Iterable<RevCommit> log;
    try {/*from w  w  w .  j a v  a  2 s  .c om*/
        log = git.log().addPath(fname).call();
    } catch (GitAPIException e) {
        throw new WsSrvException(241, e);
    }

    ArrayList<RevCommit> res = new ArrayList<RevCommit>();
    for (RevCommit rev : log)
        res.add(rev);

    return res;
}

From source file:com.osbitools.ws.shared.prj.utils.GitUtils.java

License:Open Source License

public static ByteArrayOutputStream getRevEntityEx(Git git, String base, String name, String rev,
        IEntityUtils eut, boolean minified) throws WsSrvException {
    checkFile(base, name, eut.getExt());
    String fp = getLocalPath(name);

    Iterable<RevCommit> logs;

    try {//from  ww  w.j  ava2s.co  m
        logs = git.log().addPath(fp).call();
    } catch (GitAPIException e) {
        throw new WsSrvException(242, e);
    }

    RevCommit r = null;
    for (RevCommit rc : logs) {
        if (rc.getName().equals(rev)) {
            r = rc;
            break;
        }
    }

    if (r == null)
        throw new WsSrvException(243, "Revision " + rev + " not found for entry " + fp);

    // now try to find a specific file
    ByteArrayOutputStream entity = new ByteArrayOutputStream();
    TreeWalk tw = new TreeWalk(git.getRepository());

    try {
        tw.addTree(r.getTree());
        tw.setRecursive(true);
        tw.setFilter(PathFilter.create(fp));
        if (!tw.next())
            throw new WsSrvException(244, "Entry " + fp + " not found in revision " + rev);

        ObjectId objectId = tw.getObjectId(0);
        ObjectLoader loader = git.getRepository().open(objectId);

        // and then one can the loader to read the file
        loader.copyTo(entity);
    } catch (Exception e) {
        throw new WsSrvException(245, "Error retrieving revision " + rev + " for entry " + fp);
    }

    // ByteArrayInputStream in = new ByteArrayInputStream(dse.toByteArray());

    // return eut.get(in, name, minified);

    return entity;
}

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

public List<String> getSvnLogMessages(String Url, String userName, String Password, String repoType,
        String appDirName) throws PhrescoException {
    List<String> logMessages = new ArrayList<String>();
    if (repoType.equalsIgnoreCase(SVN)) {
        setupLibrary();// www  .ja va2 s.  c o m
        long startRevision = 0;
        long endRevision = -1; //HEAD (the latest) revision
        SVNRepository repository = null;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(Url));
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(userName,
                    Password);
            repository.setAuthenticationManager(authManager);
            Collection logEntries = null;
            logEntries = repository.log(new String[] { "" }, null, startRevision, endRevision, false, true);
            for (Iterator entries = logEntries.iterator(); entries.hasNext();) {
                SVNLogEntry logEntry = (SVNLogEntry) entries.next();
                logMessages.add(logEntry.getMessage());
            }
        } catch (SVNException e) {
            throw new PhrescoException(e);
        } finally {
            if (repository != null) {
                repository.closeSession();
            }
        }
    } else if (repoType.equalsIgnoreCase(GIT)) {
        try {
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            String dotGitDir = Utility.getProjectHome() + appDirName + File.separator + DOT_GIT;
            File dotGitDirectory = new File(dotGitDir);
            if (!dotGitDirectory.exists()) {
                dotGitDir = Utility.getProjectHome() + appDirName + File.separator + appDirName + File.separator
                        + DOT_GIT;
                dotGitDirectory = new File(dotGitDir);
            }
            if (!dotGitDir.isEmpty()) {
                Repository repo = builder.setGitDir(dotGitDirectory).setMustExist(true).build();
                Git git = new Git(repo);
                Iterable<RevCommit> log = git.log().call();
                for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
                    RevCommit rev = iterator.next();
                    if (!rev.getFullMessage().isEmpty()) {
                        logMessages.add(rev.getFullMessage());
                    }
                }
                repo.close();
            }
        } catch (GitAPIException ge) {
            throw new PhrescoException(ge);
        } catch (IOException ioe) {
            throw new PhrescoException(ioe);
        }
    }
    return logMessages;
}