View Javadoc
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  
12  import java.io.IOException;
13  import java.text.MessageFormat;
14  import java.util.Collection;
15  
16  import org.eclipse.jgit.api.errors.GitAPIException;
17  import org.eclipse.jgit.api.errors.InvalidRefNameException;
18  import org.eclipse.jgit.api.errors.RefNotFoundException;
19  import org.eclipse.jgit.internal.JGitText;
20  import org.eclipse.jgit.lib.Constants;
21  import org.eclipse.jgit.lib.ReflogEntry;
22  import org.eclipse.jgit.lib.ReflogReader;
23  import org.eclipse.jgit.lib.Repository;
24  
25  /**
26   * The reflog command
27   *
28   * @see <a
29   *      href="http://www.kernel.org/pub/software/scm/git/docs/git-reflog.html"
30   *      >Git documentation about reflog</a>
31   */
32  public class ReflogCommand extends GitCommand<Collection<ReflogEntry>> {
33  
34  	private String ref = Constants.HEAD;
35  
36  	/**
37  	 * Constructor for ReflogCommand.
38  	 *
39  	 * @param repo
40  	 *            the {@link org.eclipse.jgit.lib.Repository}
41  	 */
42  	public ReflogCommand(Repository repo) {
43  		super(repo);
44  	}
45  
46  	/**
47  	 * The ref used for the reflog operation. If no ref is set, the default
48  	 * value of HEAD will be used.
49  	 *
50  	 * @param ref
51  	 *            the name of the {@code Ref} to log
52  	 * @return {@code this}
53  	 */
54  	public ReflogCommand setRef(String ref) {
55  		checkCallable();
56  		this.ref = ref;
57  		return this;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 * <p>
63  	 * Run the reflog command
64  	 */
65  	@Override
66  	public Collection<ReflogEntry> call() throws GitAPIException,
67  			InvalidRefNameException {
68  		checkCallable();
69  
70  		try {
71  			ReflogReader reader = repo.getReflogReader(ref);
72  			if (reader == null)
73  				throw new RefNotFoundException(MessageFormat.format(
74  						JGitText.get().refNotResolved, ref));
75  			return reader.getReverseEntries();
76  		} catch (IOException e) {
77  			throw new InvalidRefNameException(MessageFormat.format(
78  					JGitText.get().cannotRead, ref), e);
79  		}
80  	}
81  
82  }