ShowDirCache.java

  1. /*
  2.  * Copyright (C) 2008, Google Inc.
  3.  * Copyright (C) 2008, Jonas Fonseca <fonseca@diku.dk>
  4.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5.  * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com> and others
  6.  *
  7.  * This program and the accompanying materials are made available under the
  8.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  9.  * https://www.eclipse.org/org/documents/edl-v10.php.
  10.  *
  11.  * SPDX-License-Identifier: BSD-3-Clause
  12.  */

  13. package org.eclipse.jgit.pgm.debug;

  14. import static java.lang.Integer.valueOf;

  15. import java.time.Instant;
  16. import java.time.ZoneId;
  17. import java.time.format.DateTimeFormatter;
  18. import java.util.Locale;

  19. import org.eclipse.jgit.dircache.DirCache;
  20. import org.eclipse.jgit.dircache.DirCacheEntry;
  21. import org.eclipse.jgit.lib.FileMode;
  22. import org.eclipse.jgit.pgm.Command;
  23. import org.eclipse.jgit.pgm.TextBuiltin;
  24. import org.kohsuke.args4j.Option;

  25. @Command(usage = "usage_ShowDirCache")
  26. class ShowDirCache extends TextBuiltin {

  27.     @Option(name = "--millis", aliases = { "-m" }, usage = "usage_showTimeInMilliseconds")
  28.     private boolean millis = false;

  29.     /** {@inheritDoc} */
  30.     @Override
  31.     protected void run() throws Exception {
  32.         final DateTimeFormatter fmt = DateTimeFormatter
  33.                 .ofPattern("yyyy-MM-dd,HH:mm:ss.nnnnnnnnn") //$NON-NLS-1$
  34.                 .withLocale(Locale.getDefault())
  35.                 .withZone(ZoneId.systemDefault());

  36.         final DirCache cache = db.readDirCache();
  37.         for (int i = 0; i < cache.getEntryCount(); i++) {
  38.             final DirCacheEntry ent = cache.getEntry(i);
  39.             final FileMode mode = FileMode.fromBits(ent.getRawMode());
  40.             final int len = ent.getLength();
  41.             Instant mtime = ent.getLastModifiedInstant();
  42.             final int stage = ent.getStage();

  43.             outw.print(mode);
  44.             outw.format(" %6d", valueOf(len)); //$NON-NLS-1$
  45.             outw.print(' ');
  46.             if (millis) {
  47.                 outw.print(mtime.toEpochMilli());
  48.             } else {
  49.                 outw.print(fmt.format(mtime));
  50.             }
  51.             outw.print(' ');
  52.             outw.print(ent.getObjectId().name());
  53.             outw.print(' ');
  54.             outw.print(stage);
  55.             outw.print('\t');
  56.             outw.print(ent.getPathString());
  57.             outw.println();
  58.         }
  59.     }
  60. }