From f6f962be5ef0b8745e50477a8c6de6041ac70712 Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Jan 07 2021 10:11:58 +0000 Subject: [PATCH 1/3] add rename branches script --- diff --git a/rename_branches.py b/rename_branches.py new file mode 100644 index 0000000..258895a --- /dev/null +++ b/rename_branches.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python3 + +""" +rename branches + +""" + +import argparse +import logging +import os +import sys + +logging.basicConfig(stream=sys.stderr) +_log = logging.getLogger(__name__) + + +RENAME_FROM = "master" +RENAME_TO = "rawhide" + +def _rename_branch(folder, symlinks, dryrun): + _log.info(f"Renaming {folder} branch from {RENAME_FROM} to {RENAME_TO}") + heads_path = os.path.join(folder, "refs/heads") + if not dryrun: + os.rename(os.path.join(heads_path, RENAME_FROM), os.path.join(heads_path, RENAME_TO)) + for symlink in symlinks: + _log.info(f"Creating a symbolic reference from {RENAME_TO} to {symlink}") + if not dryrun: + os.symlink(os.path.join(heads_path, RENAME_TO), os.path.join(heads_path, symlink)) + +def main(folder, namespace, symlinks=[], dryrun=False): + for root, dirs, files in os.walk(os.path.join(folder, namespace)): + for name in dirs: + if name.endswith(".git"): + _rename_branch(os.path.join(root, name), symlinks, dryrun) + return 0 + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=f"rename branches in the git repos present " + "in the specified folder from {RENAME_FROM} to {RENAME_TO}" + ) + parser.add_argument( + "folder", + help="Folder where to look for git repos" + ) + parser.add_argument( + "namespace", + help="Namespace to work on" + ) + parser.add_argument( + "--symlink", + dest="symlinks", + action="append", + help=f"symlink the {RENAME_TO} branch the name supplied here" + ) + parser.add_argument( + "--dryrun", + dest="dryrun", + action="store_true", + default=False, + help="Don't actually change the branch names" + ) + parser.add_argument( + "--debug", + dest="debug", + action="store_true", + default=False, + help="Print the debugging output", + ) + args = parser.parse_args() + _log.setLevel(logging.INFO) + if args.debug: + _log.setLevel(logging.DEBUG) + + sys.exit(main(args.folder, args.namespace, symlinks=args.symlinks, dryrun=args.dryrun)) From b9419634904e03a23c390627f1811384e7f0553b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 07 2021 14:48:11 +0000 Subject: [PATCH 2/3] Adjust a little more the rename_branches script - Make it update the HEAD so it points to rawhide instead of master - Make it use git symlinks instead of filesystem symlinks - Only create symlinks if the user specify some - Make the logging a little more descriptive as to what it does Signed-off-by: Pierre-Yves Chibon --- diff --git a/rename_branches.py b/rename_branches.py index 258895a..37c2e22 100644 --- a/rename_branches.py +++ b/rename_branches.py @@ -17,21 +17,38 @@ _log = logging.getLogger(__name__) RENAME_FROM = "master" RENAME_TO = "rawhide" + +def _update_heads(folder, dryrun): + heads_path = os.path.join(folder, "HEAD") + _log.info(f"Updating {heads_path} to 'ref: refs/heads/{RENAME_TO}'") + if not dryrun: + with open(heads_path, 'w') as stream: + stream.write(f"ref: refs/heads/{RENAME_TO}") + + def _rename_branch(folder, symlinks, dryrun): - _log.info(f"Renaming {folder} branch from {RENAME_FROM} to {RENAME_TO}") heads_path = os.path.join(folder, "refs/heads") + src = os.path.join(heads_path, RENAME_FROM) + target = os.path.join(heads_path, RENAME_TO) + _log.info(f"Renaming {src} branch to {target}") if not dryrun: - os.rename(os.path.join(heads_path, RENAME_FROM), os.path.join(heads_path, RENAME_TO)) - for symlink in symlinks: - _log.info(f"Creating a symbolic reference from {RENAME_TO} to {symlink}") - if not dryrun: - os.symlink(os.path.join(heads_path, RENAME_TO), os.path.join(heads_path, symlink)) + os.rename(src, target) + + if symlinks: + for symlink in symlinks: + symlink_path = os.path.join(heads_path, symlink) + _log.info(f"Updating {symlink_path} to 'ref: refs/heads/{RENAME_TO}'") + if not dryrun: + with open(symlink_path, 'w') as stream: + stream.write(f"ref: refs/heads/{RENAME_TO}") + def main(folder, namespace, symlinks=[], dryrun=False): for root, dirs, files in os.walk(os.path.join(folder, namespace)): for name in dirs: if name.endswith(".git"): _rename_branch(os.path.join(root, name), symlinks, dryrun) + _update_heads(os.path.join(root, name), dryrun) return 0 From 6dec5f51bc8ef15d362fa8ce651ba210fc399347 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Jan 07 2021 16:11:04 +0000 Subject: [PATCH 3/3] rename_branches: allow to re-run the script Basically with this commit we can re-run the script if it crashed and it won't proceed what has already been processed. Signed-off-by: Pierre-Yves Chibon --- diff --git a/rename_branches.py b/rename_branches.py index 37c2e22..d23ebb3 100644 --- a/rename_branches.py +++ b/rename_branches.py @@ -30,6 +30,10 @@ def _rename_branch(folder, symlinks, dryrun): heads_path = os.path.join(folder, "refs/heads") src = os.path.join(heads_path, RENAME_FROM) target = os.path.join(heads_path, RENAME_TO) + if not os.path.exists(src): + _log.info(f"Skipping {src} - not found") + return + _log.info(f"Renaming {src} branch to {target}") if not dryrun: os.rename(src, target)