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