aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Reflog.java
blob: 46485cc5efaf34e955387ed286bd9750ff9e4d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Copyright (C) 2012, Tomasz Zarna <tomasz.zarna@tasktop.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.pgm;

import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;

import java.io.IOException;
import java.util.Collection;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ReflogCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.Repository;
import org.kohsuke.args4j.Argument;

@Command(common = true, usage = "usage_manageReflogInformation")
class Reflog extends TextBuiltin {

	@Argument(metaVar = "metaVar_ref")
	private String ref;

	@Override
	protected void run() {
		try (Git git = new Git(db)) {
			ReflogCommand cmd = git.reflog();
			if (ref != null)
				cmd.setRef(ref);
			Collection<ReflogEntry> entries = cmd.call();
			int i = 0;
			for (ReflogEntry entry : entries) {
				outw.println(toString(entry, i++));
			}
		} catch (GitAPIException | IOException e) {
			throw die(e.getMessage(), e);
		}
	}

	private String toString(ReflogEntry entry, int i) {
		final StringBuilder s = new StringBuilder();
		s.append(entry.getNewId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
				.name());
		s.append(" "); //$NON-NLS-1$
		s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref));
		s.append("@{" + i + "}:"); //$NON-NLS-1$ //$NON-NLS-2$
		s.append(" "); //$NON-NLS-1$
		s.append(entry.getComment());
		return s.toString();
	}
}