Skip to content

Instantly share code, notes, and snippets.

@bitmingw
Last active April 11, 2024 07:33
Show Gist options
  • Save bitmingw/69bfee10976a68078562a1f881eed5ab to your computer and use it in GitHub Desktop.
Save bitmingw/69bfee10976a68078562a1f881eed5ab to your computer and use it in GitHub Desktop.
Auto update local repository with remote github repository.
#!/usr/bin/env python3
"""
Author: Ming Wen ([email protected])
This script is used to track the changes in the github, and if a new commit
is found, update the local repository accordingly.
"""
from urllib.request import urlopen
from http.client import HTTPResponse
from subprocess import check_output
import json
import os
import sys
# Configurations
USERNAME = ""
REPO = ""
BRANCH = "master"
LOCAL_DIR = ""
def github_sync(directory):
os.chdir(directory)
remote_sha = fetch_remove_sha()
local_sha = fetch_local_sha()
if remote_sha != local_sha:
check_output(["git", "pull", "origin", BRANCH])
print("The local repo has been updated")
return 1;
else:
print("The local repo is already up-to-date")
return 0;
def fetch_remove_sha():
req_url = "https://api.github.com/repos/" + \
USERNAME + "/" + REPO + "/branches/" + BRANCH
resp = urlopen(req_url)
resp_str = str(resp.read(), encoding="utf-8")
resp_data = json.loads(resp_str);
remote_sha = resp_data["commit"]["sha"]
return remote_sha
def fetch_local_sha():
check_output(["git", "checkout", BRANCH])
local_sha = str(check_output(["git", "rev-parse", "HEAD"]), encoding="utf-8")
return local_sha[:-1] # remove newline
if __name__ == "__main__":
sys.exit(github_sync(LOCAL_DIR))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment