summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/transport
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2014-03-21 11:47:30 -0400
committerJames Moger <james.moger@gitblit.com>2014-04-10 18:58:09 -0400
commit8c68c71374bc6ffec7d765c83289da09ebd59189 (patch)
tree71fec9092cf158734df634c808f64a781f757d3b /src/main/java/com/gitblit/transport
parent7569e8f542ffcc049797f0cf5b79562d5852fb30 (diff)
downloadgitblit-8c68c71374bc6ffec7d765c83289da09ebd59189.tar.gz
gitblit-8c68c71374bc6ffec7d765c83289da09ebd59189.zip
Hide dispatcher from usage() if it has no visible commands or visible nested dispatchers
Diffstat (limited to 'src/main/java/com/gitblit/transport')
-rw-r--r--src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java43
1 files changed, 35 insertions, 8 deletions
diff --git a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
index 76012a21..4629bcc5 100644
--- a/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
+++ b/src/main/java/com/gitblit/transport/ssh/commands/DispatchCommand.java
@@ -109,6 +109,12 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
log.debug("registering {} dispatcher", meta.name());
try {
dispatcher.registerCommands(user);
+ if (dispatcher.commands.isEmpty() && dispatcher.dispatchers.isEmpty()) {
+ // exclude because there are no commands available to the user
+ log.debug(MessageFormat.format("excluding dispatcher {0} for {1}", meta.name(), user.username));
+ return;
+ }
+
dispatchers.put(meta.name(), dispatcher);
for (String alias : meta.aliases()) {
aliasToCommand.put(alias, meta.name());
@@ -188,6 +194,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
}
for (Map.Entry<String, DispatchCommand> entry : dispatchers.entrySet()) {
+ DispatchCommand dispatcher = entry.getValue();
map.put(entry.getKey(), entry.getValue().getClass());
}
}
@@ -259,10 +266,27 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
return cmd;
}
+ private boolean hasVisibleCommands() {
+ boolean visible = false;
+ for (Class<? extends BaseCommand> cmd : commands) {
+ visible |= !cmd.getAnnotation(CommandMetaData.class).hidden();
+ if (visible) {
+ return true;
+ }
+ }
+ for (DispatchCommand cmd : dispatchers.values()) {
+ visible |= cmd.hasVisibleCommands();
+ if (visible) {
+ return true;
+ }
+ }
+ return false;
+ }
+
@Override
public String usage() {
- Set<String> commands = new TreeSet<String>();
- Set<String> dispatchers = new TreeSet<String>();
+ Set<String> cmds = new TreeSet<String>();
+ Set<String> dcs = new TreeSet<String>();
Map<String, String> displayNames = Maps.newHashMap();
int maxLength = -1;
Map<String, Class<? extends BaseCommand>> m = getMap();
@@ -281,15 +305,18 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
maxLength = Math.max(maxLength, displayName.length());
if (DispatchCommand.class.isAssignableFrom(c)) {
- dispatchers.add(name);
+ DispatchCommand d = dispatchers.get(name);
+ if (d.hasVisibleCommands()) {
+ dcs.add(name);
+ }
} else {
- commands.add(name);
+ cmds.add(name);
}
}
String format = "%-" + maxLength + "s %s";
final StringBuilder usage = new StringBuilder();
- if (!commands.isEmpty()) {
+ if (!cmds.isEmpty()) {
usage.append("Available commands");
if (!getName().isEmpty()) {
usage.append(" of ");
@@ -297,7 +324,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
}
usage.append(" are:\n");
usage.append("\n");
- for (String name : commands) {
+ for (String name : cmds) {
final Class<? extends Command> c = m.get(name);
String displayName = displayNames.get(name);
CommandMetaData meta = c.getAnnotation(CommandMetaData.class);
@@ -308,7 +335,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
usage.append("\n");
}
- if (!dispatchers.isEmpty()) {
+ if (!dcs.isEmpty()) {
usage.append("Available command dispatchers");
if (!getName().isEmpty()) {
usage.append(" of ");
@@ -316,7 +343,7 @@ public abstract class DispatchCommand extends BaseCommand implements ExtensionPo
}
usage.append(" are:\n");
usage.append("\n");
- for (String name : dispatchers) {
+ for (String name : dcs) {
final Class<? extends BaseCommand> c = m.get(name);
String displayName = displayNames.get(name);
CommandMetaData meta = c.getAnnotation(CommandMetaData.class);