Skip to content

Instantly share code, notes, and snippets.

@BrennanMcDonald
Last active May 10, 2022 11:29
Show Gist options
  • Save BrennanMcDonald/f369d50a56b46f80f7f4f7c9820ce918 to your computer and use it in GitHub Desktop.
Save BrennanMcDonald/f369d50a56b46f80f7f4f7c9820ce918 to your computer and use it in GitHub Desktop.
Updates the default branch of a users repos to something less impactful.

Master To Main

A simple script to convert the default name of a user's branches from 'master' to 'main' (or another name specified)

Installing

Place both the main.py and requirements.txt in a directory and run pip install -r requirements.txt.

Usage

main.py -t <token> -r <repos> -n <new_name>

Flag Default Description
"-t", "--token" None A personal access token for a users account.
If omitted, an interactive login prompt will be shown
"-r", "--repos" None A comma separated list of repos to use.
If omitted, all repos will be converted.
"-n", "--name" 'main' The new name for all primary branches.
import sys
import getopt
import getpass
from github import Github
def token_login(token=''):
while (not token):
while(True):
token = raw_input("Token: ")
try:
github_object = Github(token)
print("Authenticated as {0}".format(github_object.get_user().login))
return github_object
except:
print("Token Login Failed, Try again.")
def switch_to_new_branch(repo, new_name='main'):
try:
sb = repo.get_branch("master")
# Create new branch
repo.create_git_ref(ref='refs/heads/main', sha=sb.commit.sha)
# Set new branch to default
repo.edit(name=repo.name, default_branch='main')
# Delete Master Branch
ref = repo.get_git_ref("heads/master")
ref.delete()
print("Successfully converted {0}".format(repo.name))
except Exception as e:
print("Error converting {0}: {1}".format(repo.name, e.data["message"]))
def main(argv):
token = ''
repos = ''
new_name = ''
github_object = {}
try:
opts, args = getopt.getopt(argv,"t:r:n:",["token=","repos=","name="])
except getopt.GetoptError:
print 'main.py -t <token> -r <repos> -n <new_name>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'main.py -t <token> -r <repos> -n <new_name>'
sys.exit()
elif opt in ("-t", "--token"):
token = arg
elif opt in ("-r", "--repos"):
repos = arg
elif opt in ("-n", "--name"):
new_name = arg
if (token):
github_object = token_login(token)
else:
github_object = token_login()
if (repos):
for repo in repos.split(','):
print("Switching {0}".format(repo))
repo = github_object.get_user().get_repo(repo)
switch_to_new_branch(repo, new_name=new_name)
else:
confirm = raw_input("Convert all repos? [Y/n]")
if (confirm == "n"):
sys.exit(1)
for repo in github_object.get_user().get_repos():
if(repo.owner.login == github_object.get_user().login):
switch_to_new_branch(repo, new_name=new_name)
if __name__ == "__main__":
main(sys.argv[1:])
asn1crypto==0.24.0
asyncio==3.4.3
aws-shell==0.2.1
awscli==1.18.84
backports.functools-lru-cache==1.6.1
boto3==1.14.7
botocore==1.17.7
certifi==2020.6.20
chardet==3.0.4
colorama==0.4.3
configobj==5.0.6
cryptography==2.1.4
Deprecated==1.2.10
docutils==0.15.2
dotenv==0.0.5
enum34==1.1.6
futures==3.3.0
gitdb2==2.0.6
idna==2.10
ipaddress==1.0.17
jmespath==0.10.0
keyring==10.6.0
keyrings.alt==3.0
lxml==4.5.0
prompt-toolkit==1.0.18
pyasn1==0.4.8
pycrypto==2.6.1
PyGithub==1.45
Pygments==2.5.2
pygobject==3.26.1
PyJWT==1.7.1
python-dateutil==2.8.1
pyxdg==0.25
PyYAML==5.3.1
requests==2.24.0
rsa==3.4.2
s3transfer==0.3.3
SecretStorage==2.3.1
six==1.15.0
smmap==3.0.4
smmap2==3.0.1
urllib3==1.25.10
wcwidth==0.2.4
wrapt==1.12.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment