View Javadoc
1   /*
2    * Copyright (C) 2009, 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.TreeSet;
17  
18  import org.eclipse.jgit.api.Git;
19  import org.eclipse.jgit.api.LsRemoteCommand;
20  import org.eclipse.jgit.api.errors.GitAPIException;
21  import org.eclipse.jgit.lib.AnyObjectId;
22  import org.eclipse.jgit.lib.Ref;
23  import org.kohsuke.args4j.Argument;
24  import org.kohsuke.args4j.Option;
25  
26  @Command(common = true, usage = "usage_LsRemote")
27  class LsRemote extends TextBuiltin {
28  	@Option(name = "--heads", usage = "usage_lsRemoteHeads")
29  	private boolean heads;
30  
31  	@Option(name = "--tags", usage = "usage_lsRemoteTags", aliases = { "-t" })
32  	private boolean tags;
33  
34  	@Option(name = "--timeout", metaVar = "metaVar_service", usage = "usage_abortConnectionIfNoActivity")
35  	int timeout = -1;
36  
37  	@Option(name = "--symref", usage = "usage_lsRemoteSymref")
38  	private boolean symref;
39  
40  	@Argument(index = 0, metaVar = "metaVar_uriish", required = true)
41  	private String remote;
42  
43  	/** {@inheritDoc} */
44  	@Override
45  	protected void run() {
46  		LsRemoteCommand command = Git.lsRemoteRepository().setRemote(remote)
47  				.setTimeout(timeout).setHeads(heads).setTags(tags);
48  		TreeSet<Ref> refs = new TreeSet<>(
49  				(Ref r1, Ref r2) -> r1.getName().compareTo(r2.getName()));
50  		try {
51  			refs.addAll(command.call());
52  			for (Ref r : refs) {
53  				if (symref && r.isSymbolic()) {
54  					show(r.getTarget(), r.getName());
55  				}
56  				show(r.getObjectId(), r.getName());
57  				if (r.getPeeledObjectId() != null) {
58  					show(r.getPeeledObjectId(), r.getName() + "^{}"); //$NON-NLS-1$
59  				}
60  			}
61  		} catch (GitAPIException | IOException e) {
62  			throw die(e.getMessage(), e);
63  		}
64  	}
65  
66  	/** {@inheritDoc} */
67  	@Override
68  	protected boolean requiresRepository() {
69  		return false;
70  	}
71  
72  	private void show(AnyObjectId id, String name)
73  			throws IOException {
74  		outw.print(id.name());
75  		outw.print('\t');
76  		outw.print(name);
77  		outw.println();
78  	}
79  
80  	private void show(Ref ref, String name)
81  			throws IOException {
82  		outw.print("ref: "); //$NON-NLS-1$
83  		outw.print(ref.getName());
84  		outw.print('\t');
85  		outw.print(name);
86  		outw.println();
87  	}
88  }