mirror of
https://gerrit.googlesource.com/git-repo
synced 2025-01-02 16:14:25 +00:00
Add regex support for subcommand forall
Filter the project list based on regex or wildcard matching of strings, then we can handle subset of all projects. Change-Id: Ib6c23aec79e7d981f7b6a5eb0ae93c44effec467 Signed-off-by: Zhiguang Li <muzili@gmail.com>
This commit is contained in:
parent
275e4b727a
commit
a8864fba9f
11
command.py
11
command.py
@ -186,6 +186,17 @@ class Command(object):
|
|||||||
result.sort(key=_getpath)
|
result.sort(key=_getpath)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def FindProjects(self, args):
|
||||||
|
result = []
|
||||||
|
for project in self.GetProjects(''):
|
||||||
|
for arg in args:
|
||||||
|
pattern = re.compile(r'%s' % arg, re.IGNORECASE)
|
||||||
|
if pattern.search(project.name) or pattern.search(project.relpath):
|
||||||
|
result.append(project)
|
||||||
|
break
|
||||||
|
result.sort(key=lambda project: project.relpath)
|
||||||
|
return result
|
||||||
|
|
||||||
# pylint: disable=W0223
|
# pylint: disable=W0223
|
||||||
# Pylint warns that the `InteractiveCommand` and `PagedCommand` classes do not
|
# Pylint warns that the `InteractiveCommand` and `PagedCommand` classes do not
|
||||||
# override method `Execute` which is abstract in `Command`. Since that method
|
# override method `Execute` which is abstract in `Command`. Since that method
|
||||||
|
@ -42,10 +42,14 @@ class Forall(Command, MirrorSafeCommand):
|
|||||||
helpSummary = "Run a shell command in each project"
|
helpSummary = "Run a shell command in each project"
|
||||||
helpUsage = """
|
helpUsage = """
|
||||||
%prog [<project>...] -c <command> [<arg>...]
|
%prog [<project>...] -c <command> [<arg>...]
|
||||||
|
%prog -r str1 [str2] ... -c <command> [<arg>...]"
|
||||||
"""
|
"""
|
||||||
helpDescription = """
|
helpDescription = """
|
||||||
Executes the same shell command in each project.
|
Executes the same shell command in each project.
|
||||||
|
|
||||||
|
The -r option allows running the command only on projects matching
|
||||||
|
regex or wildcard expression.
|
||||||
|
|
||||||
Output Formatting
|
Output Formatting
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
@ -103,6 +107,9 @@ without iterating through the remaining projects.
|
|||||||
setattr(parser.values, option.dest, list(parser.rargs))
|
setattr(parser.values, option.dest, list(parser.rargs))
|
||||||
while parser.rargs:
|
while parser.rargs:
|
||||||
del parser.rargs[0]
|
del parser.rargs[0]
|
||||||
|
p.add_option('-r', '--regex',
|
||||||
|
dest='regex', action='store_true',
|
||||||
|
help="Execute the command only on projects matching regex or wildcard expression")
|
||||||
p.add_option('-c', '--command',
|
p.add_option('-c', '--command',
|
||||||
help='Command (and arguments) to execute',
|
help='Command (and arguments) to execute',
|
||||||
dest='command',
|
dest='command',
|
||||||
@ -166,7 +173,12 @@ without iterating through the remaining projects.
|
|||||||
rc = 0
|
rc = 0
|
||||||
first = True
|
first = True
|
||||||
|
|
||||||
for project in self.GetProjects(args):
|
if not opt.regex:
|
||||||
|
projects = self.GetProjects(args)
|
||||||
|
else:
|
||||||
|
projects = self.FindProjects(args)
|
||||||
|
|
||||||
|
for project in projects:
|
||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
def setenv(name, val):
|
def setenv(name, val):
|
||||||
if val is None:
|
if val is None:
|
||||||
|
@ -14,7 +14,6 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import re
|
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from command import Command, MirrorSafeCommand
|
from command import Command, MirrorSafeCommand
|
||||||
@ -83,14 +82,3 @@ This is similar to running: repo forall -c 'echo "$REPO_PATH : $REPO_PROJECT"'.
|
|||||||
|
|
||||||
lines.sort()
|
lines.sort()
|
||||||
print('\n'.join(lines))
|
print('\n'.join(lines))
|
||||||
|
|
||||||
def FindProjects(self, args):
|
|
||||||
result = []
|
|
||||||
for project in self.GetProjects(''):
|
|
||||||
for arg in args:
|
|
||||||
pattern = re.compile(r'%s' % arg, re.IGNORECASE)
|
|
||||||
if pattern.search(project.name) or pattern.search(project.relpath):
|
|
||||||
result.append(project)
|
|
||||||
break
|
|
||||||
result.sort(key=lambda project: project.relpath)
|
|
||||||
return result
|
|
||||||
|
Loading…
Reference in New Issue
Block a user