View Javadoc
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  
14  package org.eclipse.jgit.pgm.debug;
15  
16  import static java.lang.Integer.valueOf;
17  
18  import java.time.Instant;
19  import java.time.ZoneId;
20  import java.time.format.DateTimeFormatter;
21  import java.util.Locale;
22  
23  import org.eclipse.jgit.dircache.DirCache;
24  import org.eclipse.jgit.dircache.DirCacheEntry;
25  import org.eclipse.jgit.lib.FileMode;
26  import org.eclipse.jgit.pgm.Command;
27  import org.eclipse.jgit.pgm.TextBuiltin;
28  import org.kohsuke.args4j.Option;
29  
30  @Command(usage = "usage_ShowDirCache")
31  class ShowDirCache extends TextBuiltin {
32  
33  	@Option(name = "--millis", aliases = { "-m" }, usage = "usage_showTimeInMilliseconds")
34  	private boolean millis = false;
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	protected void run() throws Exception {
39  		final DateTimeFormatter fmt = DateTimeFormatter
40  				.ofPattern("yyyy-MM-dd,HH:mm:ss.nnnnnnnnn") //$NON-NLS-1$
41  				.withLocale(Locale.getDefault())
42  				.withZone(ZoneId.systemDefault());
43  
44  		final DirCache cache = db.readDirCache();
45  		for (int i = 0; i < cache.getEntryCount(); i++) {
46  			final DirCacheEntry ent = cache.getEntry(i);
47  			final FileMode mode = FileMode.fromBits(ent.getRawMode());
48  			final int len = ent.getLength();
49  			Instant mtime = ent.getLastModifiedInstant();
50  			final int stage = ent.getStage();
51  
52  			outw.print(mode);
53  			outw.format(" %6d", valueOf(len)); //$NON-NLS-1$
54  			outw.print(' ');
55  			if (millis) {
56  				outw.print(mtime.toEpochMilli());
57  			} else {
58  				outw.print(fmt.format(mtime));
59  			}
60  			outw.print(' ');
61  			outw.print(ent.getObjectId().name());
62  			outw.print(' ');
63  			outw.print(stage);
64  			outw.print('\t');
65  			outw.print(ent.getPathString());
66  			outw.println();
67  		}
68  	}
69  }