StashListCommand.java

  1. /*
  2.  * Copyright (C) 2011, GitHub Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.api;

  11. import java.io.IOException;
  12. import java.text.MessageFormat;
  13. import java.util.ArrayList;
  14. import java.util.Collection;
  15. import java.util.Collections;
  16. import java.util.List;

  17. import org.eclipse.jgit.api.errors.GitAPIException;
  18. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  19. import org.eclipse.jgit.api.errors.JGitInternalException;
  20. import org.eclipse.jgit.internal.JGitText;
  21. import org.eclipse.jgit.lib.Constants;
  22. import org.eclipse.jgit.lib.ReflogEntry;
  23. import org.eclipse.jgit.lib.Repository;
  24. import org.eclipse.jgit.revwalk.RevCommit;
  25. import org.eclipse.jgit.revwalk.RevWalk;

  26. /**
  27.  * Command class to list the stashed commits in a repository.
  28.  *
  29.  * @see <a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html"
  30.  *      >Git documentation about Stash</a>
  31.  */
  32. public class StashListCommand extends GitCommand<Collection<RevCommit>> {

  33.     /**
  34.      * Create a new stash list command
  35.      *
  36.      * @param repo a {@link org.eclipse.jgit.lib.Repository} object.
  37.      */
  38.     public StashListCommand(Repository repo) {
  39.         super(repo);
  40.     }

  41.     /** {@inheritDoc} */
  42.     @Override
  43.     public Collection<RevCommit> call() throws GitAPIException,
  44.             InvalidRefNameException {
  45.         checkCallable();

  46.         try {
  47.             if (repo.exactRef(Constants.R_STASH) == null)
  48.                 return Collections.emptyList();
  49.         } catch (IOException e) {
  50.             throw new InvalidRefNameException(MessageFormat.format(
  51.                     JGitText.get().cannotRead, Constants.R_STASH), e);
  52.         }

  53.         final ReflogCommand refLog = new ReflogCommand(repo);
  54.         refLog.setRef(Constants.R_STASH);
  55.         final Collection<ReflogEntry> stashEntries = refLog.call();
  56.         if (stashEntries.isEmpty())
  57.             return Collections.emptyList();

  58.         final List<RevCommit> stashCommits = new ArrayList<>(
  59.                 stashEntries.size());
  60.         try (RevWalk walk = new RevWalk(repo)) {
  61.             for (ReflogEntry entry : stashEntries) {
  62.                 try {
  63.                     stashCommits.add(walk.parseCommit(entry.getNewId()));
  64.                 } catch (IOException e) {
  65.                     throw new JGitInternalException(MessageFormat.format(
  66.                             JGitText.get().cannotReadCommit, entry.getNewId()),
  67.                             e);
  68.                 }
  69.             }
  70.         }
  71.         return stashCommits;
  72.     }
  73. }