Commits

Anonymous committed aa2a750

- Change name to ban-changesets and add a better implementation for finding the descendant
revisions.

  • Participants
  • Parent commits 911cdf2
  • Tags release1.1

Comments (0)

Files changed (2)

File ban-changesets.py

+#!/usr/bin/env python
+
+'''ban-changesets is a Mercurial extension which sets up a pretxncommit hook.
+Any changesets to be pushed / pulled / bundled into the repository will have their
+changeset hashes compared against a list of banned hashes in repo/.hgbannedchangesets.
+If any new hash is already in this file of banned hashes then the entire
+changegroup will be rejected.
+
+Enable the ban-changesets just like any other Mercurial extension by adding the
+following to your hgrc
+
+[extensions]
+ban-changesets = /path/to/ban-changesets
+'''
+
+import os.path, re
+from mercurial import hg
+
+#print "Checking For Banned Changesets!"
+
+def getSetOfBannedChangesets(repo):
+    try:
+        bannedChangesetsPath = os.path.join(repo.root,'.hgbannedchangesets')
+        f = open(bannedChangesetsPath, 'r')
+        banned = set()
+        changesetPat = re.compile(r"(^[0-9a-fA-F]+).*")
+        for line in f:
+            m = re.match(changesetPat, line)
+            if m:
+                banned.add(m.group(1))
+        f.close()
+        return banned
+    except:
+        return {}
+
+def changesetIsBanned(node, bannedChangesets):
+    for p in bannedChangesets:
+        if re.search(p, node):
+            return True
+    return False
+
+def checkForBannedChangesets(ui, repo, **kwargs):
+    node = kwargs.get('node')
+    if node:
+        bannedChangesets = getSetOfBannedChangesets(repo)
+        startRev = int(repo[node])
+        descendantRevs = list(repo.changelog.descendants(startRev))
+        descendantRevs.append(startRev)
+
+        rejectedChangesets = set()
+        for rev in descendantRevs:
+            changeset = repo[rev].hex()
+            if changesetIsBanned(changeset, bannedChangesets):
+                rejectedChangesets.add(changeset)
+
+        if len(rejectedChangesets) > 0:
+            repoName = os.path.basename(repo.root)
+            ui.warn('The ban-changeset extension rejected the %s opertion on the repository \'%s\' due to the changeset(s):\n' % (kwargs.get('source'), repoName))
+            for changeset in rejectedChangesets:
+                ui.warn('    %s\n' % changeset)
+            if (len(rejectedChangesets) < len(descendantRevs)):
+                if len(rejectedChangesets) == 1:
+                    ui.warn('Rebase, transplant, or otherwise move any valid changesets in the source repository which are derived from this rejected changeset. Strip the banned changesets, and then redo the operation.\n')
+                else:
+                    ui.warn('Rebase, transplant, or otherwise move any valid changesets in the source repository which are derived from these rejected changesets. Strip the banned changesets, and then redo the operation.\n')
+            return True # We found a banned changeset, return True (exit code 1) which causes the changegroup addition to be aborted.
+    return False # No banned changesets were found. The changegroup addition can go ahead.
+
+def reposetup(ui, repo):
+    ui.setconfig("hooks", "pretxnchangegroup.ban-changesets", checkForBannedChangesets)

File exclude-changesets.py

-#!/usr/bin/env python
-
-'''exclude_changesets is a Mercurial extension which sets up a pretxncommit hook.
-Any changesets to be pushed / pulled / bundled into the repository will have their
-changeset hashes compared against a list of excluded hashes in repo/.excludedchangesets.
-If any new hash is already in this file of excluded hashes then the entire
-changegroup will be rejected.
-
-Enable the exclude_changesets just like any other Mercurial extension by adding the
-following to your hgrc
-
-[extensions]
-exclude_changesets = /path/to/exclude_changesets
-'''
-
-import os.path, re
-from mercurial import hg
-
-#print "Checking For Excluded Changesets!"
-
-def getSetOfExcludedChangesets(repo):
-    try:
-        excludedChangesetsPath = os.path.join(repo.root,'.excludedchangesets')
-        f = open(excludedChangesetsPath, 'r')
-        excluded = set()
-        changesetPat = re.compile(r"(^[0-9a-fA-F]+).*")
-        for line in f:
-            m = re.match(changesetPat, line)
-            if m:
-                excluded.add(m.group(1))
-        f.close()
-        return excluded
-    except:
-        return {}
-
-def changesetIsExcluded(node, excludedChangesets):
-    for p in excludedChangesets:
-        if re.search(p, node):
-            return True
-    return False
-
-def colateChildren(ctx, colation = set()):
-    testSet = set()
-    testSet.add(ctx)
-    while len(testSet) > 0:
-        revCtx = testSet.pop()
-        revCtxHex = revCtx.hex()
-        if not revCtxHex in colation:
-            colation.add(revCtxHex)
-            testSet.update(revCtx.children())            
-    return colation
-
-def checkForExcludedChangesets(ui, repo, **kwargs):
-    node = kwargs.get('node')
-    rejectedChangesets = set()
-    if node:
-        excludedChangesets = getSetOfExcludedChangesets(repo)
-        ctx = repo[node]
-        allChildren = colateChildren(ctx)
-        for childChangeset in allChildren:
-            if changesetIsExcluded(childChangeset, excludedChangesets):
-                rejectedChangesets.add(childChangeset)
-        if len(rejectedChangesets) > 0:
-            repoName = os.path.basename(repo.root)
-            if len(rejectedChangesets) == 1:
-                ui.warn('The following changeset was rejected by the repository \'%s\'\n' % repoName)
-            else:
-                ui.warn('The following %d changesets were rejected by the repository \'%s\'\n' % (len(rejectedChangesets), repoName))
-            for changeset in rejectedChangesets:
-                ui.warn('    %s\n' % changeset)
-            if (len(rejectedChangesets) < len(allChildren)):
-                if len(rejectedChangesets) == 1:
-                    ui.warn('Rebase, transplant, or otherwise transfer any changesets you have which are derived from this rejected changeset.\n')
-                else:
-                    ui.warn('Rebase, transplant, or otherwise transfer any changesets you have which are derived from these rejected changesets.\n')
-            return True # We found an excluded changeset, return True (exit code 1) which causes the changegroup addition to be aborted.
-    return False # No excluded changesets were found. The changegroup addition can go ahead.
-
-def reposetup(ui, repo):
-    ui.setconfig("hooks", "pretxnchangegroup.exclude_changesets", checkForExcludedChangesets)