changeset 226:04a90432e9e7

Implemented a "--import" subcommand for the "timestamps" command. Import an existing .hgtimestamp database file from the TimestampExtension and store it into a fresh or amended timestamps database file.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Mon, 22 Oct 2018 14:13:35 +0200
parents 13fa7c91a496
children 097d08017d78
files extensions/timestamps.py
diffstat 1 files changed, 50 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/timestamps.py	Mon Oct 22 13:08:10 2018 +0200
+++ b/extensions/timestamps.py	Mon Oct 22 14:13:35 2018 +0200
@@ -119,12 +119,14 @@
 @command(
     "timestamps",
     [
+        ("", "import", None, _("import timestamps from the TimestampExtension format")),
         ("", "save", None, _("save modification times")),
         ("", "restore", None, _("restore modification times")),
         ("", "show", None, _("show the contents of the timestamps database")),
         ("r", "rev", "", _("the revision to use (default is the working space)"), _("REV")),
         ("", "tsconfig", "", _("use an alternate configuration file"), _("TSCONFIG")),
-        ("", "amend", None, _("amend the current database file instead of generating a fresh one")),
+        ("", "amend", None, _("amend an existing database file instead of generating a fresh one")),
+        ("", "all", None, _("when importing import all timestamps instead of only the matching ones")),
     ],
     _("hg timestamps [OPTION]..."))
 def timestamps(ui, repo, **opts):
@@ -138,12 +140,23 @@
     Giving --show prints the timestamps database -- possibly at a given
     revision ``--rev REV``.
 
+    Giving --import import an existing ".hgtimestamp" database from the
+    TimestampExtension and stores the content in the timestamps database
+    format. Use --all, --tsconfig and --amend for some behaviour refinements.
+
     """
     opts = pycompat.byteskwargs(opts)
     ctx = scmutil.revsingle(repo, opts.get("rev"), default=None)
     if not ctx:
         raise error.Abort(_("no Mercurial working directory"))
-    if opts.get("save"):
+    if opts.get("import"):
+        import_timestamps(ui,
+                          repo,
+                          ctx,
+                          tsconfig=opts.get("tsconfig"),
+                          amend=opts.get("amend"),
+                          all=opts.get("all"))
+    elif opts.get("save"):
         save_timestamps(ui,
                         repo,
                         ctx,
@@ -156,7 +169,7 @@
         show_timestamps(ui, repo, ctx, fm, tsconfig=opts.get("tsconfig"))
     else:
         raise error.Abort(_("must give a command:"
-                            "--save or --restore or --show"))
+                            "--save or --restore, --show or --import"))
 
 
 def save_timestamps(ui,
@@ -185,6 +198,40 @@
         ts.write(fp)
 
 
+def import_timestamps(ui,
+                      repo,
+                      ctx,
+                      tsconfig=None,
+                      amend=False,
+                      all=False):
+    if not repo.local():
+        raise error.Abort(_("repository is not local"))
+    matcher = gen_matcher(repo, ctx, tsconfig=tsconfig)
+    if matcher is None:
+        raise error.Abort(_("timestamps are not activated/configured"))
+    if amend:
+        ts = Timestamps.from_filename(ui, name=repo.wjoin(TIMESTAMPS_DATABASE))
+        if ts is None:
+            raise error.Abort(_("timestamps database file does not exist"
+                                " -- cannot amend"))
+    else:
+        ts = Timestamps.create(ui)
+    try:
+        sourcetsfile = io.open(repo.wjoin(".hgtimestamp"), "rt",
+                               encoding="utf-8")
+    except IOError:
+        raise error.Abort(_("no .hgtimestamp database file"))
+    try:
+        for line in sourcetsfile:
+            fname, mtime = line.strip().split(',')
+            if all or matcher(fname):
+                ts[fname] = to_isoformat(float(mtime))
+    finally:
+        sourcetsfile.close()
+    with io.open(repo.wjoin(TIMESTAMPS_DATABASE), "wb") as fp:
+        ts.write(fp)
+
+
 def restore_timestamps(ui, repo, ctx,
                        tsconfig=None):
     if not repo.local():