changeset 219:22244ee82c55

Moved the core parts of hook implementations into custom methods of the repo
author Franz Glasner <hg@dom66.de>
date Sat, 20 Oct 2018 12:20:19 +0200
parents 069be0a64afc
children 710e9af4b283
files extensions/timestamps.py
diffstat 1 files changed, 50 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/timestamps.py	Thu Oct 18 09:39:14 2018 +0200
+++ b/extensions/timestamps.py	Sat Oct 20 12:20:19 2018 +0200
@@ -86,6 +86,8 @@
 
 TIMESTAMP_FORMAT = re.compile(r"^(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})T(?P<hour>[0-9]{2}):(?P<minute>[0-9]{2}):(?P<second>[0-9]{2})(\.(?P<ms>[0-9]+))?Z$")
 
+_DEV = True
+
 
 def getversion():
     """Provide the version information for verbose :hg:`version` output.
@@ -680,31 +682,32 @@
                 yield i
 
 
+def pre_hook(ui, repo, hooktype, **kwds):
+    """Generic pre-xxx hook: just forwards to the corresponding repo method"""
+    repo._ts_record_pre_data(hooktype)
+
+
 def update_hook(ui, repo, hooktype, **kwds):
-    ui.debug("UPDATE: " + repr(kwds) + '\n')
-    p1 = kwds.get("parent1")
-    p2 = kwds.get("parent2")
-    error = kwds.get("error")
-    if not repo.local():
-        ui.debug("update.timestamps: repo is not local\n")
-        return
-    wctx = repo[None]
-    tsmatch = gen_matcher(repo, wctx)
-    if tsmatch is not None:
-        ts = Timestamps.from_ctx(wctx, ui, name=TIMESTAMPS_DATABASE)
-        if ts is None:
-            ui.warn(_("update.timestamps: timestamps database file could not be found or read\n"))
-        else:
-            # XXX TBD
-            pass
-    else:
-        ui.debug("update.timestamps: timestamps not activated/configured\n")
+    if _DEV:
+        ui.debug("UPDATE: " + repr(repo) + '\n')
+        ui.debug("UPDATE: " + repr(kwds) + '\n')
+    #
+    # Note: We get only the target revisions. We cannot just compute the
+    #       difference
+    repo._ts_update(parent1=kwds.get("parent1"),
+                    parent2=kwds.get("parent2"),
+                    error=kwds.get("error"))
 
 
 def reposetup(ui, repo):
 
     class TimestampedRepo(repo.__class__):
 
+        def __init__(self, *args, **kwds):
+            super(TimestampedRepo, self).__init__(*args, **kwds)
+            self.__hooktype = None
+            self.__from_parents = None
+
         def commit(self, text="", user=None, date=None, match=None,
                    force=False, editor=False, extra=None, **kwds):
             wctx = self[None]
@@ -800,6 +803,34 @@
                                                        editor=editor,
                                                        extra=extra,
                                                        **kwds)
+        def _ts_record_pre_data(self, hooktype):
+            if not self.local():
+                return
+            self.__hooktype = hooktype
+            self.__from_parents = [p.hex() for p in self[None].parents()]
+            self.ui.debug("recorded previous parents: "
+                          + repr(self.__from_parents) + '\n')
+
+        def _ts_update(self, parent1=None, parent2=None, error=0):
+            if not self.local():
+                self.ui.debug("update.timestamps: repo is not local\n")
+                return
+            if not self.__hooktype:
+                self.ui.warn("timestamps: no recorded hook data\n")
+                return
+            wctx = self[None]
+            tsmatch = gen_matcher(self, wctx)
+            if tsmatch is not None:
+                ts = Timestamps.from_ctx(wctx,
+                                         self.ui, name=TIMESTAMPS_DATABASE)
+                if ts is None:
+                    self.ui.warn(_("update.timestamps: timestamps database file could not be found or read\n"))
+                else:
+                    # XXX TBD
+                    pass
+            else:
+                self.ui.debug("update.timestamps: timestamps not activated/configured\n")
+
 
     repo.__class__ = TimestampedRepo
 
@@ -807,4 +838,4 @@
 
 
 def uisetup(ui):
-    pass
+    ui.setconfig('hooks', 'pre-update.timestamps', pre_hook)