From 172ccfe52d023a8466f34799a07e3caa283bdfff Mon Sep 17 00:00:00 2001 From: Junghoon Jang Date: Tue, 11 Apr 2023 09:49:28 +0000 Subject: [PATCH 1/2] repo-pull: Consider branch of repo In the case of one source tree containing multiple repos with the same project, but different branches, the script won't sync them properly. To fix it, make it consider the branch along with the project. Test: Tested syncing well in the directory which contains multiple repos with the same project, but different branches. Change-Id: I9d1add4ffe85f80f8f9d6a3ae2c046473d7c4631 Signed-off-by: Junghoon Jang --- tools/repo_pull/repo_pull.py | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/tools/repo_pull/repo_pull.py b/tools/repo_pull/repo_pull.py index 34d3f7cac..3491eb914 100755 --- a/tools/repo_pull/repo_pull.py +++ b/tools/repo_pull/repo_pull.py @@ -90,6 +90,7 @@ class ChangeList(object): self.project = project self.number = change_list['_number'] + self.branch = change_list['branch'] self.fetch = fetch @@ -129,6 +130,28 @@ def find_repo_top(curdir): raise ValueError('.repo dir not found') +class ProjectNameDirDict: + """A dict which maps project name and revision to the source path.""" + def __init__(self): + self._dirs = dict() + + + def add_directory(self, name, revision, path): + """Maps project name and revision to path.""" + self._dirs[name] = path or name + if revision: + self._dirs[(name, revision)] = path or name + + + def find_directory(self, name, revision, default_result=None): + """Finds corresponding path of project name and revision.""" + if (name, revision) in self._dirs: + return self._dirs[(name, revision)] + if default_result is None: + return self._dirs[name] + return self._dirs.get(name, default_result) + + def build_project_name_dir_dict(manifest_name): """Build the mapping from Gerrit project name to source tree project directory path.""" @@ -138,14 +161,12 @@ def build_project_name_dir_dict(manifest_name): raw_manifest_xml = run(manifest_cmd, stdout=PIPE, check=True).stdout manifest_xml = xml.dom.minidom.parseString(raw_manifest_xml) - project_dirs = {} + project_dirs = ProjectNameDirDict() for project in manifest_xml.getElementsByTagName('project'): name = project.getAttribute('name') path = project.getAttribute('path') - if path: - project_dirs[name] = path - else: - project_dirs[name] = name + revision = project.getAttribute('revision') + project_dirs.add_directory(name, revision, path) return project_dirs @@ -269,7 +290,8 @@ def _main_bash(args): print(_sh_quote_command(['pushd', repo_top])) for changes in change_list_groups: for change in changes: - project_dir = project_dirs.get(change.project, change.project) + project_dir = project_dirs.find_directory( + change.project, change.branch, change.project) cmds = [] cmds.append(['pushd', project_dir]) cmds.extend(build_pull_commands( @@ -291,7 +313,7 @@ def _do_pull_change_lists_for_project(task): for i, change in enumerate(changes): try: - cwd = project_dirs[change.project] + cwd = project_dirs.find_directory(change.project, change.branch) except KeyError: err_msg = 'error: project "{}" cannot be found in manifest.xml\n' err_msg = err_msg.format(change.project).encode('utf-8') From 108c36c52beb300c5180386d00ef23e0157b1e6b Mon Sep 17 00:00:00 2001 From: Junghoon Jang Date: Tue, 11 Apr 2023 09:56:58 +0000 Subject: [PATCH 2/2] repo-pull: Introduce --current-branch argument To make the script can be used from automatic scripts, introduce `--current-branch` argument which allows pulling commits to the current branch without creating a local branch. It will skip the prompt which asking about the creation of a local branch. Test: With --current-branch argument, the script doesn't ask setting local branch name. Change-Id: Iff3cdebe6338bb2be738e60047d5c4dd81cde4f0 Signed-off-by: Junghoon Jang --- tools/repo_pull/repo_pull.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/repo_pull/repo_pull.py b/tools/repo_pull/repo_pull.py index 3491eb914..b4bdfea7a 100755 --- a/tools/repo_pull/repo_pull.py +++ b/tools/repo_pull/repo_pull.py @@ -409,6 +409,9 @@ def _parse_args(): parser.add_argument('-j', '--parallel', default=1, type=int, help='Number of parallel running commands') + parser.add_argument('--current-branch', action='store_true', + help='Pull commits to the current branch') + return parser.parse_args() @@ -421,7 +424,7 @@ def _get_change_lists_from_args(args): def _get_local_branch_name_from_args(args): """Get the local branch name from args.""" - if not args.branch and not _confirm( + if not args.branch and not args.current_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)