ReadReftable.java

  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. package org.eclipse.jgit.pgm.debug;

  11. import java.io.FileInputStream;
  12. import java.io.IOException;

  13. import org.eclipse.jgit.internal.storage.io.BlockSource;
  14. import org.eclipse.jgit.internal.storage.reftable.RefCursor;
  15. import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
  16. import org.eclipse.jgit.lib.ObjectId;
  17. import org.eclipse.jgit.lib.Ref;
  18. import org.eclipse.jgit.pgm.Command;
  19. import org.eclipse.jgit.pgm.TextBuiltin;
  20. import org.kohsuke.args4j.Argument;

  21. @Command
  22. class ReadReftable extends TextBuiltin {
  23.     @Argument(index = 0)
  24.     private String input;

  25.     @Argument(index = 1, required = false)
  26.     private String ref;

  27.     /** {@inheritDoc} */
  28.     @Override
  29.     protected void run() throws Exception {
  30.         try (FileInputStream in = new FileInputStream(input);
  31.                 BlockSource src = BlockSource.from(in);
  32.                 ReftableReader reader = new ReftableReader(src)) {
  33.             try (RefCursor rc = ref != null
  34.                     ? reader.seekRefsWithPrefix(ref)
  35.                     : reader.allRefs()) {
  36.                 while (rc.next()) {
  37.                     write(rc.getRef());
  38.                 }
  39.             }
  40.         }
  41.     }

  42.     private void write(Ref r) throws IOException {
  43.         if (r.isSymbolic()) {
  44.             outw.println(r.getTarget().getName() + '\t' + r.getName());
  45.             return;
  46.         }

  47.         ObjectId id1 = r.getObjectId();
  48.         if (id1 != null) {
  49.             outw.println(id1.name() + '\t' + r.getName());
  50.         }

  51.         ObjectId id2 = r.getPeeledObjectId();
  52.         if (id2 != null) {
  53.             outw.println('^' + id2.name());
  54.         }
  55.     }
  56. }