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