changeset 13617:9cb1a42cd4b3

eol: rename hook into checkheadshook, add checkallhook (issue2665) "hook" is still an alias for "checkheadshook".
author Patrick Mezard <pmezard@gmail.com>
date Sun, 13 Mar 2011 15:07:44 +0100
parents e6f93ca9ce86
children b217619a6cf5
files hgext/eol.py tests/test-eol-hook.t
diffstat 2 files changed, 92 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/eol.py	Sun Mar 13 15:07:44 2011 +0100
+++ b/hgext/eol.py	Sun Mar 13 15:07:44 2011 +0100
@@ -73,9 +73,13 @@
 only need to these filters until you have prepared a ``.hgeol`` file.
 
 The ``win32text.forbid*`` hooks provided by the win32text extension
-have been unified into a single hook named ``eol.hook``. The hook will
-lookup the expected line endings from the ``.hgeol`` file, which means
-you must migrate to a ``.hgeol`` file first before using the hook.
+have been unified into a single hook named ``eol.checkheadshook``. The
+hook will lookup the expected line endings from the ``.hgeol`` file,
+which means you must migrate to a ``.hgeol`` file first before using
+the hook. ``eol.checkheadshook`` only checks heads, intermediate
+invalid revisions will be pushed. To forbid them completely, use the
+``eol.checkallhook`` hook. These hooks are best used as
+``pretxnchangegroup`` hooks.
 
 See :hg:`help patterns` for more information about the glob patterns
 used.
@@ -200,23 +204,34 @@
                   "at %s: %s\n") % (inst.args[1], inst.args[0]))
     return None
 
-def hook(ui, repo, node, hooktype, **kwargs):
-    """verify that files have expected EOLs"""
-    # Extract heads and get touched files set at the same time
+def _checkhook(ui, repo, node, headsonly):
+    # Get revisions to check and touched files at the same time
     files = set()
-    heads = set()
+    revs = set()
     for rev in xrange(repo[node].rev(), len(repo)):
         ctx = repo[rev]
         files.update(ctx.files())
-        heads.add(rev)
-        for pctx in ctx.parents():
-            heads.discard(pctx.rev())
-    for rev in heads:
+        revs.add(rev)
+        if headsonly:
+            for pctx in ctx.parents():
+                revs.discard(pctx.rev())
+    for rev in revs:
         ctx = repo[rev]
         eol = parseeol(ui, repo, [ctx.node()])
         if eol:
             eol.checkrev(repo, ctx, files)
 
+def checkallhook(ui, repo, node, hooktype, **kwargs):
+    """verify that files have expected EOLs"""
+    _checkhook(ui, repo, node, False)
+
+def checkheadshook(ui, repo, node, hooktype, **kwargs):
+    """verify that files have expected EOLs"""
+    _checkhook(ui, repo, node, True)
+
+# "checkheadshook" used to be called "hook"
+hook = checkheadshook
+
 def preupdate(ui, repo, hooktype, parent1, parent2):
     #print "preupdate for %s: %s -> %s" % (repo.root, parent1, parent2)
     repo.loadeol([parent1])
--- a/tests/test-eol-hook.t	Sun Mar 13 15:07:44 2011 +0100
+++ b/tests/test-eol-hook.t	Sun Mar 13 15:07:44 2011 +0100
@@ -116,3 +116,69 @@
   rollback completed
   abort: b.txt should not have CRLF line endings
   [255]
+
+Test checkheadshook alias
+
+  $ cat > ../main/.hg/hgrc <<EOF
+  > [hooks]
+  > pretxnchangegroup = python:hgext.eol.checkheadshook
+  > EOF
+  $ hg push -f ../main
+  pushing to ../main
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 2 changesets with 2 changes to 2 files (+1 heads)
+  error: pretxnchangegroup hook failed: b.txt should not have CRLF line endings
+  transaction abort!
+  rollback completed
+  abort: b.txt should not have CRLF line endings
+  [255]
+
+We can fix the head and push again
+
+  $ hg up 6
+  1 files updated, 0 files merged, 1 files removed, 0 files unresolved
+  $ printf "first\nsecond" > b.txt
+  $ hg ci -m "remove CRLF from b.txt"
+  $ hg push -f ../main
+  pushing to ../main
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 2 files (+1 heads)
+  $ hg -R ../main rollback
+  repository tip rolled back to revision 5 (undo push)
+  working directory now based on revision -1
+
+Test it still fails with checkallhook
+
+  $ cat > ../main/.hg/hgrc <<EOF
+  > [hooks]
+  > pretxnchangegroup = python:hgext.eol.checkallhook
+  > EOF
+  $ hg push -f ../main
+  pushing to ../main
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 3 changesets with 3 changes to 2 files (+1 heads)
+  error: pretxnchangegroup hook failed: b.txt should not have CRLF line endings
+  transaction abort!
+  rollback completed
+  abort: b.txt should not have CRLF line endings
+  [255]
+
+But we can push the clean head
+
+  $ hg push -r7 -f ../main
+  pushing to ../main
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files
+