Index: extra/mercurial/hg-template-0.9.5.tmpl
===================================================================
--- extra/mercurial/hg-template-0.9.5.tmpl	(révision 0)
+++ extra/mercurial/hg-template-0.9.5.tmpl	(révision 0)
@@ -0,0 +1,12 @@
+changeset = 'This template must be used with --debug option\n'
+changeset_quiet =  'This template must be used with --debug option\n'
+changeset_verbose = 'This template must be used with --debug option\n'
+changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{files}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|strip|escape|addbreaks}</msg>\n{tags}</logentry>\n\n'
+
+file = '<path action="M">{file|escape}</path>\n'
+file_add = '<path action="A">{file_add|escape}</path>\n'
+file_del = '<path action="D">{file_del|escape}</path>\n'
+file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
+tag = '<tag>{tag|escape}</tag>\n'
+header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
+# footer="</log>"
Index: extra/mercurial/hg-template-1.0.tmpl
===================================================================
--- extra/mercurial/hg-template-1.0.tmpl	(révision 0)
+++ extra/mercurial/hg-template-1.0.tmpl	(révision 0)
@@ -0,0 +1,12 @@
+changeset = 'This template must be used with --debug option\n'
+changeset_quiet =  'This template must be used with --debug option\n'
+changeset_verbose = 'This template must be used with --debug option\n'
+changeset_debug = '<logentry revision="{rev}" node="{node|short}">\n<author>{author|escape}</author>\n<date>{date|isodate}</date>\n<paths>\n{file_mods}{file_adds}{file_dels}{file_copies}</paths>\n<msg>{desc|strip|escape|addbreaks}</msg>\n{tags}</logentry>\n\n'
+
+file_mod = '<path action="M">{file_mod|escape}</path>\n'
+file_add = '<path action="A">{file_add|escape}</path>\n'
+file_del = '<path action="D">{file_del|escape}</path>\n'
+file_copy = '<path-copied copyfrom-path="{source|escape}">{name|urlescape}</path-copied>\n'
+tag = '<tag>{tag|escape}</tag>\n'
+header='<?xml version="1.0" encoding="UTF-8" ?>\n<log>\n\n'
+# footer="</log>"
Index: lib/redmine/scm/adapters/abstract_adapter.rb
===================================================================
--- lib/redmine/scm/adapters/abstract_adapter.rb	(révision 1420)
+++ lib/redmine/scm/adapters/abstract_adapter.rb	(copie de travail)
@@ -94,7 +94,12 @@
           path ||= ''
           (path[0,1]!="/") ? "/#{path}" : path
         end
-        
+
+        def with_trailling_slash(path)
+          path ||= ''
+          (path[-1,1] == "/") ? path : "#{path}/"
+        end
+
         def shell_quote(str)
           if RUBY_PLATFORM =~ /mswin/
             '"' + str.gsub(/"/, '\\"') + '"'
Index: lib/redmine/scm/adapters/mercurial_adapter.rb
===================================================================
--- lib/redmine/scm/adapters/mercurial_adapter.rb	(révision 1420)
+++ lib/redmine/scm/adapters/mercurial_adapter.rb	(copie de travail)
@@ -24,6 +24,9 @@
       
         # Mercurial executable name
         HG_BIN = "hg"
+        TEMPLATES_DIR = "#{RAILS_ROOT}/extra/mercurial"
+        TEMPLATE_NAME = "hg-template"
+        TEMPLATE_EXTENSION = "tmpl"
         
         def info
           cmd = "#{HG_BIN} -R #{target('')} root"
@@ -40,28 +43,82 @@
           return nil
         end
         
-        def entries(path=nil, identifier=nil)
-          path ||= ''
+        def entries(path="", identifier=nil)
           entries = Entries.new
-          cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate"
-          cmd << " -r #{identifier.to_i}" if identifier
-          cmd << " " + shell_quote('glob:**')
+          cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
+          cmd << " -r " + (identifier ? identifier.to_s : "tip")
+          cmd << " " + shell_quote("path:#{path}") unless path.empty?
           shellout(cmd) do |io|
             io.each_line do |line|
-              e = line.chomp.split(%r{[\/\\]})
-              entries << Entry.new({:name => e.first,
-                                    :path => (path.empty? ? e.first : "#{path}/#{e.first}"),
-                                    :kind => (e.size > 1 ? 'dir' : 'file'),
-                                    :lastrev => Revision.new
-                                    }) unless entries.detect{|entry| entry.name == e.first}
+              # HG uses antislashs as separator on Windows
+              line = line.gsub(/\\/, "/")
+              if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
+                e ||= line
+                e = e.chomp.split(%r{[\/\\]})
+                entries << Entry.new({:name => e.first,
+                                       :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
+                                       :kind => (e.size > 1 ? 'dir' : 'file'),
+                                       :lastrev => Revision.new
+                                     }) unless entries.detect{|entry| entry.name == e.first}
+              end
             end
           end
           return nil if $? && $?.exitstatus != 0
           entries.sort_by_name
         end
           
+        
+        # Fetch the revisions by using a template file that make Mercurial producing a xml output.
         def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
           revisions = Revisions.new
+          cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
+          if identifier_from && identifier_to
+            cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
+          elsif identifier_from
+            cmd << " -r #{identifier_from.to_i}:"
+          end
+          cmd << " --limit #{options[:limit].to_i}" if options[:limit]
+          cmd << " #{path}" if path
+          shellout(cmd) do |io|
+            begin
+              # HG doesn't close the XML Document...
+              doc = REXML::Document.new(io.read << "</log>")
+              doc.elements.each("log/logentry") do |logentry|
+                paths = []
+                copies = logentry.get_elements('paths/path-copied')
+                logentry.elements.each("paths/path") do |path|
+                  # Detect if the added file is a copy
+                  if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
+                    from_path = c.attributes['copyfrom-path']
+                    from_rev = logentry.attributes['revision']
+                  end
+                  paths << {:action => path.attributes['action'],
+                    :path => "/#{path.text}",
+                    :from_path => from_path ? "/#{from_path}" : nil,
+                    :from_revision => from_rev ? from_rev : nil
+                  }
+                end
+                paths.sort! { |x,y| x[:path] <=> y[:path] }
+                
+                revisions << Revision.new({:identifier => logentry.attributes['revision'],
+                                            :scmid => logentry.attributes['node'],
+                                            :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
+                                            :time => Time.parse(logentry.elements['date'].text).localtime,
+                                            :message => logentry.elements['msg'].text,
+                                            :paths => paths
+                                          })
+              end
+            rescue
+              logger.debug($!)
+            end
+          end
+          return nil if $? && $?.exitstatus != 0
+          revisions
+        end
+        
+        
+        def revisions_old(path=nil, identifier_from=nil, identifier_to=nil, options={})
+          revisions = Revisions.new
           cmd = "#{HG_BIN} -v --encoding utf8 -R #{target('')} log"
           if identifier_from && identifier_to
             cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
@@ -125,7 +182,7 @@
         
         def cat(path, identifier=nil)
           cmd = "#{HG_BIN} -R #{target('')} cat"
-          cmd << " -r #{identifier.to_i}" if identifier
+          cmd << " -r " + (identifier ? identifier.to_s : "tip")
           cmd << " #{target(path)}"
           cat = nil
           shellout(cmd) do |io|
@@ -140,7 +197,7 @@
           path ||= ''
           cmd = "#{HG_BIN} -R #{target('')}"
           cmd << " annotate -n -u"
-          cmd << " -r #{identifier.to_i}" if identifier
+          cmd << " -r " + (identifier ? identifier.to_s : "tip")
           cmd << " #{target(path)}"
           blame = Annotate.new
           shellout(cmd) do |io|
@@ -161,8 +218,8 @@
           
           # Changes
           paths = (rev_id == 0) ?
-            # Can't get changes for revision 0 with hg status
-            changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
+          # Can't get changes for revision 0 with hg status
+          changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
             status(rev_id)
           
           Revision.new({:identifier => rev_id,
@@ -193,6 +250,20 @@
           end
           result
         end
+
+        def self.hgversion
+          @@hgversion ||= %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1].split(".").collect(&:to_i)
+        end
+
+        def self.template_path
+          if (self.hgversion <=> [0,9,5]) > 0
+            ver = "1.0"
+          else
+            ver = "0.9.5"
+          end
+          @@template ||= "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
+        end
+        
       end
     end
   end
