From 887d293a3bf77acc8bb6a0b4b9573ad947362045 Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Tue, 1 May 2012 21:53:20 -0700 Subject: [PATCH] Add git reset --hard support to git module Resets working tree to what is in HEAD and discards any uncommitted changes. --- library/git | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/library/git b/library/git index 78f0e9ab1c..7e9c77e0fb 100755 --- a/library/git +++ b/library/git @@ -90,6 +90,19 @@ def clone(repo, dest): cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return cmd.communicate() +def reset(dest): + ''' + Resets the index and working tree to HEAD. + Discards any changes to tracked files in working + tree since that commit. + ''' + os.chdir(dest) + cmd = "git reset --hard HEAD" + cmd = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (out, err) = cmd.communicate() + rc = cmd.returncode + return (rc, out, err) + def pull(repo, dest): ''' updates repo from remote sources ''' os.chdir(dest) @@ -123,6 +136,9 @@ if not os.path.exists(gitconfig): else: # else do a pull before = get_version(dest) + (rc, out, err) = reset(dest) + if rc != 0: + fail_json(out=out, err=err) (out, err) = pull(repo, dest) # handle errors from clone or pull