View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.pgm;
14  
15  import java.io.IOException;
16  import java.util.List;
17  
18  import org.eclipse.jgit.lib.AnyObjectId;
19  import org.eclipse.jgit.lib.Ref;
20  import org.eclipse.jgit.lib.RefComparator;
21  
22  @Command(usage = "usage_ShowRef")
23  class ShowRef extends TextBuiltin {
24  	/** {@inheritDoc} */
25  	@Override
26  	protected void run() {
27  		try {
28  			for (Ref r : getSortedRefs()) {
29  				show(r.getObjectId(), r.getName());
30  				if (r.getPeeledObjectId() != null) {
31  					show(r.getPeeledObjectId(), r.getName() + "^{}"); //$NON-NLS-1$
32  				}
33  			}
34  		} catch (IOException e) {
35  			throw die(e.getMessage(), e);
36  		}
37  	}
38  
39  	private Iterable<Ref> getSortedRefs() throws IOException {
40  		List<Ref> all = db.getRefDatabase().getRefs();
41  		// TODO(jrn) check if we can reintroduce fast-path by e.g. implementing
42  		// SortedList
43  		return RefComparator.sort(all);
44  	}
45  
46  	private void show(AnyObjectId id, String name)
47  			throws IOException {
48  		outw.print(id.name());
49  		outw.print('\t');
50  		outw.print(name);
51  		outw.println();
52  	}
53  }