@@ -0,0 +1,2536 @@
+# coding=utf-8%0A%0Afrom __future__ import print_function%0Afrom subprocess import check_output, call%0Aimport urllib2%0Aimport json%0Aimport sys%0A%0Aif __name__ == '__main__':%0A    %22%22%22Run a bunch of boilerplate commands to sync your local clone to its%0A       parent github repo.%0A%0A    %22%22%22%0A    print(%22Starting sync...%22, %22%5Cn%22)%0A%0A    CURRENT_REPO_CMD = %5B'git', 'config', '--get', 'remote.origin.url'%5D%0A    ADD_REMOTE_CMD = %5B'git', 'remote', 'add', 'upstream'%5D%0A    CHECK_REMOTES_CMD = %5B'git', 'remote', '-v'%5D%0A    FETCH_UPSTREAM_CMD = %5B'git', 'fetch', 'upstream'%5D%0A    CHECKOUT_MASTER_CMD = %5B'git', 'checkout', 'master'%5D%0A    MERGE_UPSTREAM_CMD = %5B'git', 'merge', 'upstream/master'%5D%0A%0A    try:%0A        repo_url = check_output(CURRENT_REPO_CMD)%0A        print(%22Getting repo's url...%22)%0A        print(%22Syncing repo:%22, repo_url)%0A%0A        url_segments = repo_url.split(%22github.com/%22)%0A        path = url_segments%5B1%5D%0A        user, repo = path.split(%22/%22)%0A%0A        print(%22Checking the fork's parent url...%22, %22%5Cn%22)%0A        url = %22https://api.github.com/repos/%7B%7D/%7B%7D%22.format(user, repo)%0A        req = urllib2.urlopen(url)%0A        res = json.load(req)%0A        parent_url = res%5B'parent'%5D%5B'git_url'%5D%0A%0A        print(%22Will add remote to parent repo:%22, parent_url, %22%5Cn%22)%0A        ADD_REMOTE_CMD.append(parent_url)%0A        print(ADD_REMOTE_CMD)%0A        call(ADD_REMOTE_CMD)%0A        print(%22%22)%0A%0A        print(%22Checking remotes...%22, %22%5Cn%22)%0A        call(CHECK_REMOTES_CMD)%0A        print(%22%22)%0A%0A        print(%22Fetching upstream...%22, %22%5Cn%22)%0A        call(FETCH_UPSTREAM_CMD)%0A        print(%22%22)%0A%0A        print(%22Merging upstream and master%22, %22%5Cn%22)%0A        check_output(CHECKOUT_MASTER_CMD)%0A        call(MERGE_UPSTREAM_CMD)%0A        print(%22Syncing done.%22)%0A%0A    except Exception as e:%0A        e_type = sys.exc_info()%5B0%5D.__name__%0A        print(%22The following error happened:%22, e, %22%5Cn%22)%0A%0A        if (e_type == 'CalledProcessError' and%0A            hasattr(e, 'cmd') and%0A            e.cmd == CURRENT_REPO_CMD):%0A            print(%22Are you sure you are on the git repo folder?%22, %22%5Cn%22)%0A        elif (e_type == 'IndexError' and%0A            e.message == 'list index out of range'):%0A            print(%22Sorry, couldn't get the user and repo names from the Git config.%22, %22%5Cn%22)%0A        elif (e_type == 'KeyError' and%0A            e.message == 'parent'):%0A            print(%22Are you sure the repo is a fork?%22)%0A        elif (e_type == 'CalledProcessError' and%0A            (e.cmd == MERGE_UPSTREAM_CMD or e.cmd == CHECKOUT_MASTER_CMD)):%0A            print(%22Didn't merge. Reason:%22, e.output)%0A        print(%22Game Over.%22)%0A

Create gsync.py