View Javadoc
1   /*
2    * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.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.pgm;
11  
12  import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
13  
14  import java.io.IOException;
15  import java.util.Collection;
16  
17  import org.eclipse.jgit.api.Git;
18  import org.eclipse.jgit.api.ReflogCommand;
19  import org.eclipse.jgit.api.errors.GitAPIException;
20  import org.eclipse.jgit.lib.Constants;
21  import org.eclipse.jgit.lib.ReflogEntry;
22  import org.eclipse.jgit.lib.Repository;
23  import org.kohsuke.args4j.Argument;
24  
25  @Command(common = true, usage = "usage_manageReflogInformation")
26  class Reflog extends TextBuiltin {
27  
28  	@Argument(metaVar = "metaVar_ref")
29  	private String ref;
30  
31  	/** {@inheritDoc} */
32  	@Override
33  	protected void run() {
34  		try (Git git = new Git(db)) {
35  			ReflogCommand cmd = git.reflog();
36  			if (ref != null)
37  				cmd.setRef(ref);
38  			Collection<ReflogEntry> entries = cmd.call();
39  			int i = 0;
40  			for (ReflogEntry entry : entries) {
41  				outw.println(toString(entry, i++));
42  			}
43  		} catch (GitAPIException | IOException e) {
44  			throw die(e.getMessage(), e);
45  		}
46  	}
47  
48  	private String toString(ReflogEntry entry, int i) {
49  		final StringBuilder s = new StringBuilder();
50  		s.append(entry.getNewId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
51  				.name());
52  		s.append(" "); //$NON-NLS-1$
53  		s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref));
54  		s.append("@{" + i + "}:"); //$NON-NLS-1$ //$NON-NLS-2$
55  		s.append(" "); //$NON-NLS-1$
56  		s.append(entry.getComment());
57  		return s.toString();
58  	}
59  }