# Enrique García:  refs #6159
# Based on the work of Yuya Nishihara <yuya@tcha.org>
# Replaced a/app and b/app by app

diff --git app/models/repository.rb app/models/repository.rb
--- app/models/repository.rb
+++ app/models/repository.rb
@@ -105,14 +105,14 @@ class Repository < ActiveRecord::Base
   # Default behaviour is to search in cached changesets
   def latest_changesets(path, rev, limit=10)
     if path.blank?
-      changesets.find(:all, :include => :user,
-                            :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
-                            :limit => limit)
+      changesets.find(:all, :include => :user, :limit => limit)
     else
-      changes.find(:all, :include => {:changeset => :user}, 
-                         :conditions => ["path = ?", path.with_leading_slash],
-                         :order => "#{Changeset.table_name}.committed_on DESC, #{Changeset.table_name}.id DESC",
-                         :limit => limit).collect(&:changeset)
+      changesets.find(:all, :select => "DISTINCT #{Changeset.table_name}.*",
+                      :joins => :changes,
+                      :conditions => ["#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ? ESCAPE ?",
+                                      path.with_leading_slash,
+                                      "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%", '\\'],
+                      :include => :user, :limit => limit)
     end
   end
     
diff --git app/models/repository/subversion.rb app/models/repository/subversion.rb
--- app/models/repository/subversion.rb
+++ app/models/repository/subversion.rb
@@ -31,8 +31,8 @@ class Repository::Subversion < Repositor
   end
 
   def latest_changesets(path, rev, limit=10)
-    revisions = scm.revisions(path, rev, nil, :limit => limit)
-    revisions ? changesets.find_all_by_revision(revisions.collect(&:identifier), :order => "committed_on DESC", :include => :user) : []
+    path = "#{relative_url}/#{path}".gsub(%r|/+|, '/') unless path.blank?
+    super(path, rev, limit)
   end
   
   # Returns a path relative to the url of the repository
