[PATCH] contrib: example hook to reject subrepos

Gregory Szorc gregory.szorc at gmail.com
Fri Nov 3 20:55:12 EDT 2017


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1509756700 25200
#      Fri Nov 03 17:51:40 2017 -0700
# Branch stable
# Node ID eeca6102193a20fd85b2ad93fd6ae3f7ddff2a67
# Parent  f2390c369bfebf32f26f5a2e4aa5620224a7c8ea
contrib: example hook to reject subrepos

This is a proof-of-concept pretxnchangegroup hook that demonstrates
how to reject changesets containing files that define subrepos.

(Please do not queue this patch.)

diff --git a/contrib/prevent-subrepos.py b/contrib/prevent-subrepos.py
new file mode 100644
--- /dev/null
+++ b/contrib/prevent-subrepos.py
@@ -0,0 +1,25 @@
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+from __future__ import absolute_import
+
+# This hook (meant to be installed as a pretxnchangegroup hook)
+# will reject changesets containing either a .hgsub or .hgsubtate
+# file. These files define subrepos. Preventing the existence of
+# these files effectively disables the subrepo feature.
+#
+# This hook can be installed by adding the following in your hgrc:
+#
+# [hooks]
+# pretxnchangegroup.prevent_subrepos = python:/path/to/prevent-subrepos.py:hook
+
+def hook(ui, repo, node, hooktype, **kwargs):
+    for rev in repo.changelog.revs(repo[node].rev()):
+        ctx = repo[rev]
+
+        if '.hgsub' not in ctx and '.hgsubstate' not in ctx:
+            return 0
+
+        ui.write('error: subrepos are not allowed in this repository\n')
+        ui.write('remove the .hgsub and/or .hgsubstate file\n')
+        return 1
diff --git a/tests/test-subrepo-hook.t b/tests/test-subrepo-hook.t
new file mode 100644
--- /dev/null
+++ b/tests/test-subrepo-hook.t
@@ -0,0 +1,44 @@
+  $ hg init hgsub
+  $ cd hgsub
+  $ echo sub0 > foo
+  $ hg -q commit -A -m 'initial sub'
+  $ cd ..
+
+  $ hg init server
+  $ cat > server/.hg/hgrc << EOF
+  > [hooks]
+  > pretxnchangegroup.subrepo = python:$TESTDIR/../contrib/prevent-subrepos.py:hook
+  > EOF
+  $ hg -q clone --pull server client
+  $ cd client
+
+  $ echo 0 > foo
+  $ hg -q commit -A -m initial
+  $ hg -q push
+
+Add a subrepo
+
+  $ cat > .hgsub << EOF
+  > hgsub = ../hgsub
+  > EOF
+
+  $ hg add .hgsub
+  $ hg commit -m 'add hgsub'
+
+Hook rejects push
+
+  $ hg push
+  pushing to $TESTTMP/server
+  pushing subrepo hgsub to $TESTTMP/hgsub
+  no changes found
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 2 changes to 2 files
+  error: subrepos are not allowed in this repository
+  remove the .hgsub and/or .hgsubstate file
+  transaction abort!
+  rollback completed
+  abort: pretxnchangegroup.subrepo hook failed
+  [255]


More information about the Mercurial-devel mailing list