From 31fabeed54f3cbf9495e559dce60f58e126f58d5 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Sun, 28 Feb 2021 22:03:04 -0500 Subject: [PATCH] download: handle shared projects a bit better If a manifest checksout a project multiple times, repo download isn't able to accurately pick the right project. We were just picking the first result which could be a bit random for the user. If we hit that situation, check if the cwd is one of the projects, and if it isn't, we emit an error and tell the user it's an ambiguous request. Bug: https://crbug.com/gerrit/13070 Change-Id: Id1059b81330229126b48c7312569b37504808383 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/298702 Reviewed-by: Michael Mortensen Tested-by: Mike Frysinger --- subcmds/download.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/subcmds/download.py b/subcmds/download.py index c0c47dd9..81d997e0 100644 --- a/subcmds/download.py +++ b/subcmds/download.py @@ -16,7 +16,7 @@ import re import sys from command import Command -from error import GitError +from error import GitError, NoSuchProjectError CHANGE_RE = re.compile(r'^([1-9][0-9]*)(?:[/\.-]([1-9][0-9]*))?$') @@ -60,6 +60,7 @@ If no project is specified try to use current directory as a project. if m: if not project: project = self.GetProjects(".")[0] + print('Defaulting to cwd project', project.name) chg_id = int(m.group(1)) if m.group(2): ps_id = int(m.group(2)) @@ -76,7 +77,23 @@ If no project is specified try to use current directory as a project. ps_id = max(int(match.group(1)), ps_id) to_get.append((project, chg_id, ps_id)) else: - project = self.GetProjects([a])[0] + projects = self.GetProjects([a]) + if len(projects) > 1: + # If the cwd is one of the projects, assume they want that. + try: + project = self.GetProjects('.')[0] + except NoSuchProjectError: + project = None + if project not in projects: + print('error: %s matches too many projects; please re-run inside ' + 'the project checkout.' % (a,), file=sys.stderr) + for project in projects: + print(' %s/ @ %s' % (project.relpath, project.revisionExpr), + file=sys.stderr) + sys.exit(1) + else: + project = projects[0] + print('Defaulting to cwd project', project.name) return to_get def ValidateOptions(self, opt, args):