From f369931089f4c7ad0afdf50396f7a84ab265b70f Mon Sep 17 00:00:00 2001 From: n2omatt Date: Sun, 19 Nov 2017 17:11:45 -0200 Subject: [PATCH 1/4] - Add the hook flags. --- gitcheck/gitcheck.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gitcheck/gitcheck.py b/gitcheck/gitcheck.py index 448f0e9..17786ca 100755 --- a/gitcheck/gitcheck.py +++ b/gitcheck/gitcheck.py @@ -472,7 +472,8 @@ def main(): "vhrubw:i:d:m:q:e:al:", [ "verbose", "debug", "help", "remote", "untracked", "bell", "watch=", "ignore-branch=", - "dir=", "maxdepth=", "quiet", "email", "init-email", "all-branch", "localignore=" + "dir=", "maxdepth=", "quiet", "email", "init-email", "all-branch", "localignore=", + "push-hook=", "pull-hook=" ] ) except getopt.GetoptError as e: @@ -527,6 +528,10 @@ def main(): elif opt in ["-h", "--help"]: usage() sys.exit(0) + elif opt in ["--push-hook"]: + argopts["push-hook"] = arg + elif opt in ["--pull-hook"]: + argopts["pull-hook"] = arg # else: # print "Unhandled option %s" % opt # sys.exit(2) From 98532f5e9f693d1e5724ce6ff39fad3160d9d015 Mon Sep 17 00:00:00 2001 From: n2omatt Date: Sun, 19 Nov 2017 17:12:04 -0200 Subject: [PATCH 2/4] - Check if the hooks are valid. --- gitcheck/gitcheck.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gitcheck/gitcheck.py b/gitcheck/gitcheck.py index 17786ca..c8374fe 100755 --- a/gitcheck/gitcheck.py +++ b/gitcheck/gitcheck.py @@ -445,6 +445,16 @@ def readDefaultConfig(): pass +def checkHooks(): + if "push-hook" in argopts and not os.path.isfile(argopts["push-hook"]): + print("push-hook isn't a valid file: %s" % argopts["push-hook"]) + argopts["push-hook"] = None; + + if "pull-hook" in argopts and not os.path.isfile(argopts["pull-hook"]): + print("pull-hook isn't a valid file: %s" % argopts["pull-hook"]) + argopts["pull-hook"] = None; + + def usage(): print("Usage: %s [OPTIONS]" % (sys.argv[0])) print("Check multiple git repository in one pass") @@ -538,6 +548,7 @@ def main(): while True: try: + checkHooks() gitcheck() if argopts.get('email', False): From 21b30aa3b06cf8d0d21b713de922688dbd4a4ed0 Mon Sep 17 00:00:00 2001 From: n2omatt Date: Sun, 19 Nov 2017 17:12:50 -0200 Subject: [PATCH 3/4] - Refactor make the repo name relative since it's used more the one place now. --- gitcheck/gitcheck.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/gitcheck/gitcheck.py b/gitcheck/gitcheck.py index c8374fe..ad2f738 100755 --- a/gitcheck/gitcheck.py +++ b/gitcheck/gitcheck.py @@ -68,6 +68,23 @@ def showDebug(mess, level='info'): if argopts.get('debugmod', False): print(mess) +def makeRepoNameRelative(rep): + # Remove trailing slash from repository/directory name + if rep[-1:] == '/': + rep = rep[:-1] + + # Do some magic to not show the absolute path as repository name + # Case 1: script was started in a directory that is a git repo + if rep == os.path.abspath(os.getcwd()): + (head, tail) = os.path.split(rep) + if tail != '': + return tail + # Case 2: script was started in a directory with possible subdirs that contain git repos + elif rep.find(os.path.abspath(os.getcwd())) == 0: + return rep[len(os.path.abspath(os.getcwd())) + 1:] + # Case 3: script was started with -d and above cases do not apply + else: + return rep # Search all local repositories from current directory def searchRepositories(): @@ -150,22 +167,7 @@ def checkRepository(rep, branch): count ) if ischange or not argopts.get('quiet', False): - # Remove trailing slash from repository/directory name - if rep[-1:] == '/': - rep = rep[:-1] - - # Do some magic to not show the absolute path as repository name - # Case 1: script was started in a directory that is a git repo - if rep == os.path.abspath(os.getcwd()): - (head, tail) = os.path.split(rep) - if tail != '': - repname = tail - # Case 2: script was started in a directory with possible subdirs that contain git repos - elif rep.find(os.path.abspath(os.getcwd())) == 0: - repname = rep[len(os.path.abspath(os.getcwd())) + 1:] - # Case 3: script was started with -d and above cases do not apply - else: - repname = rep + repname = makeRepoNameRelative(rep) if ischange: prjname = "%s%s%s" % (colortheme['prjchanged'], repname, colortheme['default']) From 06d6b5d9c3ce3ce6b1ae545ac3f5391a39af8a96 Mon Sep 17 00:00:00 2001 From: n2omatt Date: Sun, 19 Nov 2017 17:13:20 -0200 Subject: [PATCH 4/4] - Run the hooks if user specified them. --- gitcheck/gitcheck.py | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/gitcheck/gitcheck.py b/gitcheck/gitcheck.py index ad2f738..8f8b381 100755 --- a/gitcheck/gitcheck.py +++ b/gitcheck/gitcheck.py @@ -350,6 +350,17 @@ def gitExec(path, cmd): return output.decode('utf-8') +def hookExec(hook, repo): + relative_name = makeRepoNameRelative(repo); + + commandToExecute = "%s %s" % (argopts[hook], repo) + cmdargs = shlex.split(commandToExecute) + p = subprocess.Popen(cmdargs, stdout=PIPE, stderr=PIPE) + output, errors = p.communicate() + if p.returncode and not argopts.get('quiet', False): + print('Failed running (%s) on repo %s' % (hook, relative_name)) + + # Check all git repositories def gitcheck(): showDebug("Global Vars: %s" % argopts) @@ -368,13 +379,25 @@ def gitcheck(): showDebug("Processing repositories... please wait.") for r in repo: - if (argopts.get('checkall', False)): - branch = getAllBranches(r) - else: - branch = getDefaultBranch(r) - for b in branch: - if checkRepository(r, b): - actionNeeded = True + hook_ran = False; + + if argopts.get("pull-hook", False): + hookExec("pull-hook", r); + hook_ran = True; + + if argopts.get("push-hook", False): + hookExec("push-hook", r); + hook_ran = True; + + if not hook_ran: + if (argopts.get('checkall', False)): + branch = getAllBranches(r) + else: + branch = getDefaultBranch(r) + for b in branch: + if checkRepository(r, b): + actionNeeded = True + html.timestamp = strftime("%Y-%m-%d %H:%M:%S") html.msg += "\n

Report created on %s

\n" % html.timestamp