ReflogCommand.java

  1. /*
  2.  * Copyright (C) 2011, Chris Aniszczyk <caniszczyk@gmail.com> 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.Collection;

  14. import org.eclipse.jgit.api.errors.GitAPIException;
  15. import org.eclipse.jgit.api.errors.InvalidRefNameException;
  16. import org.eclipse.jgit.api.errors.RefNotFoundException;
  17. import org.eclipse.jgit.internal.JGitText;
  18. import org.eclipse.jgit.lib.Constants;
  19. import org.eclipse.jgit.lib.ReflogEntry;
  20. import org.eclipse.jgit.lib.ReflogReader;
  21. import org.eclipse.jgit.lib.Repository;

  22. /**
  23.  * The reflog command
  24.  *
  25.  * @see <a
  26.  *      href="http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html"
  27.  *      >Git documentation about reflog</a>
  28.  */
  29. public class ReflogCommand extends GitCommand<Collection<ReflogEntry>> {

  30.     private String ref = Constants.HEAD;

  31.     /**
  32.      * Constructor for ReflogCommand.
  33.      *
  34.      * @param repo
  35.      *            the {@link org.eclipse.jgit.lib.Repository}
  36.      */
  37.     public ReflogCommand(Repository repo) {
  38.         super(repo);
  39.     }

  40.     /**
  41.      * The ref used for the reflog operation. If no ref is set, the default
  42.      * value of HEAD will be used.
  43.      *
  44.      * @param ref
  45.      *            the name of the {@code Ref} to log
  46.      * @return {@code this}
  47.      */
  48.     public ReflogCommand setRef(String ref) {
  49.         checkCallable();
  50.         this.ref = ref;
  51.         return this;
  52.     }

  53.     /**
  54.      * {@inheritDoc}
  55.      * <p>
  56.      * Run the reflog command
  57.      */
  58.     @Override
  59.     public Collection<ReflogEntry> call() throws GitAPIException,
  60.             InvalidRefNameException {
  61.         checkCallable();

  62.         try {
  63.             ReflogReader reader = repo.getReflogReader(ref);
  64.             if (reader == null)
  65.                 throw new RefNotFoundException(MessageFormat.format(
  66.                         JGitText.get().refNotResolved, ref));
  67.             return reader.getReverseEntries();
  68.         } catch (IOException e) {
  69.             throw new InvalidRefNameException(MessageFormat.format(
  70.                     JGitText.get().cannotRead, ref), e);
  71.         }
  72.     }

  73. }