Commits

Fredrik Håård  committed de9ccea

porting to hgapi

  • Participants
  • Parent commits e11c7a9
  • Branches hgapi

Comments (0)

Files changed (1)

File autohook/autohook.py

- # -*- coding: utf-8 -*-
-import mercurial.node
+# -*- coding: utf-8 -*-
 import os.path, sys
+import hgapi
 
-def example_hook(ui, repo, user, start, end):
+def example_hook(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")
+        if not "TESTED!!!!" in repo[rev].desc:
+            print("Abort: Code not tested!\n")
+            print("Comment was: %s" % (repo[rev].desc,)) 
             return True
 
-def has_multiple_heads(ui, repo, user, start, end):
+def has_multiple_heads(repo, user, start, end):
     """Check if multiple heads are being added"""
-    heads = repo.heads()
+    heads = repo.hg_heads()
     count = 0
     for rev in xrange(start, end):
-        if repo[rev] in heads:
+        if repo[rev].node in heads:
             count += 1
-    ui.debug("Found %d heads\n" % (count+1,))
+    print("Found %d heads\n" % (count+1,))
     return count > 1
     
 
-def adds_branch(ui, repo, user, start, end):
+def adds_branch(repo, user, start, end):
     """Check if a set of changes adds a new branch
     Returns True if any revision in repo[start:end] does
     not share branch with any parent"""
-    heads = repo.heads()
+    heads = repo.hg_heads()
     
     for rev in xrange(start, end):
-        revbranch = repo[rev].branch()
+        revbranch = repo[rev].branch
         one_parent_ok = False
-        for parent in repo[rev].parents():
-            if parent.branch() == revbranch:
+        for parent in repo[rev].parents:
+            if repo[parent].branch == revbranch:
                 one_parent_ok = True
         if not one_parent_ok:
-            ui.warn("Rev %s adds a branch" % (repo[rev].hex()))
+            print("Rev %s adds a branch" % (repo[rev].node))
             return True
     return False
 
-def _get_user(ui):
-    """Get username from, in order: REMOTE_USER from ui.environ, 
-    USER, USERNAME from os.environ"""
-    import os
-    if 'REMOTE_USER' in ui.environ: #remote
-        user = ui.environ['REMOTE_USER']
-    elif 'USER' in os.environ: #unixes
-        user = os.environ['USER']
-    elif 'USERNAME' in os.environ: #windows
-        user = os.environ['USERNAME']
+def _get_user(env):
+    """Get username from, in order: REMOTE_USER, 
+    USER, USERNAME from the env given"""
+
+    if 'REMOTE_USER' in env: #remote
+        user = env['REMOTE_USER']
+    elif 'USER' in env: #unixes
+        user = env['USER']
+    elif 'USERNAME' in env: #windows
+        user = env['USERNAME']
     return user
 
 
-def _get_reponame(ui, dest):
+def _get_reponame(repo, dest):
     """Get configured name of this repository"""
-    for tmp in ui.configlist('autohook', 'repos'):
-        if os.path.abspath(ui.config('autohook', "%s.path" % tmp)) == dest:
+    for tmp in repo.configlist('autohook', 'repos'):
+        if os.path.abspath(repo.config('autohook', "%s.path" % tmp)) == dest:
             return tmp
     return None
 
     globals()[name] = imp.load_module(name, *mod)
 
 
-def _load_modules(ui):
+def _load_modules(repo):
     """Load modules configured in autohook.loadmodules"""
-    for module in ui.configlist("autohook", "loadmodules"):
-        ui.debug("Loading %s" % (module,))
-        _load_module(module, ui.configlist("autohook", "loadpath"))
+    for module in repo.configlist("autohook", "loadmodules"):
+        print("Loading %s" % (module,))
+        _load_module(module, repo.configlist("autohook", "loadpath"))
 
     
-def hook(ui, repo, url, node, hooktype, pending, **kwargs):
+def hook(repo, url, node, pending, env):
     
     def debug(msg): 
-        if ui.configbool('autohook', 'debug') or ui.verbose: 
-            ui.write(msg + "\n")
+        if repo.configbool('autohook', 'debug'): 
+            print(msg + "\n")
     def info(msg):
-        ui.write(msg + "\n")
+        print(msg + "\n")
     def error(msg):
-        ui.write_err(msg + "\n")
+        print(msg + "\n")
         
     #Get the user
-    user = _get_user(ui)
+    user = _get_user(env)
     debug("User is %s" % (user,))
     
     #if superuser is set and matches, bypass hooks
-    if user in ui.configlist('autohook', 'superuser'):
+    if user in repo.configlist('autohook', 'superuser'):
         info("User %s bypassing hooks" % (user,))
         return False
         
     #load configured modules
-    _load_modules(ui)
+    _load_modules(repo)
 
     #Resolve path and see if we should handle this repo
-    dest = os.path.abspath(pending())
-    reponame = _get_reponame(ui, dest)
+    dest = os.path.abspath(pending)
+    reponame = _get_reponame(repo, dest)
     if not reponame: 
         info("Not configured for repo at %s" % (dest,))
         return False
 
     #Get first and last revision
     start = repo[node].rev()
-    end = len(repo)
-
+    end = repo['tip'].rev()
         
     #Run tests, first global and then specific for this repo
-    for test in (ui.configlist('autohook', 'tests') +
-                 ui.configlist('autohook', '%s.tests' % (reponame,))):
+    for test in (repo.configlist('autohook', 'tests') +
+                 repo.configlist('autohook', '%s.tests' % (reponame,))):
         debug("Running " + test)
         #Resolve nested names
         if "." in test:
         else:
             test_func = globals()[test]
         #run test
-        if test_func(ui=ui, repo=repo, user=user, start=start, end=end):
+        if test_func(repo=repo, user=user, start=start, end=end):
             error("Test '%s' failed" % (test,))
             return True
     #Remember, False means no errors found
     return False
+
+
+if __name__ == "__main__":
+    import os
+    #for key in os.environ: #[x for x in os.environ if x.startswith("HG") ]:
+    #    print("%s: %s" % (key, os.environ[key]))
+
+    repo = hgapi.Repo(os.path.abspath(os.curdir))
+    node = os.environ['HG_NODE']
+    url = os.environ['HG_URL']
+    pending = os.environ['HG_PENDING']
+    print(int(hook(repo, node, url, pending, os.environ)))
+    sys.exit(1)