From 9af690a59a82f61ed8b71a65f6a4cfa81c3b1a86 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 25 Feb 2022 11:58:02 -0800 Subject: [PATCH 1/3] Check if branch exists in at least one repo Signed-off-by: Aditya --- ros2_batch_job/__main__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 9be57ab90..8a7585ec9 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -654,6 +654,10 @@ def run(args, build_function, blacklisted_package_names=None): info('Attempting to create a well known branch name for all the default branches') job.run(vcs_cmd + ['custom', '.', '--git', '--args', 'checkout', '-b', '__ci_default']) + # Check if the branch exists in at least one repo from the given repos file + for filename in repos_filenames: + branch_found = check_branch_exists(filename, args.test_branch) + if not branch_found: return False # Attempt to switch all the repositories to a given branch info("Attempting to switch all repositories to the '{0}' branch" .format(args.test_branch)) @@ -777,5 +781,25 @@ def _fetch_repos_file(url, filename, job): with open(filename, 'r') as f: print(f.read()) +# Checks if the given branch exists in at least one repo +def check_branch_exists(filename, branch): + # Populate all .git repos + lines = [] + with open(filename) as f: lines = f.readlines() + + urls = [] + for line in lines: + text = line.strip() + if text[-4:] == ".git": + urls.append(text.replace("url: ", "")) + + for repo in urls : + # Check if 'branch' exists in 'repo' + ret = os.system("git ls-remote --exit-code --heads " + repo + " " + branch) + print(repo, branch, ret) + if not ret : return True + + return False + if __name__ == '__main__': sys.exit(main()) From 118745e39a30a0b923f88f4482a35a33209f30c0 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 25 Feb 2022 12:03:27 -0800 Subject: [PATCH 2/3] Added warning if the branch is not found Signed-off-by: Aditya --- ros2_batch_job/__main__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index 8a7585ec9..dab3022b3 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -657,7 +657,8 @@ def run(args, build_function, blacklisted_package_names=None): # Check if the branch exists in at least one repo from the given repos file for filename in repos_filenames: branch_found = check_branch_exists(filename, args.test_branch) - if not branch_found: return False + if not branch_found: + print('Warning: None of the repos in ', filename, ' contain the branch ', args.test_branch) # Attempt to switch all the repositories to a given branch info("Attempting to switch all repositories to the '{0}' branch" .format(args.test_branch)) From 9bccbffa3bf4ccf7780b59520f76038c5f52e047 Mon Sep 17 00:00:00 2001 From: Aditya Date: Fri, 25 Feb 2022 12:05:07 -0800 Subject: [PATCH 3/3] Removed debug prints Signed-off-by: Aditya --- ros2_batch_job/__main__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/ros2_batch_job/__main__.py b/ros2_batch_job/__main__.py index dab3022b3..f46b0a7eb 100644 --- a/ros2_batch_job/__main__.py +++ b/ros2_batch_job/__main__.py @@ -797,7 +797,6 @@ def check_branch_exists(filename, branch): for repo in urls : # Check if 'branch' exists in 'repo' ret = os.system("git ls-remote --exit-code --heads " + repo + " " + branch) - print(repo, branch, ret) if not ret : return True return False