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
This commit is contained in:
Logan Chien
2018-04-20 17:34:55 +08:00
parent 9d19090823
commit 1e026cfcc4
2 changed files with 29 additions and 10 deletions

View File

@@ -72,8 +72,8 @@ These are common queries:
* `-g` or `--gerrit` specifies the URL of the Gerrit Code Review website. * `-g` or `--gerrit` specifies the URL of the Gerrit Code Review website.
*(required)* *(required)*
* `-b` or `--branch` specifies the local topic branch name that will be passed * `-b` or `--branch` specifies the local branch name that will be passed to
to `repo start`. `repo start`.
* `-j` or `--parallel` specifies the number of parallel threads while pulling * `-j` or `--parallel` specifies the number of parallel threads while pulling
change lists. change lists.

View File

@@ -40,6 +40,11 @@ try:
except ImportError: except ImportError:
from urllib2 import HTTPBasicAuthHandler, build_opener # PY2 from urllib2 import HTTPBasicAuthHandler, build_opener # PY2
try:
from __builtin__ import raw_input as input # PY2
except ImportError:
pass
try: try:
from shlex import quote as _sh_quote # PY3.3 from shlex import quote as _sh_quote # PY3.3
except ImportError: except ImportError:
@@ -96,6 +101,18 @@ else:
file.buffer.write(data) 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): class ChangeList(object):
"""A ChangeList to be checked out.""" """A ChangeList to be checked out."""
@@ -269,7 +286,8 @@ def build_pull_commands(change, branch_name, merge_opt):
to subprocess.run().""" to subprocess.run()."""
cmds = [] 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]) cmds.append(['git', 'fetch', change.fetch_url, change.fetch_ref])
if change.is_merge(): if change.is_merge():
cmds.append(_MERGE_COMMANDS[merge_opt] + ['FETCH_HEAD']) cmds.append(_MERGE_COMMANDS[merge_opt] + ['FETCH_HEAD'])
@@ -293,7 +311,7 @@ def _sh_quote_commands(cmds):
def _main_bash(args): def _main_bash(args):
"""Print the bash command to pull the change lists.""" """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) manifest_path = _get_manifest_xml_from_args(args)
project_dirs = build_project_name_to_directory_dict(manifest_path) 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): def _main_pull(args):
"""Pull the change lists.""" """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) manifest_path = _get_manifest_xml_from_args(args)
project_dirs = build_project_name_to_directory_dict(manifest_path) project_dirs = build_project_name_to_directory_dict(manifest_path)
@@ -397,7 +415,7 @@ def _parse_args():
help='Method to pull merge commits') help='Method to pull merge commits')
parser.add_argument('-b', '--branch', 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, parser.add_argument('-j', '--parallel', default=1, type=int,
help='Number of parallel running commands') help='Number of parallel running commands')
@@ -419,10 +437,11 @@ def _get_change_lists_from_args(args):
args.limits) args.limits)
def _get_topic_branch_from_args(args): def _get_local_branch_name_from_args(args):
"""Get the topic branch name from args.""" """Get the local branch name from args."""
if not args.branch: if not args.branch and not _confirm(
print('error: --branch must be specified') 'Do you want to continue without local branch name?', False):
print('error: `-b` or `--branch` must be specified', file=sys.stderr)
sys.exit(1) sys.exit(1)
return args.branch return args.branch