Skip to content

Instantly share code, notes, and snippets.

@thirstydevil
Created October 16, 2018 08:48
Show Gist options
  • Save thirstydevil/c78ccd8a842dde143046633228adc098 to your computer and use it in GitHub Desktop.
Save thirstydevil/c78ccd8a842dde143046633228adc098 to your computer and use it in GitHub Desktop.
Git - force pull latest repro
import git, os, shutil
DIR_NAME = "c:/temp2"
REMOTE_URL = "https://gitlab.com/blarblar/blar.git"
# This is a note to myself on how to pull down to a local folder the latest code from a remote branch.
class Progress(git.remote.RemoteProgress):
"""
git python provides a callback object to get info from pull, clone, fetch etc
"""
# def update(self, op_code, cur_count, max_count=None, message=''):
# print 'update(%s, %s, %s, %s)'%(op_code, cur_count, max_count, message)
def update(self, op_code, cur_count, max_count=None, message=''):
print self._cur_line
# 1st time make the DIR
if not os.path.exists(DIR_NAME):
os.mkdir(DIR_NAME)
# Init the repro, this is fine if the repro already exists
repo = git.Repo.init(DIR_NAME)
if not repo.remotes:
# If there isn't a remote assigned then lets bind the URL to this repro
origin = repo.create_remote('origin',REMOTE_URL)
origin.fetch()
origin.pull(origin.refs[0].remote_head)
else:
# Repro exists on disk and has a remote path.
# TODO : Should check if the remote exists!!
head = repo.heads[0]
if repo.is_dirty():
# We've changed local files. I don't want this, having local changes could
# mean we will get bugs in user code. Lets force update the local repro
head.checkout(force=True)
if repo.untracked_files:
# user has added files. Now this is a grey area as some might be settings files we want
# to keep. Not sure if .gitignore files will be returned in this list. If so we need to deal with them, like python .pyc files
git = repo.git
git.clean('-xdf')
# commits_behind = [x for x in repo.iter_commits('master..origin/master')]
# commits_ahead = [x for x in repo.iter_commits('origin/master..master')]
# print commits_behind, commits_ahead
# Have to use fetch to know if we're out of date!
repo.remotes[0].fetch()
diff = repo.git.diff("master", "origin/master")
if diff:
# This should be checking if the local brach is different to the remote.
# This could be done on a tread and a GUI could tell an artist to get latest Tech Art deployed code
repo.git.pull('origin', 'master') # from -> to or remote(origin) to local(master)
print "---- DONE ----"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment