You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LsRemote.java 2.4KB

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