View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. 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  
11  package org.eclipse.jgit.pgm.debug;
12  
13  import java.io.FileInputStream;
14  import java.io.IOException;
15  
16  import org.eclipse.jgit.internal.storage.io.BlockSource;
17  import org.eclipse.jgit.internal.storage.reftable.RefCursor;
18  import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
19  import org.eclipse.jgit.lib.ObjectId;
20  import org.eclipse.jgit.lib.Ref;
21  import org.eclipse.jgit.pgm.Command;
22  import org.eclipse.jgit.pgm.TextBuiltin;
23  import org.kohsuke.args4j.Argument;
24  
25  @Command
26  class ReadReftable extends TextBuiltin {
27  	@Argument(index = 0)
28  	private String input;
29  
30  	@Argument(index = 1, required = false)
31  	private String ref;
32  
33  	/** {@inheritDoc} */
34  	@Override
35  	protected void run() throws Exception {
36  		try (FileInputStream in = new FileInputStream(input);
37  				BlockSource src = BlockSource.from(in);
38  				ReftableReader reader = new ReftableReader(src)) {
39  			try (RefCursor rc = ref != null
40  					? reader.seekRefsWithPrefix(ref)
41  					: reader.allRefs()) {
42  				while (rc.next()) {
43  					write(rc.getRef());
44  				}
45  			}
46  		}
47  	}
48  
49  	private void write(Ref r) throws IOException {
50  		if (r.isSymbolic()) {
51  			outw.println(r.getTarget().getName() + '\t' + r.getName());
52  			return;
53  		}
54  
55  		ObjectId id1 = r.getObjectId();
56  		if (id1 != null) {
57  			outw.println(id1.name() + '\t' + r.getName());
58  		}
59  
60  		ObjectId id2 = r.getPeeledObjectId();
61  		if (id2 != null) {
62  			outw.println('^' + id2.name());
63  		}
64  	}
65  }