Commits

Anonymous committed 176689f

change diff in order of sending intelligent message when we use hg mv

Comments (0)

Files changed (2)

hgview/gtk/hgview_gtk.py

         textwidget = self.xml.get_widget( "textview_status" )
         text_buffer = textwidget.get_buffer()
         tag_table = text_buffer.get_tag_table()
-
+        print 'tag_table',tag_table, tag_table.__dict__
+        
         tag_table.add( make_texttag( "mono", family="Monospace" ))
         tag_table.add( make_texttag( "blue", foreground='blue' ))
         tag_table.add( make_texttag( "red", foreground='red' ))
         buf.insert( eob, "\n" )
 
 
-    def prepare_diff( self, difflines, offset ):
-        idx = 0
-        outlines = []
-        tags = []
-        filespos = []
-        def addtag( name, offset, length ):
-            if tags and tags[-1][0] == name and tags[-1][2]==offset:
-                tags[-1][2] += length
-            else:
-                tags.append( [name, offset, offset+length] )
-        #print "DIFF:", len(difflines)
-        stats = [0,0]
-        statmax = 0
-        for i,l in enumerate(difflines):
-            if l.startswith("diff"):
-                f = l.split()[-1]
-                txt = DIFFHDR % f
-                addtag( "greybg", offset, len(txt) )
-                outlines.append(txt)
-                markname = "file%d" % idx
-                idx += 1
-                statmax = max( statmax, stats[0]+stats[1] )
-                stats = [0,0]
-                filespos.append(( f, markname, offset, stats ))
-                offset += len(txt)
-                continue
-            elif l.startswith("+++"):
-                continue
-            elif l.startswith("---"):
-                continue
-            elif l.startswith("+"):
-                tag = "green"
-                stats[0] += 1
-            elif l.startswith("-"):
-                stats[1] += 1
-                tag = "red"
-            elif l.startswith("@@"):
-                tag = "blue"
-            else:
-                tag = "black"
-            l = l+"\n"
-            length = len(l)
-            addtag( tag, offset, length )
-            outlines.append( l )
-            offset += length
+    def prepare_diff( self, difflines, offset ,changes):
+        if changes[2] and  changes[1]: #remove file
+            file_remove = changes[2][0]
+            file_added  = changes[1][0]
+            print '////',file_remove , file_added
+            idx = 0
+            outlines = []
+            tags = []
+            filespos = []
+            def addtag( name, offset, length ):
+                if tags and tags[-1][0] == name and tags[-1][2]==offset:
+                    tags[-1][2] += length
+                else:
+                    tags.append( [name, offset, offset+length] )
+                #print "DIFF:", len(difflines)
+            stats = [0,0]
+            statmax = 0
+            DIFFHDR = "rename from === %s ===\nrename to === %s ===\n"
+            txt = DIFFHDR % (file_remove, file_added)
+            addtag( "greybg", offset, len(txt) )
+            outlines.append(txt)
+            markname = "file%d" % idx
+            idx += 1
+            statmax = max( statmax, stats[0]+stats[1] )
+            stats = [0,0]
+            filespos.append(( file_added , markname, offset, stats ))
+            filespos.append(( file_remove , markname, offset, stats ))
+            offset += len(txt) 
+        else:
+            idx = 0
+            outlines = []
+            tags = []
+            filespos = []
+            def addtag( name, offset, length ):
+                if tags and tags[-1][0] == name and tags[-1][2]==offset:
+                    tags[-1][2] += length
+                else:
+                    tags.append( [name, offset, offset+length] )
+                #print "DIFF:", len(difflines)
+            stats = [0,0]
+            statmax = 0
+            for i,l in enumerate(difflines):
+                if l.startswith("diff"):
+                    f = l.split()[-1]
+                    DIFFHDR = "=== %s ===\n"
+                    txt = DIFFHDR % f
+                    addtag( "greybg", offset, len(txt) )
+                    outlines.append(txt)
+                    markname = "file%d" % idx
+                    idx += 1
+                    statmax = max( statmax, stats[0]+stats[1] )
+                    stats = [0,0]
+                    filespos.append(( f, markname, offset, stats ))
+                    offset += len(txt)
+                    continue
+                elif l.startswith("+++"):
+                    continue
+                elif l.startswith("---"):
+                    continue
+                elif l.startswith("+"):
+                    tag = "green"
+                    stats[0] += 1
+                elif l.startswith("-"):
+                    stats[1] += 1
+                    tag = "red"
+                elif l.startswith("@@"):
+                    tag = "blue"
+                else:
+                    tag = "black"
+                l = l+"\n"
+                length = len(l)
+                addtag( tag, offset, length )
+                outlines.append( l )
+                offset += length
         statmax = max( statmax, stats[0]+stats[1] )
         return filespos, tags, outlines, statmax
 
     def selection_changed( self, selection ):
+        #import pdb
+        #pdb.set_trace()
         model, it = selection.get_selected()
         if it is None:
             return
-        node, rnode = model.get( it, M_ID, M_NODE )
+        node, rnode = model.get(
+            it, M_ID, M_NODE )
         textwidget = self.xml.get_widget( "textview_status" )
         text_buffer = textwidget.get_buffer()
         textwidget.freeze_child_notify()
             enddesc.backward_line()
             text_buffer.create_mark( "enddesc", enddesc )
             self.filelist.append( ("Content", "begdesc", None ) )
-            buff = self.repo.diff( self.repo.parents(node), node, rnode.files )
+            buff, changes = self.repo.diff(self.repo.parents(node), node, rnode.files)
+            print '*********changes', changes
             try:
                 buff = unicode( buff, "utf-8" )
             except UnicodeError:
             eob = text_buffer.get_end_iter()
             
             offset = eob.get_offset()
-            fileoffsets, tags, lines, statmax = self.prepare_diff( difflines, offset )
+            fileoffsets, tags, lines, statmax = self.prepare_diff( difflines, offset, changes )
             txt = u"".join(lines)
 
             # XXX debug : sometime gtk complains it's not valid utf-8 !!!
 import os
-from mercurial import hg, ui, patch
-from mercurial.node import short as short_hex, bin as short_bin
+from mercurial import hg, ui, patch, cmdutil
+from mercurial.node import hex, short as short_hex, bin as short_bin
 from mercurial.localrepo import localrepository
 from mercurial.node import nullid
 from buildtree import RevGraph
 import textwrap
 import time
 import mercurial.commands
+import re
 
 
 class RevNode(object):
     def children( self, node ):
         return [ n for n in self.repo.changelog.children( node ) if n!=nullid ]
     
-    def diff( self, parents, node2, files ):
+    def diff(self, parents, node2, files, match=True):
+        changes = self.repo.status(parents[0], node2, files)[:4]            
+        modified, added, removed, deleted = changes
+        # find renamed files
+
+        files = list(files[:])
+        print "files=", files
+        print "added files=", added
+        print "node2 = ", self.read_node(node2).rev
+        print "parents = ", [self.read_node(p).rev for p in parents]
+
+        diffmsg = ""
+        ctx = self.repo.changectx(node2)
+        for f in added:
+            print "check if %s has been renamed"%f
+##             for src, abs, rel, exact in cmdutil.walk(self.repo, (f,), {},
+##                                                      ctx.node()):
+            fctx = ctx.filectx(f)
+            m = fctx.filelog().renamed(fctx.filenode())
+            if m:
+                diffmsg += "%s renamed from %s:%s\n" % (f, m[0], hex(m[1]))
+                files.remove(f)
+                files.remove(m[0])
+        print "diffmsg = ", diffmsg
+        print "files=",  files
         if len(parents)==1:
-            return self.single_diff( parents[0], node2, files )
+            print self.single_diff(parents[0], node2, files)
+            return diffmsg + self.single_diff(parents[0], node2, files), changes
         else:
-#            return self.merge_diff( parents, node2, files )
-            return self.single_diff( parents[0], node2, files )
+#           return self.merge_diff( parents, node2, files )
+            return diffmsg + self.single_diff(parents[0], node2, files), changes
 
-    def single_diff( self, node1, node2, files ):
+    def single_diff( self, node1, node2, files):
         out = StringIO()
         patch.diff( self.repo, node1=node1,
                     node2=node2, files=files, fp=out )
         mercurial.commands.tag( self.ui, self.repo, label,
                                 rev=1, message="hop",
                                 local=True, user=None, date=None )
+
+
+if __name__ == "__main__":
+    import sys
+    rep = HgHLRepo(sys.argv[1])
+