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

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

Introduction

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

Prototype

public StashListCommand stashList() 

Source Link

Document

Return a command object used to list stashed commits

Usage

From source file:org.eclipse.orion.server.git.servlets.GitStashHandlerV1.java

License:Open Source License

/**
 * Helper method extracting the StashRef for the stash commit rev
 * /*from www.j a  va2 s  .  c o m*/
 * @param git
 *            Git handler object
 * @param stashRev
 *            Git commit name
 * @return StashRef wrapper object or <code>null</code> if the given commit is not present in the stash
 * @throws InvalidRefNameException
 * @throws GitAPIException
 */
protected StashRef getStashRef(Git git, String stashRev) throws InvalidRefNameException, GitAPIException {

    if (stashRev == null)
        return null;

    StashListCommand stashList = git.stashList();
    Collection<RevCommit> stashedRefsCollection = stashList.call();

    int k = 0;
    for (RevCommit rev : stashedRefsCollection)
        if (stashRev.equals(rev.getName()))
            return new StashRef(k);
        else
            ++k;

    return null;
}

From source file:org.eclipse.orion.server.git.servlets.GitStashHandlerV1.java

License:Open Source License

/**
 * Helper method returning whether the stash is empty or not
 * //from ww  w . j a  v a  2  s.  co  m
 * @param git
 *            Git handler object
 * @return <code>true</code> iff the git stash is empty
 * @throws InvalidRefNameException
 * @throws GitAPIException
 */
protected boolean isStashEmpty(Git git) throws InvalidRefNameException, GitAPIException {
    StashListCommand stashList = git.stashList();
    Collection<RevCommit> stashedRefsCollection = stashList.call();
    return stashedRefsCollection.isEmpty();
}

From source file:org.eclipse.orion.server.git.servlets.GitStashHandlerV1.java

License:Open Source License

@Override
protected boolean handleGet(RequestInfo requestInfo) throws ServletException {

    HttpServletRequest request = requestInfo.request;
    HttpServletResponse response = requestInfo.response;
    Repository db = requestInfo.db;/* www . jav a  2 s  . c  o  m*/

    int page = request.getParameter("page") != null ? new Integer(request.getParameter("page")).intValue() : 1; //$NON-NLS-1$ //$NON-NLS-2$
    int pageSize = request.getParameter("pageSize") != null //$NON-NLS-1$
            ? new Integer(request.getParameter("pageSize")).intValue() //$NON-NLS-1$
            : PAGE_SIZE;
    String messageFilter = request.getParameter("filter"); //$NON-NLS-1$
    try {

        URI baseLocation = getURI(request);
        URI cloneLocation = BaseToCloneConverter.getCloneLocation(baseLocation, BaseToCloneConverter.COMMIT);

        Git git = new Git(db);
        StashListCommand stashList = git.stashList();
        Collection<RevCommit> stashedRefsCollection = stashList.call();

        StashPage stashPage = new StashPage(cloneLocation, db, stashedRefsCollection, page, pageSize,
                messageFilter);
        OrionServlet.writeJSONResponse(request, response, stashPage.toJSON());
        return true;

    } catch (Exception ex) {
        String msg = "An error occured for stash command.";
        return statusHandler.handleRequest(request, response,
                new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, ex));
    }
}

From source file:org.jboss.forge.addon.git.GitUtilsTest.java

License:Open Source License

@Test
public void shouldStashAndApplyCommit() throws Exception {
    boolean isCreated = false;
    String[] commitMsgs = { "initial commit", "First commit" };

    Project project = projectFactory.createTempProject();
    Git repo = gitUtils.init(project.getRoot().reify(DirectoryResource.class));

    gitUtils.addAll(repo);// w w w .java 2  s . c  o  m
    gitUtils.commitAll(repo, commitMsgs[0]);

    FileResource<?> file1 = project.getRoot().getChild("test.txt").reify(FileResource.class);
    isCreated = file1.createNewFile();
    Assert.assertTrue("file 1 was not created", isCreated);
    file1.setContents("Foo bar baz contents");

    gitUtils.addAll(repo);
    gitUtils.stashCreate(repo);

    Assert.assertTrue("should contain one stash", repo.stashList().call().iterator().hasNext());

    gitUtils.stashApply(repo);

    gitUtils.addAll(repo);
    gitUtils.commit(repo, commitMsgs[1]);

    gitUtils.stashDrop(repo);

    List<String> logs = gitUtils.getLogForCurrentBranch(repo);
    Collections.reverse(logs); // git-log shows logs in DESC order

    Assert.assertNotNull("log should not be null", logs);
    Assert.assertEquals("log should contain two items", 2, logs.size());
    Assert.assertEquals("commit messages should be the same", commitMsgs[0], logs.get(0));
    Assert.assertEquals("commit messages should be the same", commitMsgs[1], logs.get(1));

    Assert.assertFalse("should contain no stashes", repo.stashList().call().iterator().hasNext());
}

From source file:org.jboss.forge.git.GitUtilsTest.java

License:Open Source License

@Test
public void shouldStashAndApplyCommit() throws Exception {
    boolean isCreated = false;
    String[] commitMsgs = { "initial commit", "First commit" };

    Project project = initializeJavaProject();
    Git repo = GitUtils.init(project.getProjectRoot());

    GitUtils.addAll(repo);/*from  w ww  .  j a va 2  s .co m*/
    GitUtils.commitAll(repo, commitMsgs[0]);

    FileResource<?> file1 = project.getProjectRoot().getChild("test.txt").reify(FileResource.class);
    isCreated = file1.createNewFile();
    Assert.assertTrue("file 1 was not created", isCreated);
    file1.setContents("Foo bar baz contents");

    GitUtils.addAll(repo);
    GitUtils.stashCreate(repo);

    Assert.assertTrue("should contain one stash", repo.stashList().call().iterator().hasNext());

    GitUtils.stashApply(repo);

    GitUtils.addAll(repo);
    GitUtils.commit(repo, commitMsgs[1]);

    GitUtils.stashDrop(repo);

    List<String> logs = GitUtils.getLogForCurrentBranch(repo);
    Collections.reverse(logs); // git-log shows logs in DESC order

    Assert.assertNotNull("log should not be null", logs);
    Assert.assertEquals("log should contain two items", 2, logs.size());
    Assert.assertEquals("commit messages should be the same", commitMsgs[0], logs.get(0));
    Assert.assertEquals("commit messages should be the same", commitMsgs[1], logs.get(1));

    Assert.assertFalse("should contain no stashes", repo.stashList().call().iterator().hasNext());
}