Skip to content

Instantly share code, notes, and snippets.

@sbesson
Last active October 6, 2016 13:39
Show Gist options
  • Select an option

  • Save sbesson/3e4f20b4bbb390f077f1 to your computer and use it in GitHub Desktop.

Select an option

Save sbesson/3e4f20b4bbb390f077f1 to your computer and use it in GitHub Desktop.
Script to initialize a snoopy fork of a new top-level repository
#! /usr/bin/env python
import scc.git
import time
# Create GitHub connection
token = scc.git.get_token()
gh = scc.git.get_github(token)
username = gh.get_user().login
print "Authenticated as %s" % username
# Initialize the local repository
git_repo = gh.git_repo('.')
reponame = git_repo.origin.name
# Fork the repository
print "Forking the %s repository" % str(git_repo.origin)
fork = gh.get_user().create_fork(git_repo.origin.repo)
if fork.name != reponame:
print "Renaming the repository as %s" % reponame
fork.edit(reponame)
# Define remote URL for pushing to the fork using the token
forkremote = "https://%s@github.com/%s/%s.git" % (token, username, reponame)
olddefault = fork.default_branch
newdefault = "%s/merge/daily" % olddefault
# Create temporary branch to override default branch
print "Creating tmp branch and updating the default branch"
git_repo.push_branch("HEAD:refs/heads/tmp", forkremote)
time.sleep(5)
fork.edit(reponame, default_branch="tmp")
# Replace main branch by develop/merge/daily
print "Removing remote %s branch" % olddefault
git_repo.push_branch(":refs/heads/%s" % olddefault, forkremote, force=True)
print "Creating %s branch" % newdefault
git_repo.push_branch("HEAD:refs/heads/%s" % newdefault, forkremote, force=True)
time.sleep(5)
print "Changing default branch to %s" % newdefault
fork.edit(reponame, default_branch="%s" % newdefault)
print "Deleting tmp branch"
git_repo.push_branch(":refs/heads/tmp", forkremote)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment