ls-groups: Add option to only list groups that are visible to all

Change-Id: I68a9ec5397f9e314bc582844c620722641702154
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/Documentation/cmd-ls-groups.txt b/Documentation/cmd-ls-groups.txt
index cb65f7e..78b2f2e 100644
--- a/Documentation/cmd-ls-groups.txt
+++ b/Documentation/cmd-ls-groups.txt
@@ -10,6 +10,7 @@
 [verse]
 'ssh' -p <port> <host> 'gerrit ls-groups'
   [--project <NAME>]
+  [--visible-to-all]
 
 DESCRIPTION
 -----------
@@ -38,6 +39,11 @@
 	projects. In this case all groups are listed that have a
 	permission for any of the specified projects.
 
+--visible-to-all::
+	Displays only groups that are visible to all registered users
+	(groups that are explicitly marked as visible to all registered
+	users).
+
 EXAMPLES
 --------
 
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
index c7487f3..94eed9f 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/VisibleGroups.java
@@ -45,6 +45,7 @@
   private final GroupDetailFactory.Factory groupDetailFactory;
 
   private Collection<ProjectControl> projects;
+  private boolean onlyVisibleToAll;
 
   @Inject
   VisibleGroups(final Provider<IdentifiedUser> currentUser,
@@ -61,6 +62,10 @@
     this.projects = projects;
   }
 
+  public void setOnlyVisibleToAll(final boolean onlyVisibleToAll) {
+    this.onlyVisibleToAll = onlyVisibleToAll;
+  }
+
   public GroupList get() throws OrmException, NoSuchGroupException {
     final Iterable<AccountGroup> groups;
     if (projects != null && !projects.isEmpty()) {
@@ -98,6 +103,9 @@
           continue;
         }
       }
+      if (onlyVisibleToAll && !group.isVisibleToAll()) {
+        continue;
+      }
       filteredGroups.add(group);
     }
     return filteredGroups;
diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java
index f70f742..7a9bd70 100644
--- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java
+++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/commands/ListGroupsCommand.java
@@ -41,6 +41,9 @@
       usage = "projects for which the groups should be listed")
   private final List<ProjectControl> projects = new ArrayList<ProjectControl>();
 
+  @Option(name = "--visible-to-all", usage = "to list only groups that are visible to all registered users")
+  private boolean visibleToAll;
+
   @Override
   public void start(final Environment env) throws IOException {
     startThread(new CommandRunnable() {
@@ -57,6 +60,7 @@
     try {
       final VisibleGroups visibleGroups = visibleGroupsFactory.create();
       visibleGroups.setProjects(projects);
+      visibleGroups.setOnlyVisibleToAll(visibleToAll);
       final GroupList groupList = visibleGroups.get();
       for (final GroupDetail groupDetail : groupList.getGroups()) {
         stdout.print(groupDetail.group.getName() + "\n");