Skip to content

Instantly share code, notes, and snippets.

@ryanlerch
Last active January 7, 2021 09:56
Show Gist options
  • Save ryanlerch/039ecf735f93576bff002884d7d232bb to your computer and use it in GitHub Desktop.
Save ryanlerch/039ecf735f93576bff002884d7d232bb to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Check post-receive hook.
This script checks if the post-receive hook in place is a symlink to
the desired post-receive hook defined below in the TARGET variable.
"""
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment