Skip to content

Instantly share code, notes, and snippets.

@OzTamir
Created February 10, 2014 13:49
Show Gist options
  • Save OzTamir/8916271 to your computer and use it in GitHub Desktop.
Save OzTamir/8916271 to your computer and use it in GitHub Desktop.
Little Python script to mimic GitHub's GUI 'Sync' button - The code doesn't work, but it does demonstrate some nice concepts
import subprocess
#TES
# -- Global Variables Declarations -- #
USER = 'Oz Tamir'
EMAIL = '[email protected]'
#REPO = 'https://' + USER + '@github.com/OzTamir/git-fix.git'
# Changed files
modified = []
# Deleted files to remove from remote
deleted = []
# Added files (Untracked at first, but added while pasring output)
added = []
# ----------------------------------- #
def git_discard():
'''Unstage changes'''
subprocess.call(['git', 'reset', 'HEAD', '.'], shell=True)
def git_push():
'''Push local changes to remote'''
subprocess.call(['git', 'push', 'origin', 'master'], shell=True)
def git_commit(message = "Being lazy, No commit message!"):
'''Commit staged changes'''
subprocess.call(['git', 'commit', '-m', message], shell=True)
def git_pull():
'''Pull updates from remote (and rebase)'''
subprocess.call(['git', 'pull', '--rebase'], shell=True)
def git_sync():
'''Mimic GitHub GUI's 'Sync' Button'''
set_gitconfig()
git_pull()
commit_msg = raw_input('Enter commit message:')
if len(commit_msg) < 1:
git_commit()
else:
git_commit(commit_msg)
git_push()
print 'Changed sent!'
def stage_file(filename):
''' aka git add <filename> '''
subprocess.call(['git', 'add', filename], shell=True)
def print_status():
''' Print the git status with style'''
global modified, deleted, added
print 'Git Status:'
if len(modified) != 0:
print ' Modified:'
for changed_file in modified:
print ' - ' + changed_file
print ''
if len(added) != 0:
print ' Added:'
for added_file in added:
print ' - ' + added_file
print ''
if len(deleted) != 0:
print ' Deleted:'
for deleted_file in deleted:
print ' - ' + deleted_file
print ''
def git_status():
'''Get the status of the local git repo'''
global modified, deleted, added
# A Dictionary with status-list pairs
delta = {'M' : modified, 'D' : deleted, '?' : added, 'A' : added}
# Get the output of the 'git status -s' CMD command as a string
status_out = subprocess.check_output(['git', 'status', '-s'])
# Split recived output to lines
files = status_out.split('\n')
# Sort the filenames to their appropriate list
for filename in files:
# Avoid errors (last line of output might be '')
if len(filename) < 1:
continue
# Remove all spaces in filename
filename = filename.replace(' ', '')
# Get the status and remove it from file name
file_status = filename[0]
filename = filename[1:]
# Untracked files are prefixed by '??' so we need to remove one '?'
if file_status == '?':
filename = filename[1:]
# If status unfamilier, ignore the file
# TODO: add 'other' list
# TODO: Dispose of unwanted extansions here
if file_status not in delta.keys():
continue
delta[file_status].append(filename)
stage_file(filename)
if __name__ == '__main__':
git_status()
print_status()
while True:
continue_prompt = raw_input('Would you wish to upload changes (Y/N)?')
if len(continue_prompt) == 1:
if continue_prompt == 'Y':
git_sync()
break
elif continue_prompt == 'N':
git_discard()
git_pull()
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment