Index: tracdarcs/changesparser.py
===================================================================
--- tracdarcs/changesparser.py	(revision 176)
+++ tracdarcs/changesparser.py	(revision 181)
@@ -136,2 +136,38 @@
             log.warning('-'*(e._colnum-1)+'^'*5)
     return handler.patches
+
+# Aid for the repository posthook: since Trac 0.12 the preferred way
+# to trigger a syncronization is "trac-admin $ENV changeset added ..."
+#
+# With darcs, this can be achieved by putting something like
+#
+#  apply posthook trac-admin /path/to/trac/env changeset added $(pwd) $(python -m tracdarcs.changesparser)
+#  apply run-posthook
+#
+# into the repository's `_darcs/prefs/defaults` configuration.
+
+if __name__ == '__main__':
+    from logging import basicConfig, root as log
+    from os import environ
+    import sys
+
+    basicConfig()
+
+    # If invoked by a darcs posthook, the text to parse comes
+    # from the $DARCS_PATCHES_XML envvar. Otherwise slurp it
+    # from the stdin.
+
+    if 'DARCS_PATCHES_XML' in environ:
+        changes = environ['DARCS_PATCHES_XML']
+    else:
+        changes = sys.stdin.read()
+
+    if len(sys.argv)>1:
+        output = open(sys.argv[1], 'w')
+    else:
+        output = sys.stdout
+
+    for patch in parse_changes(changes, log):
+        print >>output, patch.hash
+
+    output.close()
