diff app/models/repository/.svn/text-base/mercurial.rb.svn-base @ 245:051f544170fe

* Update to SVN trunk revision 4993
author Chris Cannam
date Thu, 03 Mar 2011 11:42:28 +0000
parents 8661b858af72
children eeebe205a056 cbce1fd3b1b7
line wrap: on
line diff
--- a/app/models/repository/.svn/text-base/mercurial.rb.svn-base	Thu Mar 03 11:40:10 2011 +0000
+++ b/app/models/repository/.svn/text-base/mercurial.rb.svn-base	Thu Mar 03 11:42:28 2011 +0000
@@ -24,7 +24,16 @@
   attr_protected :root_url
   validates_presence_of :url
 
-  def scm_adapter
+  FETCH_AT_ONCE = 100  # number of changesets to fetch at once
+
+  ATTRIBUTE_KEY_NAMES = {
+      "url"          => "Root directory",
+    }
+  def self.human_attribute_name(attribute_key_name)
+    ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
+  end
+
+  def self.scm_adapter_class
     Redmine::Scm::Adapters::MercurialAdapter
   end
 
@@ -32,6 +41,10 @@
     'Mercurial'
   end
 
+  def repo_log_encoding
+    'UTF-8'
+  end
+
   # Returns the readable identifier for the given mercurial changeset
   def self.format_changeset_identifier(changeset)
     "#{changeset.revision}:#{changeset.scmid}"
@@ -42,33 +55,18 @@
     changeset.scmid
   end
 
+  def branches
+    nil
+  end
+
+  def tags
+    nil
+  end
+
   def diff_format_revisions(cs, cs_to, sep=':')
     super(cs, cs_to, ' ')
   end
 
-  def entries(path=nil, identifier=nil)
-    entries=scm.entries(path, identifier)
-    if entries
-      entries.each do |entry|
-        next unless entry.is_file?
-        # Set the filesize unless browsing a specific revision
-        if identifier.nil?
-          full_path = File.join(root_url, entry.path)
-          entry.size = File.stat(full_path).size if File.file?(full_path)
-        end
-        # Search the DB for the entry's last change
-        change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
-        if change
-          entry.lastrev.identifier = change.changeset.revision
-          entry.lastrev.name = change.changeset.revision
-          entry.lastrev.author = change.changeset.committer
-          entry.lastrev.revision = change.revision
-        end
-      end
-    end
-    entries
-  end
-
   # Finds and returns a revision with a number or the beginning of a hash
   def find_changeset_by_name(name)
     return nil if name.nil? || name.empty?
@@ -82,50 +80,39 @@
   end
 
   # Returns the latest changesets for +path+; sorted by revision number
+  # Default behavior is to search in cached changesets
   def latest_changesets(path, rev, limit=10)
     if path.blank?
       changesets.find(:all, :include => :user, :limit => limit)
     else
-      changes.find(:all, :include => {:changeset => :user},
-                         :conditions => ["path = ?", path.with_leading_slash],
-                         :order => "#{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
 
   def fetch_changesets
-    scm_info = scm.info
-    if scm_info
-      # latest revision found in database
-      db_revision = latest_changeset ? latest_changeset.revision.to_i : -1
-      # latest revision in the repository
-      latest_revision = scm_info.lastrev
-      return if latest_revision.nil?
-      scm_revision = latest_revision.identifier.to_i
-      if db_revision < scm_revision
-        logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug?
-        identifier_from = db_revision + 1
-        while (identifier_from <= scm_revision)
-          # loads changesets by batches of 100
-          identifier_to = [identifier_from + 99, scm_revision].min
-          revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true)
-          transaction do
-            revisions.each do |revision|
-              changeset = Changeset.create(:repository => self,
-                                           :revision => revision.identifier,
-                                           :scmid => revision.scmid,
-                                           :committer => revision.author, 
-                                           :committed_on => revision.time,
-                                           :comments => revision.message)
-              
-              revision.paths.each do |change|
-                changeset.create_change(change)
-              end
-            end
-          end unless revisions.nil?
-          identifier_from = identifier_to + 1
+    scm_rev = scm.info.lastrev.revision.to_i
+    db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
+    return unless db_rev < scm_rev  # already up-to-date
+
+    logger.debug "Fetching changesets for repository #{url}" if logger
+    (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
+      transaction do
+        scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
+          cs = Changeset.create(:repository => self,
+                                :revision => re.revision,
+                                :scmid => re.scmid,
+                                :committer => re.author,
+                                :committed_on => re.time,
+                                :comments => re.message)
+          re.paths.each { |e| cs.create_change(e) }
         end
       end
     end
+    self
   end
 end