diff --git a/tools/check_main_sync.py b/tools/check_main_sync.py index fa3265d310..12d5b78a32 100644 --- a/tools/check_main_sync.py +++ b/tools/check_main_sync.py @@ -97,12 +97,17 @@ if __name__ == "__main__": parser.add_argument("--strict", action="store_true", help="also fail when the branch is behind the base") args = parser.parse_args() try: - base = resolve_base(args.repo, args.base) - if args.fetch: - remote, _, branch = base.partition("/") + def fetch(ref): + remote, _, branch = ref.partition("/") if not branch: parser.error("--fetch needs a remote-tracking base such as upstream/main") git(args.repo, "fetch", "--quiet", remote, branch) + + if args.fetch and args.base: + fetch(args.base) + base = resolve_base(args.repo, args.base) + if args.fetch and not args.base: + fetch(base) data = report(args.repo, base, args.head) except RuntimeError as error: parser.error(str(error)) diff --git a/tools/tests/test_check_main_sync.py b/tools/tests/test_check_main_sync.py index 4a13a56e0a..d893e70b0e 100644 --- a/tools/tests/test_check_main_sync.py +++ b/tools/tests/test_check_main_sync.py @@ -177,6 +177,14 @@ class CheckMainSyncTests(unittest.TestCase): self.assertEqual(result.returncode, 2) self.assertIn("no base ref found", result.stderr) + def test_explicit_base_is_fetched_before_it_is_resolved(self): + with tempfile.TemporaryDirectory() as other: + subprocess.run(["git", "clone", "-q", self.directory.name, other], check=True, capture_output=True) + self.repo.git("remote", "add", "upstream", other) + result = subprocess.run([sys.executable, str(TOOL), "--repo", self.directory.name, "--base", "upstream/main", "--fetch"], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertIn("merge: clean", result.stdout) + if __name__ == "__main__": unittest.main()