Index: /CMT/HEAD/ChangeLog
===================================================================
--- /CMT/HEAD/ChangeLog	(revision 511)
+++ /CMT/HEAD/ChangeLog	(revision 512)
@@ -1,2 +1,7 @@
+2009-06-18    <rybkin@lal.in2p3.fr> 401
+	
+	* mgr/cmt_svn_checkout.py: Introduce function trunk_tag to determine
+	the tag of the module's trunk
+	
 2009-05-23    <rybkin@lal.in2p3.fr> 400
 	
Index: /CMT/HEAD/mgr/cmt_svn_checkout.py
===================================================================
--- /CMT/HEAD/mgr/cmt_svn_checkout.py	(revision 511)
+++ /CMT/HEAD/mgr/cmt_svn_checkout.py	(revision 512)
@@ -23,6 +23,6 @@
 """
 
-__version__ = '0.1.5'
-__date__ = 'Tue May 05 2009'
+__version__ = '0.2.0'
+__date__ = 'Thu Jun 18 2009'
 __author__ = 'Grigory Rybkin'
 
@@ -33,4 +33,5 @@
 import os.path
 import urlparse
+import tempfile
 
 self = 'cmt_svn_checkout.py'
@@ -210,4 +211,69 @@
         self.modules.append(module)
 
+    def info_receiver(self, path, info, pool):
+        self.path = path
+        self.info = info
+
+    def cmp(self, path1, path2, client_context):
+        outfile = tempfile.TemporaryFile('wb')
+        errfile = tempfile.TemporaryFile('wb')
+        outb = outfile.tell()
+        errb = errfile.tell()
+        client.diff([]
+                    , path1
+                    , self.head_revision
+                    , path2
+                    , self.head_revision
+                    , True, True, False
+                    , outfile
+                    , errfile
+                    , client_context())
+
+        # position at the end of the file
+        outfile.seek(0, 2)
+        errfile.seek(0, 2)
+        oute = outfile.tell()
+        erre = errfile.tell()
+
+        if erre > errb: return 2
+        elif oute > outb: return 1
+        else: return 0
+
+    def trunk_tag(self, module, client_context):
+        """Attempt to determine the tag of the module's trunk.
+        
+        Return the tag if its contents are the same as those of the trunk, and
+        its last_changed_rev is greater than the trunk created_rev, None otherwise.
+        """
+        try:
+            trunk = core.svn_path_canonicalize(posixpath.join(module.url, self.trunk))
+            client.info(trunk,
+                        self.head_revision,
+                        self.head_revision,
+                        self.info_receiver,
+                        False,
+                        client_context())
+            
+            tags = core.svn_path_canonicalize(posixpath.join(module.url, self.tags))
+            tags_dirent = client.ls(tags,
+                                    self.head_revision,
+                                    False,
+                                    client_context())
+            
+            rev_tag = dict([(tags_dirent[p].created_rev, p) for p in tags_dirent if tags_dirent[p].kind == core.svn_node_dir])
+            revs = rev_tag.keys()
+            revs.sort()
+            revs.reverse()
+
+            for rev in revs:
+                if rev < self.info.last_changed_rev: break
+                tag = core.svn_path_canonicalize(posixpath.join(tags, rev_tag[rev]))
+                if 0 == self.cmp(trunk, tag, client_context):
+                    return rev_tag[rev]
+
+            return None
+        except core.SubversionException, e:
+            return None
+
     def initialize(self, cmt_context, client_context):
         sc = 0
@@ -238,23 +304,5 @@
             if self.version is None:
                 m.head = True
-                tags = core.svn_path_canonicalize(posixpath.join(m.url, self.tags))
-                try:
-                    #print 'client.ls2:', tags
-                    ls_tags = client.ls2(tags,
-                                         self.head_revision,
-                                         self.head_revision,
-                                         False,
-                                         client_context())
-                    rev_tag = dict([(ls_tags[p].created_rev, p) for p in ls_tags if ls_tags[p].kind == core.svn_node_dir])
-#                rev_latest = max(rev_tag.keys())
-#                tag_latest = rev_tag[rev_latest]
-                    m.version = rev_tag[max(rev_tag.keys())]
-                except core.SubversionException, e:
-                    error(e)
-                    #print >> sys.stderr, e
-                    m.version = 'HEAD'
-                except ValueError, e: # max() arg is an empty sequence
-                    #print >> sys.stderr, e
-                    m.version = 'HEAD'
+                m.version = self.trunk_tag(m, client_context) or 'HEAD'
             else:
                 m.head = False
