repo: rework parser setup to handle init -c

We added support for `repo init -c` to main.py, but not to the
launcher, so the -c option only works after the first init has
run which kind of defeats its purpose.  Rework the parser setup
so that we can tell it whether it's for "init" or "gitc-init"
and then add the -c option in the same way we do in main.py.

This has the benefit of getting the parser entirely out of the
module scope which makes it a lot easier to reason about, and
it means we can write some unittests.

Change-Id: Icbc2ec3aceb938d5a8f941d5fbce1548553dc5f7
Test: repo help init
Test: repo help gitc-init
Test: repo init -u https://android.googlesource.com/platform/manifest -c
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/255113
Reviewed-by: David Pursehouse <dpursehouse@collab.net>
Tested-by: Mike Frysinger <vapier@google.com>
This commit is contained in:
Mike Frysinger 2020-02-14 00:24:38 -05:00
parent 9cc1d70476
commit d8fda90eed
2 changed files with 47 additions and 28 deletions

61
repo
View File

@ -253,26 +253,37 @@ home_dot_repo = os.path.expanduser('~/.repoconfig')
gpg_dir = os.path.join(home_dot_repo, 'gnupg') gpg_dir = os.path.join(home_dot_repo, 'gnupg')
extra_args = [] extra_args = []
init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
def _InitParser(): def GetParser(gitc_init=False):
"""Setup the init subcommand parser.""" """Setup the CLI parser."""
if gitc_init:
usage = 'repo gitc-init -u url -c client [options]'
else:
usage = 'repo init -u url [options]'
parser = optparse.OptionParser(usage=usage)
# Logging. # Logging.
group = init_optparse.add_option_group('Logging options') group = parser.add_option_group('Logging options')
group.add_option('-q', '--quiet', group.add_option('-q', '--quiet',
action='store_true', default=False, action='store_true', default=False,
help='be quiet') help='be quiet')
# Manifest. # Manifest.
group = init_optparse.add_option_group('Manifest options') group = parser.add_option_group('Manifest options')
group.add_option('-u', '--manifest-url', group.add_option('-u', '--manifest-url',
help='manifest repository location', metavar='URL') help='manifest repository location', metavar='URL')
group.add_option('-b', '--manifest-branch', group.add_option('-b', '--manifest-branch',
help='manifest branch or revision', metavar='REVISION') help='manifest branch or revision', metavar='REVISION')
group.add_option('-m', '--manifest-name', group.add_option('-m', '--manifest-name',
help='initial manifest file', metavar='NAME.xml') help='initial manifest file', metavar='NAME.xml')
group.add_option('--current-branch', cbr_opts = ['--current-branch']
# The gitc-init subcommand allocates -c itself, but a lot of init users
# want -c, so try to satisfy both as best we can.
if not gitc_init:
cbr_opts += ['-c']
group.add_option(*cbr_opts,
dest='current_branch_only', action='store_true', dest='current_branch_only', action='store_true',
help='fetch only current manifest branch from server') help='fetch only current manifest branch from server')
group.add_option('--mirror', action='store_true', group.add_option('--mirror', action='store_true',
@ -310,7 +321,7 @@ def _InitParser():
help="don't fetch tags in the manifest") help="don't fetch tags in the manifest")
# Tool. # Tool.
group = init_optparse.add_option_group('repo Version options') group = parser.add_option_group('repo Version options')
group.add_option('--repo-url', metavar='URL', group.add_option('--repo-url', metavar='URL',
help='repo repository location ($REPO_URL)') help='repo repository location ($REPO_URL)')
group.add_option('--repo-branch', metavar='REVISION', group.add_option('--repo-branch', metavar='REVISION',
@ -319,21 +330,20 @@ def _InitParser():
help='do not verify repo source code') help='do not verify repo source code')
# Other. # Other.
group = init_optparse.add_option_group('Other options') group = parser.add_option_group('Other options')
group.add_option('--config-name', group.add_option('--config-name',
action='store_true', default=False, action='store_true', default=False,
help='Always prompt for name/e-mail') help='Always prompt for name/e-mail')
# gitc-init specific settings.
def _GitcInitOptions(init_optparse_arg): if gitc_init:
init_optparse_arg.set_usage("repo gitc-init -u url -c client [options]") group = parser.add_option_group('GITC options')
g = init_optparse_arg.add_option_group('GITC options') group.add_option('-f', '--manifest-file',
g.add_option('-f', '--manifest-file',
dest='manifest_file',
help='Optional manifest file to use for this GITC client.') help='Optional manifest file to use for this GITC client.')
g.add_option('-c', '--gitc-client', group.add_option('-c', '--gitc-client',
dest='gitc_client', help='Name of the gitc_client instance to create or modify.')
help='The name of the gitc_client instance to create or modify.')
return parser
# This is a poor replacement for subprocess.run until we require Python 3.6+. # This is a poor replacement for subprocess.run until we require Python 3.6+.
@ -432,11 +442,10 @@ class CloneFailure(Exception):
def _Init(args, gitc_init=False): def _Init(args, gitc_init=False):
"""Installs repo by cloning it over the network. """Installs repo by cloning it over the network.
""" """
if gitc_init: parser = GetParser(gitc_init=gitc_init)
_GitcInitOptions(init_optparse) opt, args = parser.parse_args(args)
opt, args = init_optparse.parse_args(args)
if args: if args:
init_optparse.print_usage() parser.print_usage()
sys.exit(1) sys.exit(1)
url = opt.repo_url url = opt.repo_url
@ -896,12 +905,9 @@ For access to the full online help, install repo ("repo init").
def _Help(args): def _Help(args):
if args: if args:
if args[0] == 'init': if args[0] in {'init', 'gitc-init'}:
init_optparse.print_help() parser = GetParser(gitc_init=args[0] == 'gitc-init')
sys.exit(0) parser.print_help()
elif args[0] == 'gitc-init':
_GitcInitOptions(init_optparse)
init_optparse.print_help()
sys.exit(0) sys.exit(0)
else: else:
print("error: '%s' is not a bootstrap command.\n" print("error: '%s' is not a bootstrap command.\n"
@ -983,7 +989,6 @@ def main(orig_args):
'command from the corresponding client under /gitc/', 'command from the corresponding client under /gitc/',
file=sys.stderr) file=sys.stderr)
sys.exit(1) sys.exit(1)
_InitParser()
if not repo_main: if not repo_main:
if opt.help: if opt.help:
_Usage() _Usage()

View File

@ -66,6 +66,20 @@ class RepoWrapperUnitTest(RepoWrapperTestCase):
self.assertEqual('', stderr.getvalue()) self.assertEqual('', stderr.getvalue())
self.assertIn('repo launcher version', stdout.getvalue()) self.assertIn('repo launcher version', stdout.getvalue())
def test_init_parser(self):
"""Make sure 'init' GetParser works."""
parser = self.wrapper.GetParser(gitc_init=False)
opts, args = parser.parse_args([])
self.assertEqual([], args)
self.assertIsNone(opts.manifest_url)
def test_gitc_init_parser(self):
"""Make sure 'gitc-init' GetParser works."""
parser = self.wrapper.GetParser(gitc_init=True)
opts, args = parser.parse_args([])
self.assertEqual([], args)
self.assertIsNone(opts.manifest_file)
def test_get_gitc_manifest_dir_no_gitc(self): def test_get_gitc_manifest_dir_no_gitc(self):
""" """
Test reading a missing gitc config file Test reading a missing gitc config file