bash-completion: complete projects with repo forall

We need to add a little bit more logic here so we stop completing
projects once we see the -c argument.

Bug: https://crbug.com/gerrit/14797
Change-Id: Ic2ba4f3dd616ec49d8ad754ff62d0d6e0250dbe6
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/312905
Reviewed-by: Xin Li <delphij@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
diff --git a/completion.bash b/completion.bash
index 6a5bfe1..09291d5 100644
--- a/completion.bash
+++ b/completion.bash
@@ -14,6 +14,9 @@
 
 # Programmable bash completion.  https://github.com/scop/bash-completion
 
+# TODO: Handle interspersed options.  We handle `repo h<tab>`, but not
+# `repo --time h<tab>`.
+
 # Complete the list of repo subcommands.
 __complete_repo_list_commands() {
   local repo=${COMP_WORDS[0]}
@@ -79,6 +82,23 @@
   fi
 }
 
+# Complete `repo forall`.
+__complete_repo_command_forall() {
+  local current=$1
+  # CWORD=1 is "forall".
+  # CWORD=2+ are <projects> *until* we hit the -c option.
+  local i
+  for (( i = 0; i < COMP_CWORD; ++i )); do
+    if [[ "${COMP_WORDS[i]}" == "-c" ]]; then
+      return 0
+    fi
+  done
+
+  COMPREPLY=(
+    $(compgen -W "$(__complete_repo_list_projects)" -- "${current}")
+  )
+}
+
 # Complete `repo start`.
 __complete_repo_command_start() {
   local current=$1
@@ -112,7 +132,7 @@
     return 0
     ;;
 
-  help|start)
+  help|start|forall)
     __complete_repo_command_${command} "${current}"
     return 0
     ;;