git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 9/9] Add debug-show-commands to display the command table

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jul 25, 2008, 19:46 UTC
Message-ID
<1217015167-4680-10-git-send-email-spearce@spearce.org>
In-Reply-To
<1217015167-4680-9-git-send-email-spearce@spearce.org>
This can be useful to debug the command catalog.
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../services/org.spearce.jgit.pgm.TextBuiltin      |    2 +
 .../src/org/spearce/jgit/pgm/CommandRef.java       |    7 ++
 .../org/spearce/jgit/pgm/debug/ShowCommands.java   |   78 ++++++++++++++++++++
 3 files changed, 87 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/ShowCommands.java
diff --git a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
index 69ef046..1ff5a30 100644
--- a/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
+++ b/org.spearce.jgit.pgm/src/META-INF/services/org.spearce.jgit.pgm.TextBuiltin
@@ -10,3 +10,5 @@ org.spearce.jgit.pgm.Push
 org.spearce.jgit.pgm.RevList
 org.spearce.jgit.pgm.ShowRev
 org.spearce.jgit.pgm.Tag
+
+org.spearce.jgit.pgm.debug.ShowCommands
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/CommandRef.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/CommandRef.java
index 40400a7..8bf784b 100644
--- a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/CommandRef.java
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/CommandRef.java
@@ -120,6 +120,13 @@ public class CommandRef {
 	}
 
 	/**
+	 * @return loader for {@link #getImplementationClassName()}.
+	 */
+	public ClassLoader getImplementationClassLoader() {
+		return impl.getClassLoader();
+	}
+
+	/**
 	 * @return a new instance of the command implementation.
 	 */
 	public TextBuiltin create() {
diff --git a/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/ShowCommands.java b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/ShowCommands.java
new file mode 100644
index 0000000..64ecf7f
--- /dev/null
+++ b/org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/debug/ShowCommands.java
@@ -0,0 +1,78 @@
+package org.spearce.jgit.pgm.debug;
+
+import java.net.URL;
+
+import org.kohsuke.args4j.Option;
+import org.spearce.jgit.pgm.Command;
+import org.spearce.jgit.pgm.CommandCatalog;
+import org.spearce.jgit.pgm.CommandRef;
+import org.spearce.jgit.pgm.TextBuiltin;
+
+@Command(usage = "Display a list of all registered jgit commands")
+class ShowCommands extends TextBuiltin {
+	@Option(name = "--pretty", usage = "alter the detail shown")
+	private Format pretty = Format.USAGE;
+
+	@Override
+	protected void run() throws Exception {
+		final CommandRef[] list = CommandCatalog.all();
+
+		int width = 0;
+		for (final CommandRef c : list)
+			width = Math.max(width, c.getName().length());
+		width += 2;
+
+		for (final CommandRef c : list) {
+			System.err.print(c.isCommon() ? '*' : ' ');
+			System.err.print(' ');
+
+			System.err.print(c.getName());
+			for (int i = c.getName().length(); i < width; i++)
+				System.err.print(' ');
+
+			pretty.print(c);
+			System.err.println();
+		}
+		System.err.println();
+	}
+
+	static enum Format {
+		/** */
+		USAGE {
+			void print(final CommandRef c) {
+				System.err.print(c.getUsage());
+			}
+		},
+
+		/** */
+		CLASSES {
+			void print(final CommandRef c) {
+				System.err.print(c.getImplementationClassName());
+			}
+		},
+
+		/** */
+		URLS {
+			void print(final CommandRef c) {
+				final ClassLoader ldr = c.getImplementationClassLoader();
+
+				String cn = c.getImplementationClassName();
+				cn = cn.replace('.', '/') + ".class";
+
+				final URL url = ldr.getResource(cn);
+				if (url == null) {
+					System.err.print("!! NOT FOUND !!");
+					return;
+				}
+
+				String rn = url.toExternalForm();
+				if (rn.endsWith(cn))
+					rn = rn.substring(0, rn.length() - cn.length());
+
+				System.err.print(rn);
+			}
+		};
+
+		abstract void print(CommandRef c);
+	}
+}
-- 
1.6.0.rc0.182.gb96c7
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 10 of 12 in “List commonly used (or recognized) commands”
  1. 0/9 List commonly used (or recognized) commandsShawn O. Pearce, Jul 25, 2008
  2. 1/9 Switch jgit.pgm to J2SE-1.5 execution environmentShawn O. Pearce, Jul 25, 2008
  3. 2/9 Remove unnecessary duplicate if (help) test inside TextBuiltinShawn O. Pearce, Jul 25, 2008
  4. 3/9 Create an optional documentation annotation for TextBuiltinShawn O. Pearce, Jul 25, 2008
  5. 4/9 Create a lightweight registration wrapper for TextBuiltinShawn O. Pearce, Jul 25, 2008
  6. 5/9 Create a catalog of CommandRefs for lookup and enumerationShawn O. Pearce, Jul 25, 2008
  7. 6/9 Document some common commands with the new Command annotationShawn O. Pearce, Jul 25, 2008
  8. 7/9 Include commonly used commands in main help outputShawn O. Pearce, Jul 25, 2008
  9. 8/9 Refactor SubcommandHandler to use CommandCatalog instead of reflectionShawn O. Pearce, Jul 25, 2008
  10. 9/9 Add debug-show-commands to display the command tableShawn O. Pearce, Jul 25, 2008
  11. 5/9 Create a catalog of CommandRefs for lookup and enumerationShawn O. Pearce, Jul 25, 2008
  12. Shawn O. PearceJul 25, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.