Commits

Fredrik Håård committed e11c7a9

Examples and formatting

Comments (0)

Files changed (2)

 <reponame>.tests
     Tests to run for the repository named <reponame>
 
+Custom hooks are expected to take five arguments (check the example hook):
+   ui - hg ui object
+   repo - hg repo object
+   user - username
+   start - first revision pushed
+   end - last revision pushed
+
 
 Sample config:
 [hooks]
 repos = orig
 orig.path = /path/to/repo
 orig.tests = has_multiple_heads hookmodule2.hook
+
+
+__________________________________________
+Licensed under GPLv2 since it uses the Mercurial API; I'd rather use 
+MIT (or WTF) license, and I'm not interested in forcing anyone to
+publish anything they don't want to

autohook/autohook.py

 import mercurial.node
 import os.path, sys
 
-    
+def example_hook(ui, repo, user, start, end):
+    """Small, stupid example hook that verifies
+    that the code is really, really tested. For sure.
+    """
+    for rev in xrange(start, end):
+        if not "TESTED!!!!" in repo[rev].description():
+            ui.write_err("Abort: Code not tested!\n")
+            return True
+
 def has_multiple_heads(ui, repo, user, start, end):
     """Check if multiple heads are being added"""
     heads = repo.heads()
 
     
 def hook(ui, repo, url, node, hooktype, pending, **kwargs):
-        def debug(msg): 
-            if ui.configbool('autohook', 'debug') or ui.verbose: 
-                ui.write(msg + "\n")
-        def info(msg):
+    
+    def debug(msg): 
+        if ui.configbool('autohook', 'debug') or ui.verbose: 
             ui.write(msg + "\n")
-        def error(msg):
-            ui.write_err(msg + "\n")
+    def info(msg):
+        ui.write(msg + "\n")
+    def error(msg):
+        ui.write_err(msg + "\n")
         
-        #Get the user
-        user = _get_user(ui)
-        debug("User is %s" % (user,))
+    #Get the user
+    user = _get_user(ui)
+    debug("User is %s" % (user,))
+    
+    #if superuser is set and matches, bypass hooks
+    if user in ui.configlist('autohook', 'superuser'):
+        info("User %s bypassing hooks" % (user,))
+        return False
         
-        #if superuser is set and matches, bypass hooks
-        if user in ui.configlist('autohook', 'superuser'):
-            info("User %s bypassing hooks" % (user,))
-            return False
-        
-        #load configured modules
-        _load_modules(ui)
+    #load configured modules
+    _load_modules(ui)
 
-        #Resolve path and see if we should handle this repo
-        dest = os.path.abspath(pending())
-        reponame = _get_reponame(ui, dest)
-        if not reponame: 
-            info("Not configured for repo at %s" % (dest,))
-            return False
-        debug("Repo name " + reponame)
+    #Resolve path and see if we should handle this repo
+    dest = os.path.abspath(pending())
+    reponame = _get_reponame(ui, dest)
+    if not reponame: 
+        info("Not configured for repo at %s" % (dest,))
+        return False
+    debug("Repo name " + reponame)
 
-        #Get first and last revision
-        start = repo[node].rev()
-        end = len(repo)
+    #Get first and last revision
+    start = repo[node].rev()
+    end = len(repo)
 
         
-        #Run tests, first global and then specific for this repo
-        for test in (ui.configlist('autohook', 'tests') +
-                     ui.configlist('autohook', '%s.tests' % (reponame,))):
-            debug("Running " + test)
-            #Resolve nested names
-            if "." in test:
-                names = test.split(".")
-                test_func = getattr(globals()[".".join(names[:-1])], names[-1])
-            else:
-                test_func = globals()[test]
-            #run test
-            if test_func(ui=ui, repo=repo, user=user, start=start, end=end):
-                error("Test '%s' failed" % (test,))
-                return True
-        #Remember, False means no errors found
-        return False
+    #Run tests, first global and then specific for this repo
+    for test in (ui.configlist('autohook', 'tests') +
+                 ui.configlist('autohook', '%s.tests' % (reponame,))):
+        debug("Running " + test)
+        #Resolve nested names
+        if "." in test:
+            names = test.split(".")
+            test_func = getattr(globals()[".".join(names[:-1])], names[-1])
+        else:
+            test_func = globals()[test]
+        #run test
+        if test_func(ui=ui, repo=repo, user=user, start=start, end=end):
+            error("Test '%s' failed" % (test,))
+            return True
+    #Remember, False means no errors found
+    return False