From 1e026cfcc4043b1a001a6e62d73b05d7663e7dfd Mon Sep 17 00:00:00 2001 From: Logan Chien Date: Fri, 20 Apr 2018 17:34:55 +0800 Subject: [PATCH] repo-pull: Allow to ignore local branch name This commit allows users to omit `--branch` from command line options. `repo_pull.py` will not call `repo start` if `--branch` is unspecified. Test: repo_pull.py bash -g GERRIT QUERY Change-Id: I62380485f057bd35ab8ae4d5e70372ff5d05fcc5 --- tools/repo_pull/README.md | 4 ++-- tools/repo_pull/repo_pull.py | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/tools/repo_pull/README.md b/tools/repo_pull/README.md index a70128bad..402db1d0b 100644 --- a/tools/repo_pull/README.md +++ b/tools/repo_pull/README.md @@ -72,8 +72,8 @@ These are common queries: * `-g` or `--gerrit` specifies the URL of the Gerrit Code Review website. *(required)* -* `-b` or `--branch` specifies the local topic branch name that will be passed - to `repo start`. +* `-b` or `--branch` specifies the local branch name that will be passed to + `repo start`. * `-j` or `--parallel` specifies the number of parallel threads while pulling change lists. diff --git a/tools/repo_pull/repo_pull.py b/tools/repo_pull/repo_pull.py index a11df0182..c171f28bd 100755 --- a/tools/repo_pull/repo_pull.py +++ b/tools/repo_pull/repo_pull.py @@ -40,6 +40,11 @@ try: except ImportError: from urllib2 import HTTPBasicAuthHandler, build_opener # PY2 +try: + from __builtin__ import raw_input as input # PY2 +except ImportError: + pass + try: from shlex import quote as _sh_quote # PY3.3 except ImportError: @@ -96,6 +101,18 @@ else: file.buffer.write(data) +def _confirm(question, default, file=sys.stderr): + """Prompt a yes/no question and convert the answer to a boolean value.""" + answers = {'': default, 'y': True, 'yes': True, 'n': False, 'no': False} + suffix = '[Y/n] ' if default else ' [y/N] ' + while True: + file.write(question + suffix) + file.flush() + ans = answers.get(input().lower()) + if ans is not None: + return ans + + class ChangeList(object): """A ChangeList to be checked out.""" @@ -269,7 +286,8 @@ def build_pull_commands(change, branch_name, merge_opt): to subprocess.run().""" cmds = [] - cmds.append(['repo', 'start', branch_name]) + if branch_name is not None: + cmds.append(['repo', 'start', branch_name]) cmds.append(['git', 'fetch', change.fetch_url, change.fetch_ref]) if change.is_merge(): cmds.append(_MERGE_COMMANDS[merge_opt] + ['FETCH_HEAD']) @@ -293,7 +311,7 @@ def _sh_quote_commands(cmds): def _main_bash(args): """Print the bash command to pull the change lists.""" - branch_name = _get_topic_branch_from_args(args) + branch_name = _get_local_branch_name_from_args(args) manifest_path = _get_manifest_xml_from_args(args) project_dirs = build_project_name_to_directory_dict(manifest_path) @@ -330,7 +348,7 @@ def _do_pull_change_lists_for_project(task): def _main_pull(args): """Pull the change lists.""" - branch_name = _get_topic_branch_from_args(args) + branch_name = _get_local_branch_name_from_args(args) manifest_path = _get_manifest_xml_from_args(args) project_dirs = build_project_name_to_directory_dict(manifest_path) @@ -397,7 +415,7 @@ def _parse_args(): help='Method to pull merge commits') parser.add_argument('-b', '--branch', - help='Topic branch name to start with') + help='Local branch name for `repo start`') parser.add_argument('-j', '--parallel', default=1, type=int, help='Number of parallel running commands') @@ -419,10 +437,11 @@ def _get_change_lists_from_args(args): args.limits) -def _get_topic_branch_from_args(args): - """Get the topic branch name from args.""" - if not args.branch: - print('error: --branch must be specified') +def _get_local_branch_name_from_args(args): + """Get the local branch name from args.""" + if not args.branch and not _confirm( + 'Do you want to continue without local branch name?', False): + print('error: `-b` or `--branch` must be specified', file=sys.stderr) sys.exit(1) return args.branch