From 1cb99c2fd62e4c77f4a7103e544ea102de597f6e Mon Sep 17 00:00:00 2001 From: shane knapp Date: Mon, 9 Sep 2024 16:00:21 -0700 Subject: [PATCH] adding branch creation and push functions --- ...c-image-repos.py => manage-image-repos.py} | 80 ++++++++++++++++++- 1 file changed, 76 insertions(+), 4 deletions(-) rename scripts/user-image-management/{sync-image-repos.py => manage-image-repos.py} (63%) diff --git a/scripts/user-image-management/sync-image-repos.py b/scripts/user-image-management/manage-image-repos.py similarity index 63% rename from scripts/user-image-management/sync-image-repos.py rename to scripts/user-image-management/manage-image-repos.py index c875b67d7..ada548d6a 100755 --- a/scripts/user-image-management/sync-image-repos.py +++ b/scripts/user-image-management/manage-image-repos.py @@ -73,19 +73,67 @@ def sync(args): if args.push: if not args.origin: - origin = "origin" + remote = "origin" else: - origin = args.origin - print(f"Pushing {name} to {origin}...") - subprocess.check_call(["git", "push", origin, "main"], cwd=path) + remote = args.origin + print(f"Pushing {name} to {remote}...") + subprocess.check_call(["git", "push", remote, "main"], cwd=path) print() +def branch(args): + """ + Create a new branch in all repositories in the config file. + """ + with open(args.config) as f: + for line in f: + line = line.strip() + if not line or line.startswith("#"): + continue + name = line.split("/")[-1].replace(".git", "") + path = os.path.join(args.destination, name) + if not os.path.exists(path): + print(f"Skipping {name} as it doesn't exist.") + continue + print(f"Creating new branch in {name} in {path}...") + subprocess.check_call(["git", "switch", "-c", args.branch], cwd=path) + print() + +def push(args): + """ + Push all repositories in the config file to a remote. + """ + if not args.branch: + print("Please specify a branch to push.") + sys.exit(1) + + with open(args.config) as f: + for line in f: + line = line.strip() + if not line or line.startswith("#"): + continue + name = line.split("/")[-1].replace(".git", "") + path = os.path.join(args.destination, name) + if not os.path.exists(path): + print(f"Skipping {name} as it doesn't exist.") + continue + print(f"Pushing {name} to {args.origin}...") + if not args.origin: + remote = "origin" + else: + remote = args.origin + subprocess.check_call(["git", "push", remote, args.branch], cwd=path) + print() + def main(args): if args.command == "clone": clone(args) elif args.command == "sync": sync(args) + elif args.command == "branch": + branch(args) + elif args.command == "push": + push(args) if __name__ == "__main__": argparser = argparse.ArgumentParser() @@ -137,5 +185,29 @@ def main(args): help="GitHub user to set the origin to." ) + branch_parser = subparsers.add_parser( + "branch", + help="Create a new feature branch in all image repositories." + ) + branch_parser.add_argument( + "-b", + "--branch", + help="Name of the new feature branch to create." + ) + + push_parser = subparsers.add_parser( + "push", + help="Push all image repositories to a remote." + ) + push_parser.add_argument( + "-o", + "--origin", + help="Origin to push to. This is optional and defaults to 'origin'." + ) + push_parser.add_argument( + "-b", + "--branch", + help="Name of the branch to push." + ) args = argparser.parse_args() main(args)