summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2020-02-14 00:24:38 -0500
committerMike Frysinger <vapier@google.com>2020-02-14 05:52:17 +0000
commitd8fda90eedcee2cc39ba13b9f8f7b7bab37310c3 (patch)
tree3eed85f120ee2d1941828cf63d6b9a0451eb03f0
parent9cc1d70476b0cfc12b28792d77c15d96d16e64df (diff)
downloadgit-repo-d8fda90eedcee2cc39ba13b9f8f7b7bab37310c3.tar.gz
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>
-rwxr-xr-xrepo61
-rw-r--r--tests/test_wrapper.py14
2 files changed, 47 insertions, 28 deletions
diff --git a/repo b/repo
index f7e7581a..5674be84 100755
--- a/repo
+++ b/repo
@@ -253,26 +253,37 @@ home_dot_repo = os.path.expanduser('~/.repoconfig')
253gpg_dir = os.path.join(home_dot_repo, 'gnupg') 253gpg_dir = os.path.join(home_dot_repo, 'gnupg')
254 254
255extra_args = [] 255extra_args = []
256init_optparse = optparse.OptionParser(usage="repo init -u url [options]")
257 256
258 257
259def _InitParser(): 258def GetParser(gitc_init=False):
260 """Setup the init subcommand parser.""" 259 """Setup the CLI parser."""
260 if gitc_init:
261 usage = 'repo gitc-init -u url -c client [options]'
262 else:
263 usage = 'repo init -u url [options]'
264
265 parser = optparse.OptionParser(usage=usage)
266
261 # Logging. 267 # Logging.
262 group = init_optparse.add_option_group('Logging options') 268 group = parser.add_option_group('Logging options')
263 group.add_option('-q', '--quiet', 269 group.add_option('-q', '--quiet',
264 action='store_true', default=False, 270 action='store_true', default=False,
265 help='be quiet') 271 help='be quiet')
266 272
267 # Manifest. 273 # Manifest.
268 group = init_optparse.add_option_group('Manifest options') 274 group = parser.add_option_group('Manifest options')
269 group.add_option('-u', '--manifest-url', 275 group.add_option('-u', '--manifest-url',
270 help='manifest repository location', metavar='URL') 276 help='manifest repository location', metavar='URL')
271 group.add_option('-b', '--manifest-branch', 277 group.add_option('-b', '--manifest-branch',
272 help='manifest branch or revision', metavar='REVISION') 278 help='manifest branch or revision', metavar='REVISION')
273 group.add_option('-m', '--manifest-name', 279 group.add_option('-m', '--manifest-name',
274 help='initial manifest file', metavar='NAME.xml') 280 help='initial manifest file', metavar='NAME.xml')
275 group.add_option('--current-branch', 281 cbr_opts = ['--current-branch']
282 # The gitc-init subcommand allocates -c itself, but a lot of init users
283 # want -c, so try to satisfy both as best we can.
284 if not gitc_init:
285 cbr_opts += ['-c']
286 group.add_option(*cbr_opts,
276 dest='current_branch_only', action='store_true', 287 dest='current_branch_only', action='store_true',
277 help='fetch only current manifest branch from server') 288 help='fetch only current manifest branch from server')
278 group.add_option('--mirror', action='store_true', 289 group.add_option('--mirror', action='store_true',
@@ -310,7 +321,7 @@ def _InitParser():
310 help="don't fetch tags in the manifest") 321 help="don't fetch tags in the manifest")
311 322
312 # Tool. 323 # Tool.
313 group = init_optparse.add_option_group('repo Version options') 324 group = parser.add_option_group('repo Version options')
314 group.add_option('--repo-url', metavar='URL', 325 group.add_option('--repo-url', metavar='URL',
315 help='repo repository location ($REPO_URL)') 326 help='repo repository location ($REPO_URL)')
316 group.add_option('--repo-branch', metavar='REVISION', 327 group.add_option('--repo-branch', metavar='REVISION',
@@ -319,21 +330,20 @@ def _InitParser():
319 help='do not verify repo source code') 330 help='do not verify repo source code')
320 331
321 # Other. 332 # Other.
322 group = init_optparse.add_option_group('Other options') 333 group = parser.add_option_group('Other options')
323 group.add_option('--config-name', 334 group.add_option('--config-name',
324 action='store_true', default=False, 335 action='store_true', default=False,
325 help='Always prompt for name/e-mail') 336 help='Always prompt for name/e-mail')
326 337
338 # gitc-init specific settings.
339 if gitc_init:
340 group = parser.add_option_group('GITC options')
341 group.add_option('-f', '--manifest-file',
342 help='Optional manifest file to use for this GITC client.')
343 group.add_option('-c', '--gitc-client',
344 help='Name of the gitc_client instance to create or modify.')
327 345
328def _GitcInitOptions(init_optparse_arg): 346 return parser
329 init_optparse_arg.set_usage("repo gitc-init -u url -c client [options]")
330 g = init_optparse_arg.add_option_group('GITC options')
331 g.add_option('-f', '--manifest-file',
332 dest='manifest_file',
333 help='Optional manifest file to use for this GITC client.')
334 g.add_option('-c', '--gitc-client',
335 dest='gitc_client',
336 help='The name of the gitc_client instance to create or modify.')
337 347
338 348
339# This is a poor replacement for subprocess.run until we require Python 3.6+. 349# This is a poor replacement for subprocess.run until we require Python 3.6+.
@@ -432,11 +442,10 @@ class CloneFailure(Exception):
432def _Init(args, gitc_init=False): 442def _Init(args, gitc_init=False):
433 """Installs repo by cloning it over the network. 443 """Installs repo by cloning it over the network.
434 """ 444 """
435 if gitc_init: 445 parser = GetParser(gitc_init=gitc_init)
436 _GitcInitOptions(init_optparse) 446 opt, args = parser.parse_args(args)
437 opt, args = init_optparse.parse_args(args)
438 if args: 447 if args:
439 init_optparse.print_usage() 448 parser.print_usage()
440 sys.exit(1) 449 sys.exit(1)
441 450
442 url = opt.repo_url 451 url = opt.repo_url
@@ -896,12 +905,9 @@ For access to the full online help, install repo ("repo init").
896 905
897def _Help(args): 906def _Help(args):
898 if args: 907 if args:
899 if args[0] == 'init': 908 if args[0] in {'init', 'gitc-init'}:
900 init_optparse.print_help() 909 parser = GetParser(gitc_init=args[0] == 'gitc-init')
901 sys.exit(0) 910 parser.print_help()
902 elif args[0] == 'gitc-init':
903 _GitcInitOptions(init_optparse)
904 init_optparse.print_help()
905 sys.exit(0) 911 sys.exit(0)
906 else: 912 else:
907 print("error: '%s' is not a bootstrap command.\n" 913 print("error: '%s' is not a bootstrap command.\n"
@@ -983,7 +989,6 @@ def main(orig_args):
983 'command from the corresponding client under /gitc/', 989 'command from the corresponding client under /gitc/',
984 file=sys.stderr) 990 file=sys.stderr)
985 sys.exit(1) 991 sys.exit(1)
986 _InitParser()
987 if not repo_main: 992 if not repo_main:
988 if opt.help: 993 if opt.help:
989 _Usage() 994 _Usage()
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py
index 4d2cf3fb..a98c4130 100644
--- a/tests/test_wrapper.py
+++ b/tests/test_wrapper.py
@@ -66,6 +66,20 @@ class RepoWrapperUnitTest(RepoWrapperTestCase):
66 self.assertEqual('', stderr.getvalue()) 66 self.assertEqual('', stderr.getvalue())
67 self.assertIn('repo launcher version', stdout.getvalue()) 67 self.assertIn('repo launcher version', stdout.getvalue())
68 68
69 def test_init_parser(self):
70 """Make sure 'init' GetParser works."""
71 parser = self.wrapper.GetParser(gitc_init=False)
72 opts, args = parser.parse_args([])
73 self.assertEqual([], args)
74 self.assertIsNone(opts.manifest_url)
75
76 def test_gitc_init_parser(self):
77 """Make sure 'gitc-init' GetParser works."""
78 parser = self.wrapper.GetParser(gitc_init=True)
79 opts, args = parser.parse_args([])
80 self.assertEqual([], args)
81 self.assertIsNone(opts.manifest_file)
82
69 def test_get_gitc_manifest_dir_no_gitc(self): 83 def test_get_gitc_manifest_dir_no_gitc(self):
70 """ 84 """
71 Test reading a missing gitc config file 85 Test reading a missing gitc config file