@@ -1,255 +1,253 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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/abstract_adapter' |
|
19 | 19 | require 'cgi' |
|
20 | 20 | |
|
21 | 21 | module Redmine |
|
22 | 22 | module Scm |
|
23 | 23 | module Adapters |
|
24 | 24 | class MercurialAdapter < AbstractAdapter |
|
25 | 25 | |
|
26 | 26 | # Mercurial executable name |
|
27 | 27 | HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg" |
|
28 | 28 | HELPERS_DIR = File.dirname(__FILE__) + "/mercurial" |
|
29 | 29 | HG_HELPER_EXT = "#{HELPERS_DIR}/redminehelper.py" |
|
30 | 30 | TEMPLATE_NAME = "hg-template" |
|
31 | 31 | TEMPLATE_EXTENSION = "tmpl" |
|
32 | 32 | |
|
33 | 33 | # raised if hg command exited with error, e.g. unknown revision. |
|
34 | 34 | class HgCommandAborted < CommandFailed; end |
|
35 | 35 | |
|
36 | 36 | class << self |
|
37 | 37 | def client_command |
|
38 | 38 | @@bin ||= HG_BIN |
|
39 | 39 | end |
|
40 | 40 | |
|
41 | 41 | def sq_bin |
|
42 | 42 | @@sq_bin ||= shell_quote(HG_BIN) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def client_version |
|
46 | 46 | @@client_version ||= (hgversion || []) |
|
47 | 47 | end |
|
48 | 48 | |
|
49 | 49 | def client_available |
|
50 | 50 | !client_version.empty? |
|
51 | 51 | end |
|
52 | 52 | |
|
53 | 53 | def hgversion |
|
54 | 54 | # The hg version is expressed either as a |
|
55 | 55 | # release number (eg 0.9.5 or 1.0) or as a revision |
|
56 | 56 | # id composed of 12 hexa characters. |
|
57 | 57 | theversion = hgversion_from_command_line |
|
58 | 58 | if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)}) |
|
59 | 59 | m[2].scan(%r{\d+}).collect(&:to_i) |
|
60 | 60 | end |
|
61 | 61 | end |
|
62 | 62 | |
|
63 | 63 | def hgversion_from_command_line |
|
64 | 64 | shellout("#{sq_bin} --version") { |io| io.read }.to_s |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def template_path |
|
68 | 68 | @@template_path ||= template_path_for(client_version) |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def template_path_for(version) |
|
72 | 72 | if ((version <=> [0,9,5]) > 0) || version.empty? |
|
73 | 73 | ver = "1.0" |
|
74 | 74 | else |
|
75 | 75 | ver = "0.9.5" |
|
76 | 76 | end |
|
77 | 77 | "#{HELPERS_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}" |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def info |
|
82 | 82 | tip = summary['repository']['tip'] |
|
83 | 83 | Info.new(:root_url => CGI.unescape(summary['repository']['root']), |
|
84 | 84 | :lastrev => Revision.new(:revision => tip['revision'], |
|
85 | 85 | :scmid => tip['node'])) |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | def summary |
|
89 | 89 | @summary ||= hg 'rhsummary' do |io| |
|
90 | 90 | ActiveSupport::XmlMini.parse(io.read)['rhsummary'] |
|
91 | 91 | end |
|
92 | 92 | end |
|
93 | 93 | private :summary |
|
94 | 94 | |
|
95 | 95 | def entries(path=nil, identifier=nil) |
|
96 | 96 | path ||= '' |
|
97 | 97 | entries = Entries.new |
|
98 | 98 | cmd = "#{self.class.sq_bin} -R #{target('')} --cwd #{target('')} locate" |
|
99 | 99 | cmd << " -r #{hgrev(identifier, true)}" |
|
100 | 100 | cmd << " " + shell_quote("path:#{path}") unless path.empty? |
|
101 | 101 | shellout(cmd) do |io| |
|
102 | 102 | io.each_line do |line| |
|
103 | 103 | # HG uses antislashs as separator on Windows |
|
104 | 104 | line = line.gsub(/\\/, "/") |
|
105 | 105 | if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'') |
|
106 | 106 | e ||= line |
|
107 | 107 | e = e.chomp.split(%r{[\/\\]}) |
|
108 | 108 | entries << Entry.new({:name => e.first, |
|
109 | 109 | :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"), |
|
110 | 110 | :kind => (e.size > 1 ? 'dir' : 'file'), |
|
111 | 111 | :lastrev => Revision.new |
|
112 | 112 | }) unless e.empty? || entries.detect{|entry| entry.name == e.first} |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | end |
|
116 | 116 | return nil if $? && $?.exitstatus != 0 |
|
117 | 117 | entries.sort_by_name |
|
118 | 118 | end |
|
119 | 119 | |
|
120 | # Fetch the revisions by using a template file that | |
|
120 | def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
|
121 | revs = Revisions.new | |
|
122 | each_revision(path, identifier_from, identifier_to, options) { |e| revs << e } | |
|
123 | revs | |
|
124 | end | |
|
125 | ||
|
126 | # Iterates the revisions by using a template file that | |
|
121 | 127 | # makes Mercurial produce a xml output. |
|
122 |
def revision |
|
|
123 | revisions = Revisions.new | |
|
124 | cmd = "#{self.class.sq_bin} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}" | |
|
125 | if identifier_from && identifier_to | |
|
126 | cmd << " -r #{hgrev(identifier_from, true)}:#{hgrev(identifier_to, true)}" | |
|
127 | elsif identifier_from | |
|
128 | cmd << " -r #{hgrev(identifier_from, true)}:" | |
|
129 | end | |
|
130 | cmd << " --limit #{options[:limit].to_i}" if options[:limit] | |
|
131 | cmd << " #{shell_quote path}" unless path.blank? | |
|
132 | shellout(cmd) do |io| | |
|
133 | begin | |
|
134 | # HG doesn't close the XML Document... | |
|
135 | doc = REXML::Document.new(io.read << "</log>") | |
|
136 | doc.elements.each("log/logentry") do |logentry| | |
|
137 | paths = [] | |
|
138 | copies = logentry.get_elements('paths/path-copied') | |
|
139 | logentry.elements.each("paths/path") do |path| | |
|
140 | # Detect if the added file is a copy | |
|
141 | if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text } | |
|
142 | from_path = c.attributes['copyfrom-path'] | |
|
143 | from_rev = logentry.attributes['revision'] | |
|
144 | end | |
|
145 | paths << {:action => path.attributes['action'], | |
|
146 | :path => "/#{CGI.unescape(path.text)}", | |
|
147 | :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil, | |
|
148 | :from_revision => from_rev ? from_rev : nil | |
|
149 | } | |
|
150 | end | |
|
151 | paths.sort! { |x,y| x[:path] <=> y[:path] } | |
|
152 | ||
|
153 | revisions << Revision.new({:revision => logentry.attributes['revision'], | |
|
154 | :scmid => logentry.attributes['node'], | |
|
155 | :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""), | |
|
156 | :time => Time.parse(logentry.elements['date'].text).localtime, | |
|
157 | :message => logentry.elements['msg'].text, | |
|
158 | :paths => paths, | |
|
159 | }) | |
|
160 | end | |
|
161 | rescue | |
|
162 | logger.debug($!) | |
|
128 | def each_revision(path=nil, identifier_from=nil, identifier_to=nil, options={}) | |
|
129 | hg_args = ['log', '--debug', '-C', '--style', self.class.template_path] | |
|
130 | hg_args << '-r' << "#{hgrev(identifier_from)}:#{hgrev(identifier_to)}" | |
|
131 | hg_args << '--limit' << options[:limit] if options[:limit] | |
|
132 | hg_args << hgtarget(path) unless path.blank? | |
|
133 | log = hg(*hg_args) do |io| | |
|
134 | # Mercurial < 1.5 does not support footer template for '</log>' | |
|
135 | ActiveSupport::XmlMini.parse("#{io.read}</log>")['log'] | |
|
136 | end | |
|
137 | ||
|
138 | as_ary(log['logentry']).each do |le| | |
|
139 | cpalist = as_ary(le['paths']['path-copied']).map do |e| | |
|
140 | [e['__content__'], e['copyfrom-path']].map { |s| CGI.unescape(s) } | |
|
163 | 141 | end |
|
164 | end | |
|
165 | return nil if $? && $?.exitstatus != 0 | |
|
166 | revisions | |
|
142 | cpmap = Hash[*cpalist.flatten] | |
|
143 | ||
|
144 | paths = as_ary(le['paths']['path']).map do |e| | |
|
145 | p = CGI.unescape(e['__content__']) | |
|
146 | {:action => e['action'], :path => with_leading_slash(p), | |
|
147 | :from_path => (cpmap.member?(p) ? with_leading_slash(cpmap[p]) : nil), | |
|
148 | :from_revision => (cpmap.member?(p) ? le['revision'] : nil)} | |
|
149 | end.sort { |a, b| a[:path] <=> b[:path] } | |
|
150 | ||
|
151 | yield Revision.new(:revision => le['revision'], | |
|
152 | :scmid => le['node'], | |
|
153 | :author => (le['author']['__content__'] rescue ''), | |
|
154 | :time => Time.parse(le['date']['__content__']).localtime, | |
|
155 | :message => le['msg']['__content__'], | |
|
156 | :paths => paths) | |
|
157 | end | |
|
158 | self | |
|
167 | 159 | end |
|
168 | 160 | |
|
169 | 161 | def diff(path, identifier_from, identifier_to=nil) |
|
170 | 162 | path ||= '' |
|
171 | 163 | diff_args = '' |
|
172 | 164 | diff = [] |
|
173 | 165 | if identifier_to |
|
174 | 166 | diff_args = "-r #{hgrev(identifier_to, true)} -r #{hgrev(identifier_from, true)}" |
|
175 | 167 | else |
|
176 | 168 | if self.class.client_version_above?([1, 2]) |
|
177 | 169 | diff_args = "-c #{hgrev(identifier_from, true)}" |
|
178 | 170 | else |
|
179 | 171 | return [] |
|
180 | 172 | end |
|
181 | 173 | end |
|
182 | 174 | cmd = "#{self.class.sq_bin} -R #{target('')} --config diff.git=false diff --nodates #{diff_args}" |
|
183 | 175 | cmd << " -I #{target(path)}" unless path.empty? |
|
184 | 176 | shellout(cmd) do |io| |
|
185 | 177 | io.each_line do |line| |
|
186 | 178 | diff << line |
|
187 | 179 | end |
|
188 | 180 | end |
|
189 | 181 | return nil if $? && $?.exitstatus != 0 |
|
190 | 182 | diff |
|
191 | 183 | end |
|
192 | 184 | |
|
193 | 185 | def cat(path, identifier=nil) |
|
194 | 186 | hg 'cat', '-r', hgrev(identifier), hgtarget(path) do |io| |
|
195 | 187 | io.binmode |
|
196 | 188 | io.read |
|
197 | 189 | end |
|
198 | 190 | rescue HgCommandAborted |
|
199 | 191 | nil # means not found |
|
200 | 192 | end |
|
201 | 193 | |
|
202 | 194 | def annotate(path, identifier=nil) |
|
203 | 195 | blame = Annotate.new |
|
204 | 196 | hg 'annotate', '-ncu', '-r', hgrev(identifier), hgtarget(path) do |io| |
|
205 | 197 | io.each_line do |line| |
|
206 | 198 | next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$} |
|
207 | 199 | r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3, |
|
208 | 200 | :identifier => $3) |
|
209 | 201 | blame.add_line($4.rstrip, r) |
|
210 | 202 | end |
|
211 | 203 | end |
|
212 | 204 | blame |
|
213 | 205 | rescue HgCommandAborted |
|
214 | 206 | nil # means not found or cannot be annotated |
|
215 | 207 | end |
|
216 | 208 | |
|
217 | 209 | class Revision < Redmine::Scm::Adapters::Revision |
|
218 | 210 | # Returns the readable identifier |
|
219 | 211 | def format_identifier |
|
220 | 212 | "#{revision}:#{scmid}" |
|
221 | 213 | end |
|
222 | 214 | end |
|
223 | 215 | |
|
224 | 216 | # Runs 'hg' command with the given args |
|
225 | 217 | def hg(*args, &block) |
|
226 | 218 | repo_path = root_url || url |
|
227 | 219 | full_args = [HG_BIN, '-R', repo_path, '--encoding', 'utf-8'] |
|
228 | 220 | full_args << '--config' << "extensions.redminehelper=#{HG_HELPER_EXT}" |
|
229 | 221 | full_args << '--config' << 'diff.git=false' |
|
230 | 222 | full_args += args |
|
231 | 223 | ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block) |
|
232 | 224 | if $? && $?.exitstatus != 0 |
|
233 | 225 | raise HgCommandAborted, "hg exited with non-zero status: #{$?.exitstatus}" |
|
234 | 226 | end |
|
235 | 227 | ret |
|
236 | 228 | end |
|
237 | 229 | private :hg |
|
238 | 230 | |
|
239 | 231 | # Returns correct revision identifier |
|
240 | 232 | def hgrev(identifier, sq=false) |
|
241 | 233 | rev = identifier.blank? ? 'tip' : identifier.to_s |
|
242 | 234 | rev = shell_quote(rev) if sq |
|
243 | 235 | rev |
|
244 | 236 | end |
|
245 | 237 | private :hgrev |
|
246 | 238 | |
|
247 | 239 | def hgtarget(path) |
|
248 | 240 | path ||= '' |
|
249 | 241 | root_url + '/' + without_leading_slash(path) |
|
250 | 242 | end |
|
251 | 243 | private :hgtarget |
|
244 | ||
|
245 | def as_ary(o) | |
|
246 | return [] unless o | |
|
247 | o.is_a?(Array) ? o : Array[o] | |
|
248 | end | |
|
249 | private :as_ary | |
|
252 | 250 | end |
|
253 | 251 | end |
|
254 | 252 | end |
|
255 | 253 | end |
General Comments 0
You need to be logged in to leave comments.
Login now