From 40dbd9b3d71fec8015be7ac39ba0bb730914b938 Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Tue, 1 May 2012 21:22:31 -0700 Subject: [PATCH 1/2] Add exit_json and fail_json to git module This adds exit_json() and fail_json() to git module. It also sets version to 'HEAD', if not provided. --- library/git | 52 ++++++++++++++++++---------------------------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/library/git b/library/git index 5a25915a3f..78f0e9ab1c 100755 --- a/library/git +++ b/library/git @@ -32,35 +32,34 @@ import sys import shlex import subprocess +# =========================================== +# Basic support methods + +def exit_json(rc=0, **kwargs): + print json.dumps(kwargs) + sys.exit(rc) + +def fail_json(**kwargs): + kwargs['failed'] = True + exit_json(rc=1, **kwargs) + # =========================================== # convert arguments of form a=b c=d # to a dictionary # FIXME: make more idiomatic if len(sys.argv) == 1: - print json.dumps({ - "failed" : True, - "msg" : "the command module requires arguments (-a)" - }) - sys.exit(1) + fail_json(msg="the command module requires arguments (-a)") argfile = sys.argv[1] if not os.path.exists(argfile): - print json.dumps({ - "failed" : True, - "msg" : "Argument file not found" - }) - sys.exit(1) + fail_json(msg="Argument file not found") args = open(argfile, 'r').read() items = shlex.split(args) if not len(items): - print json.dumps({ - "failed" : True, - "msg" : "the command module requires arguments (-a)" - }) - sys.exit(1) + fail_json(msg="the command module requires arguments (-a)") params = {} for x in items: @@ -69,7 +68,7 @@ for x in items: dest = params['dest'] repo = params['repo'] -version = params['version'] +version = params.get('version', 'HEAD') # =========================================== @@ -129,24 +128,14 @@ else: # handle errors from clone or pull if out.find('error') != -1: - print json.dumps({ - "failed" : True, - "out" : out, - "err" : err - }) - sys.exit(1) + fail_json(out=out, err=err) # switch to version specified regardless of whether # we cloned or pulled (out, err) = switchver(version, dest) if err.find('error') != -1: - print json.dumps({ - "failed" : True, - "out" : out, - "err" : err - }) - sys.exit(1) + fail_json(out=out, err=err) # determine if we changed anything @@ -156,9 +145,4 @@ changed = False if before != after: changed = True -print json.dumps({ - "changed" : changed, - "before" : before, - "after" : after -}) - +exit_json(changed=changed, before=before, after=after) From 887d293a3bf77acc8bb6a0b4b9573ad947362045 Mon Sep 17 00:00:00 2001 From: Stephen Fromm Date: Tue, 1 May 2012 21:53:20 -0700 Subject: [PATCH 2/2] 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