##// END OF EJS Templates
scm: mercurial: use regexp %r{} instead of // in model latest_changesets_cond()....
Toshi MARUYAMA -
r5810:adbe6af44a16
parent child
Show More
@@ -1,146 +1,146
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'redmine/scm/adapters/mercurial_adapter'
18 require 'redmine/scm/adapters/mercurial_adapter'
19
19
20 class Repository::Mercurial < Repository
20 class Repository::Mercurial < Repository
21 # sort changesets by revision number
21 # sort changesets by revision number
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 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
27 FETCH_AT_ONCE = 100 # number of changesets to fetch at once
28
28
29 def self.human_attribute_name(attribute_key_name)
29 def self.human_attribute_name(attribute_key_name)
30 attr_name = attribute_key_name
30 attr_name = attribute_key_name
31 if attr_name == "url"
31 if attr_name == "url"
32 attr_name = "path_to_repository"
32 attr_name = "path_to_repository"
33 end
33 end
34 super(attr_name)
34 super(attr_name)
35 end
35 end
36
36
37 def self.scm_adapter_class
37 def self.scm_adapter_class
38 Redmine::Scm::Adapters::MercurialAdapter
38 Redmine::Scm::Adapters::MercurialAdapter
39 end
39 end
40
40
41 def self.scm_name
41 def self.scm_name
42 'Mercurial'
42 'Mercurial'
43 end
43 end
44
44
45 def supports_directory_revisions?
45 def supports_directory_revisions?
46 true
46 true
47 end
47 end
48
48
49 def repo_log_encoding
49 def repo_log_encoding
50 'UTF-8'
50 'UTF-8'
51 end
51 end
52
52
53 # Returns the readable identifier for the given mercurial changeset
53 # Returns the readable identifier for the given mercurial changeset
54 def self.format_changeset_identifier(changeset)
54 def self.format_changeset_identifier(changeset)
55 "#{changeset.revision}:#{changeset.scmid}"
55 "#{changeset.revision}:#{changeset.scmid}"
56 end
56 end
57
57
58 # Returns the identifier for the given Mercurial changeset
58 # Returns the identifier for the given Mercurial changeset
59 def self.changeset_identifier(changeset)
59 def self.changeset_identifier(changeset)
60 changeset.scmid
60 changeset.scmid
61 end
61 end
62
62
63 def diff_format_revisions(cs, cs_to, sep=':')
63 def diff_format_revisions(cs, cs_to, sep=':')
64 super(cs, cs_to, ' ')
64 super(cs, cs_to, ' ')
65 end
65 end
66
66
67 # Finds and returns a revision with a number or the beginning of a hash
67 # Finds and returns a revision with a number or the beginning of a hash
68 def find_changeset_by_name(name)
68 def find_changeset_by_name(name)
69 return nil if name.nil? || name.empty?
69 return nil if name.nil? || name.empty?
70 if /[^\d]/ =~ name or name.to_s.size > 8
70 if /[^\d]/ =~ name or name.to_s.size > 8
71 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
71 e = changesets.find(:first, :conditions => ['scmid = ?', name.to_s])
72 else
72 else
73 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
73 e = changesets.find(:first, :conditions => ['revision = ?', name.to_s])
74 end
74 end
75 return e if e
75 return e if e
76 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
76 changesets.find(:first, :conditions => ['scmid LIKE ?', "#{name}%"]) # last ditch
77 end
77 end
78
78
79 # Returns the latest changesets for +path+; sorted by revision number
79 # Returns the latest changesets for +path+; sorted by revision number
80 #
80 #
81 # Because :order => 'id DESC' is defined at 'has_many',
81 # Because :order => 'id DESC' is defined at 'has_many',
82 # there is no need to set 'order'.
82 # there is no need to set 'order'.
83 # But, MySQL test fails.
83 # But, MySQL test fails.
84 # Sqlite3 and PostgreSQL pass.
84 # Sqlite3 and PostgreSQL pass.
85 # Is this MySQL bug?
85 # Is this MySQL bug?
86 def latest_changesets(path, rev, limit=10)
86 def latest_changesets(path, rev, limit=10)
87 changesets.find(:all, :include => :user,
87 changesets.find(:all, :include => :user,
88 :conditions => latest_changesets_cond(path, rev, limit),
88 :conditions => latest_changesets_cond(path, rev, limit),
89 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
89 :limit => limit, :order => "#{Changeset.table_name}.id DESC")
90 end
90 end
91
91
92 def latest_changesets_cond(path, rev, limit)
92 def latest_changesets_cond(path, rev, limit)
93 cond, args = [], []
93 cond, args = [], []
94 if scm.branchmap.member? rev
94 if scm.branchmap.member? rev
95 # Mercurial named branch is *stable* in each revision.
95 # Mercurial named branch is *stable* in each revision.
96 # So, named branch can be stored in database.
96 # So, named branch can be stored in database.
97 # Mercurial provides *bookmark* which is equivalent with git branch.
97 # Mercurial provides *bookmark* which is equivalent with git branch.
98 # But, bookmark is not implemented.
98 # But, bookmark is not implemented.
99 cond << "#{Changeset.table_name}.scmid IN (?)"
99 cond << "#{Changeset.table_name}.scmid IN (?)"
100 # Revisions in root directory and sub directory are not equal.
100 # Revisions in root directory and sub directory are not equal.
101 # So, in order to get correct limit, we need to get all revisions.
101 # So, in order to get correct limit, we need to get all revisions.
102 # But, it is very heavy.
102 # But, it is very heavy.
103 # Mercurial does not treat direcotry.
103 # Mercurial does not treat direcotry.
104 # So, "hg log DIR" is very heavy.
104 # So, "hg log DIR" is very heavy.
105 branch_limit = path.blank? ? limit : ( limit * 5 )
105 branch_limit = path.blank? ? limit : ( limit * 5 )
106 args << scm.nodes_in_branch(rev, :limit => branch_limit)
106 args << scm.nodes_in_branch(rev, :limit => branch_limit)
107 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
107 elsif last = rev ? find_changeset_by_name(scm.tagmap[rev] || rev) : nil
108 cond << "#{Changeset.table_name}.id <= ?"
108 cond << "#{Changeset.table_name}.id <= ?"
109 args << last.id
109 args << last.id
110 end
110 end
111
111
112 unless path.blank?
112 unless path.blank?
113 cond << "EXISTS (SELECT * FROM #{Change.table_name}
113 cond << "EXISTS (SELECT * FROM #{Change.table_name}
114 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
114 WHERE #{Change.table_name}.changeset_id = #{Changeset.table_name}.id
115 AND (#{Change.table_name}.path = ?
115 AND (#{Change.table_name}.path = ?
116 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
116 OR #{Change.table_name}.path LIKE ? ESCAPE ?))"
117 args << path.with_leading_slash
117 args << path.with_leading_slash
118 args << "#{path.with_leading_slash.gsub(/[%_\\]/) { |s| "\\#{s}" }}/%" << '\\'
118 args << "#{path.with_leading_slash.gsub(%r{[%_\\]}) { |s| "\\#{s}" }}/%" << '\\'
119 end
119 end
120
120
121 [cond.join(' AND '), *args] unless cond.empty?
121 [cond.join(' AND '), *args] unless cond.empty?
122 end
122 end
123 private :latest_changesets_cond
123 private :latest_changesets_cond
124
124
125 def fetch_changesets
125 def fetch_changesets
126 scm_rev = scm.info.lastrev.revision.to_i
126 scm_rev = scm.info.lastrev.revision.to_i
127 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
127 db_rev = latest_changeset ? latest_changeset.revision.to_i : -1
128 return unless db_rev < scm_rev # already up-to-date
128 return unless db_rev < scm_rev # already up-to-date
129
129
130 logger.debug "Fetching changesets for repository #{url}" if logger
130 logger.debug "Fetching changesets for repository #{url}" if logger
131 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
131 (db_rev + 1).step(scm_rev, FETCH_AT_ONCE) do |i|
132 transaction do
132 transaction do
133 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
133 scm.each_revision('', i, [i + FETCH_AT_ONCE - 1, scm_rev].min) do |re|
134 cs = Changeset.create(:repository => self,
134 cs = Changeset.create(:repository => self,
135 :revision => re.revision,
135 :revision => re.revision,
136 :scmid => re.scmid,
136 :scmid => re.scmid,
137 :committer => re.author,
137 :committer => re.author,
138 :committed_on => re.time,
138 :committed_on => re.time,
139 :comments => re.message)
139 :comments => re.message)
140 re.paths.each { |e| cs.create_change(e) }
140 re.paths.each { |e| cs.create_change(e) }
141 end
141 end
142 end
142 end
143 end
143 end
144 self
144 self
145 end
145 end
146 end
146 end
General Comments 0
You need to be logged in to leave comments. Login now