comparison app/models/repository/mercurial.rb @ 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
comparison
equal deleted inserted replaced
244:8972b600f4fb 245:051f544170fe
22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id' 22 has_many :changesets, :order => "#{Changeset.table_name}.id DESC", :foreign_key => 'repository_id'
23 23
24 attr_protected :root_url 24 attr_protected :root_url
25 validates_presence_of :url 25 validates_presence_of :url
26 26
27 def scm_adapter 27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
28
29 ATTRIBUTE_KEY_NAMES = {
30 "url" => "Root directory",
31 }
32 def self.human_attribute_name(attribute_key_name)
33 ATTRIBUTE_KEY_NAMES[attribute_key_name] || super
34 end
35
36 def self.scm_adapter_class
28 Redmine::Scm::Adapters::MercurialAdapter 37 Redmine::Scm::Adapters::MercurialAdapter
29 end 38 end
30 39
31 def self.scm_name 40 def self.scm_name
32 'Mercurial' 41 'Mercurial'
42 end
43
44 def repo_log_encoding
45 'UTF-8'
33 end 46 end
34 47
35 # Returns the readable identifier for the given mercurial changeset 48 # Returns the readable identifier for the given mercurial changeset
36 def self.format_changeset_identifier(changeset) 49 def self.format_changeset_identifier(changeset)
37 "#{changeset.revision}:#{changeset.scmid}" 50 "#{changeset.revision}:#{changeset.scmid}"
40 # Returns the identifier for the given Mercurial changeset 53 # Returns the identifier for the given Mercurial changeset
41 def self.changeset_identifier(changeset) 54 def self.changeset_identifier(changeset)
42 changeset.scmid 55 changeset.scmid
43 end 56 end
44 57
58 def branches
59 nil
60 end
61
62 def tags
63 nil
64 end
65
45 def diff_format_revisions(cs, cs_to, sep=':') 66 def diff_format_revisions(cs, cs_to, sep=':')
46 super(cs, cs_to, ' ') 67 super(cs, cs_to, ' ')
47 end
48
49 def entries(path=nil, identifier=nil)
50 entries=scm.entries(path, identifier)
51 if entries
52 entries.each do |entry|
53 next unless entry.is_file?
54 # Set the filesize unless browsing a specific revision
55 if identifier.nil?
56 full_path = File.join(root_url, entry.path)
57 entry.size = File.stat(full_path).size if File.file?(full_path)
58 end
59 # Search the DB for the entry's last change
60 change = changes.find(:first, :conditions => ["path = ?", scm.with_leading_slash(entry.path)], :order => "#{Changeset.table_name}.committed_on DESC")
61 if change
62 entry.lastrev.identifier = change.changeset.revision
63 entry.lastrev.name = change.changeset.revision
64 entry.lastrev.author = change.changeset.committer
65 entry.lastrev.revision = change.revision
66 end
67 end
68 end
69 entries
70 end 68 end
71 69
72 # Finds and returns a revision with a number or the beginning of a hash 70 # Finds and returns a revision with a number or the beginning of a hash
73 def find_changeset_by_name(name) 71 def find_changeset_by_name(name)
74 return nil if name.nil? || name.empty? 72 return nil if name.nil? || name.empty?
80 return e if e 78 return e if e
81 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch 79 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
82 end 80 end
83 81
84 # Returns the latest changesets for +path+; sorted by revision number 82 # Returns the latest changesets for +path+; sorted by revision number
83 # Default behavior is to search in cached changesets
85 def latest_changesets(path, rev, limit=10) 84 def latest_changesets(path, rev, limit=10)
86 if path.blank? 85 if path.blank?
87 changesets.find(:all, :include => :user, :limit => limit) 86 changesets.find(:all, :include => :user, :limit => limit)
88 else 87 else
89 changes.find(:all, :include => {:changeset => :user}, 88 changesets.find(:all, :select => "DISTINCT #{Changeset.table_name}.*",
90 :conditions => ["path = ?", path.with_leading_slash], 89 :joins => :changes,
91 :order => "#{Changeset.table_name}.id DESC", 90 :conditions => ["#{Change.table_name}.path = ? OR #{Change.table_name}.path LIKE ? ESCAPE ?",
92 :limit => limit).collect(&:changeset) 91 path.with_leading_slash,
92 "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%", '\\'],
93 :include => :user, :limit => limit)
93 end 94 end
94 end 95 end
95 96
96 def fetch_changesets 97 def fetch_changesets
97 scm_info = scm.info 98 scm_rev = scm.info.lastrev.revision.to_i
98 if scm_info 99 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
99 # latest revision found in database 100 return unless db_rev < scm_rev # already up-to-date
100 db_revision = latest_changeset ? latest_changeset.revision.to_i : -1 101
101 # latest revision in the repository 102 logger.debug "Fetching changesets for repository #{url}" if logger
102 latest_revision = scm_info.lastrev 103 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
103 return if latest_revision.nil? 104 transaction do
104 scm_revision = latest_revision.identifier.to_i 105 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
105 if db_revision < scm_revision 106 cs = Changeset.create(:repository => self,
106 logger.debug "Fetching changesets for repository #{url}" if logger && logger.debug? 107 :revision => re.revision,
107 identifier_from = db_revision + 1 108 :scmid => re.scmid,
108 while (identifier_from <= scm_revision) 109 :committer => re.author,
109 # loads changesets by batches of 100 110 :committed_on => re.time,
110 identifier_to = [identifier_from + 99, scm_revision].min 111 :comments => re.message)
111 revisions = scm.revisions('', identifier_from, identifier_to, :with_paths => true) 112 re.paths.each { |e| cs.create_change(e) }
112 transaction do
113 revisions.each do |revision|
114 changeset = Changeset.create(:repository => self,
115 :revision => revision.identifier,
116 :scmid => revision.scmid,
117 :committer => revision.author,
118 :committed_on => revision.time,
119 :comments => revision.message)
120
121 revision.paths.each do |change|
122 changeset.create_change(change)
123 end
124 end
125 end unless revisions.nil?
126 identifier_from = identifier_to + 1
127 end 113 end
128 end 114 end
129 end 115 end
116 self
130 end 117 end
131 end 118 end