How to write tags

Frank A. Kingswood frank at kingswood-consulting.co.uk
Thu Dec 24 20:37:44 CST 2009


Steve Borho wrote:
> Slightly off-topic, but you may want to store the perforce tags in a
> localtags file to avoid this memctx issue, and to avoid the future
> issue of knowing not to push changes to the .hgtags file back to
> perforce.
>   
I've just implemented the writes to .hgtags, that was not too bad. Most 
difficult was finding out where to get the file data - the API spec does 
not really cover much. The convert extension does not do this yet either.
> Since p4 tags are not allowed to move (AFAIK), they don't need to be
> versioned like native tags.
>   
No, p4 tags can move, and often do. There even appears to be something 
to make them move automagically (p4 help labelsync???)

Frank

# HG changeset patch
# User Frank Kingswood <frank at kingswood-consulting.co.uk>
# Date 1261708108 0
# Node ID 3ef91c14b4fba9c8c026525bba063da6caab5d86
# Parent  74b50742f9a1326aaa922ad0f55d1927bb300404
[mq]: p4-tags

diff -r 74b50742f9a1 -r 3ef91c14b4fb perfarce.py
--- a/perfarce.py    Thu Dec 24 10:08:20 2009 +0000
+++ b/perfarce.py    Fri Dec 25 02:28:28 2009 +0000
@@ -317,6 +317,18 @@
         return mode, contents
 
 
+    def labels(self, change):
+        'Return p4 labels a.k.a. tags at the given changelist'
+
+        tags = []
+        for d in self.run('labels @%d,%d' % (change, change)):
+            l = d.get('label')
+            if l:
+                tags.append(l)
+
+        return tags
+
+
     @staticmethod
     def pullcommon(original, ui, repo, source, **opts):
         'Shared code for pull and incoming'
@@ -486,10 +498,12 @@
     client, p4rev, p4id, changes = r
     for c in changes:
         desc, user, date, files = client.describe(c, files=ui.verbose)
+        tags = client.labels(c)
 
         ui.write(_('changelist:  %d\n') % c)
         # ui.write(_('branch:      %s\n') % branch)
-        # ui.write(_('tag:         %s\n') % tag)
+        for tag in tags:
+            ui.write(_('tag:         %s\n') % tag)
         # ui.write(_('parent:      %d:%s\n') % parent)
         ui.write(_('user:        %s\n') % user)
         ui.write(_('date:        %s\n') % util.datestr(date))
@@ -524,6 +538,7 @@
         mode, contents = client.getfile(revcache[fn], c)
         return context.memfilectx(fn, contents, 'l' in mode, 'x' in 
mode, None)
 
+    tags = []
     for c in changes:
         desc, user, date, files = client.describe(c, files=True)
 
@@ -545,8 +560,34 @@
 
         p4rev = repo.commitctx(ctx)
         ctx = repo[p4rev]
+
+        labels = client.labels(c)
+        if labels:
+            tags.append((c, ctx.hex(), labels))
+
         ui.note(_('added changeset %d:%s\n') % (ctx.rev(), ctx))
 
+    if tags:
+        if '.hgtags' in ctx:
+            tagdata = ctx.filectx('.hgtags').data()
+        else:
+            tagdata = ''
+
+        desc = ['p4 tags']
+        for t in tags:
+            for l in t[2]:
+                desc.append('   %s @ %d' % (l, t[0]))
+                tagdata += '%s %s\n' % (t[1], l)
+
+        def getfilectx(repo, memctx, fn):
+            'callback to read file data'
+            assert fn=='.hgtags'
+            return context.memfilectx(fn, tagdata, False, False, None)
+
+        ctx = context.memctx(repo, (p4rev, None), '\n'.join(desc),
+                             ['.hgtags'], getfilectx)
+        repo.commitctx(ctx)
+
     client.close()
 
     if opts['update']:
diff -r 74b50742f9a1 -r 3ef91c14b4fb test-push-perforce
--- a/test-push-perforce    Thu Dec 24 10:08:20 2009 +0000
+++ b/test-push-perforce    Fri Dec 25 02:28:28 2009 +0000
@@ -2,11 +2,39 @@
 
 set -e
 
+HELP=0
+DEBUG=0
+SHELL=0
+P4ROOT=$PWD/depot
+
+while getopts "dr:s" OPT ; do
+   case "$OPT" in
+      d)
+         DEBUG=1
+         ;;
+      r)
+         P4ROOT=$OPTARG
+         ;;
+      s)
+         SHELL=1
+         ;;
+      \?)
+     HELP=1
+     break
+         ;;
+   esac
+done
+
+shift $(($OPTIND-1))
+
+if [ $HELP -ne 0 ] ; then
+   echo "Usage: $0 [-ds] [-r DIR]"
+   exit 1
+fi
+
 echo % create p4 depot
 
-DEBUG=0
-
-P4ROOT=${P4ROOT:-$PWD/depot}; export P4ROOT
+export P4ROOT
 P4AUDIT=$P4ROOT/audit; export P4AUDIT
 P4JOURNAL=$P4ROOT/journal; export P4JOURNAL
 P4LOG=$P4ROOT/log; export P4LOG
@@ -62,6 +90,7 @@
 data > b/c
 p4 add a b/c
 p4 submit -d initial
+p4 tag -l change-one ...
 
 echo % change some files
 p4 edit a
@@ -75,6 +104,7 @@
 data >>b/d
 p4 add b/d
 p4 submit -d "p4 add b/d"
+p4 tag -l change-four ...
 
 p4 delete b/c
 p4 submit -d "p4 delete b/c"
@@ -143,6 +173,8 @@
 p4 changes       | filter
 p4 describe -s 7 | filter
 
+p4 tag -l change-seven ...
+
 cd ../dst
 
 echo % incoming
@@ -163,5 +195,7 @@
 echo % p4stat
 head .hg/p4stat
 
-#echo % run bash
-#bash
+if [ $SHELL -ne 0 ] ; then
+   echo % run bash
+   bash
+fi

-- 
------------------------------------------------------------------------
Frank A. Kingswood                      frank at kingswood-consulting.co.uk
Cambridge, United Kingdom                               +44-7545-209 100



More information about the Mercurial-devel mailing list