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