21 |
21 |
module Scm
|
22 |
22 |
module Adapters
|
23 |
23 |
class MercurialAdapter < AbstractAdapter
|
24 |
|
|
|
24 |
|
25 |
25 |
# Mercurial executable name
|
26 |
26 |
HG_BIN = "hg"
|
|
27 |
TEMPLATES_DIR = "#{RAILS_ROOT}/extra/mercurial"
|
|
28 |
TEMPLATE_NAME = "hg-template"
|
|
29 |
TEMPLATE_EXTENSION = "tmpl"
|
27 |
30 |
|
28 |
31 |
def info
|
29 |
32 |
cmd = "#{HG_BIN} -R #{target('')} root"
|
... | ... | |
33 |
36 |
end
|
34 |
37 |
return nil if $? && $?.exitstatus != 0
|
35 |
38 |
info = Info.new({:root_url => root_url.chomp,
|
36 |
|
:lastrev => revisions(nil,nil,nil,{:limit => 1}).last
|
37 |
|
})
|
|
39 |
:lastrev => revisions(nil,nil,nil,{:limit => 1}).last
|
|
40 |
})
|
38 |
41 |
info
|
39 |
42 |
rescue CommandFailed
|
40 |
43 |
return nil
|
... | ... | |
43 |
46 |
def entries(path=nil, identifier=nil)
|
44 |
47 |
path ||= ''
|
45 |
48 |
entries = Entries.new
|
46 |
|
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target(path)} locate"
|
47 |
|
cmd << " -r #{identifier.to_i}" if identifier
|
48 |
|
cmd << " " + shell_quote('glob:**')
|
|
49 |
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
|
|
50 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
|
51 |
cmd << " " + shell_quote("path:#{path}") unless path.empty?
|
49 |
52 |
shellout(cmd) do |io|
|
50 |
53 |
io.each_line do |line|
|
51 |
|
e = line.chomp.split(%r{[\/\\]})
|
52 |
|
entries << Entry.new({:name => e.first,
|
53 |
|
:path => (path.empty? ? e.first : "#{path}/#{e.first}"),
|
54 |
|
:kind => (e.size > 1 ? 'dir' : 'file'),
|
55 |
|
:lastrev => Revision.new
|
56 |
|
}) unless entries.detect{|entry| entry.name == e.first}
|
|
54 |
# HG uses antislashs as separator on Windows
|
|
55 |
line = line.gsub(/\\/, "/")
|
|
56 |
if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
|
|
57 |
e ||= line
|
|
58 |
e = e.chomp.split(%r{[\/\\]})
|
|
59 |
entries << Entry.new({:name => e.first,
|
|
60 |
:path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
|
|
61 |
:kind => (e.size > 1 ? 'dir' : 'file'),
|
|
62 |
:lastrev => Revision.new
|
|
63 |
}) unless entries.detect{|entry| entry.name == e.first}
|
|
64 |
end
|
57 |
65 |
end
|
58 |
66 |
end
|
59 |
67 |
return nil if $? && $?.exitstatus != 0
|
60 |
68 |
entries.sort_by_name
|
61 |
69 |
end
|
62 |
|
|
63 |
|
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
|
70 |
|
|
71 |
# Fetch the revisions by using a template file that
|
|
72 |
# makes Mercurial produce a xml output.
|
|
73 |
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
64 |
74 |
revisions = Revisions.new
|
|
75 |
cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
|
|
76 |
if identifier_from && identifier_to
|
|
77 |
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
|
78 |
elsif identifier_from
|
|
79 |
cmd << " -r #{identifier_from.to_i}:"
|
|
80 |
end
|
|
81 |
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
|
82 |
cmd << " #{path}" if path
|
|
83 |
shellout(cmd) do |io|
|
|
84 |
begin
|
|
85 |
# HG doesn't close the XML Document...
|
|
86 |
doc = REXML::Document.new(io.read << "</log>")
|
|
87 |
doc.elements.each("log/logentry") do |logentry|
|
|
88 |
paths = []
|
|
89 |
copies = logentry.get_elements('paths/path-copied')
|
|
90 |
logentry.elements.each("paths/path") do |path|
|
|
91 |
# Detect if the added file is a copy
|
|
92 |
if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
|
|
93 |
from_path = c.attributes['copyfrom-path']
|
|
94 |
from_rev = logentry.attributes['revision']
|
|
95 |
end
|
|
96 |
paths << {:action => path.attributes['action'],
|
|
97 |
:path => "/#{path.text}",
|
|
98 |
:from_path => from_path ? "/#{from_path}" : nil,
|
|
99 |
:from_revision => from_rev ? from_rev : nil
|
|
100 |
}
|
|
101 |
end
|
|
102 |
paths.sort! { |x,y| x[:path] <=> y[:path] }
|
|
103 |
|
|
104 |
revisions << Revision.new({:identifier => logentry.attributes['revision'],
|
|
105 |
:scmid => logentry.attributes['node'],
|
|
106 |
:author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
|
|
107 |
:time => Time.parse(logentry.elements['date'].text).localtime,
|
|
108 |
:message => logentry.elements['msg'].text,
|
|
109 |
:paths => paths
|
|
110 |
})
|
|
111 |
end
|
|
112 |
rescue
|
|
113 |
logger.debug($!)
|
|
114 |
end
|
|
115 |
end
|
|
116 |
return nil if $? && $?.exitstatus != 0
|
|
117 |
revisions
|
|
118 |
end
|
|
119 |
|
|
120 |
|
|
121 |
def revisions_old(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
|
122 |
revisions = Revisions.new
|
65 |
123 |
cmd = "#{HG_BIN} -v --encoding utf8 -R #{target('')} log"
|
66 |
124 |
if identifier_from && identifier_to
|
67 |
125 |
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
... | ... | |
125 |
183 |
|
126 |
184 |
def cat(path, identifier=nil)
|
127 |
185 |
cmd = "#{HG_BIN} -R #{target('')} cat"
|
128 |
|
cmd << " -r #{identifier.to_i}" if identifier
|
|
186 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
129 |
187 |
cmd << " #{target(path)}"
|
130 |
188 |
cat = nil
|
131 |
189 |
shellout(cmd) do |io|
|
... | ... | |
140 |
198 |
path ||= ''
|
141 |
199 |
cmd = "#{HG_BIN} -R #{target('')}"
|
142 |
200 |
cmd << " annotate -n -u"
|
|
201 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
143 |
202 |
cmd << " -r #{identifier.to_i}" if identifier
|
144 |
203 |
cmd << " #{target(path)}"
|
145 |
204 |
blame = Annotate.new
|
... | ... | |
153 |
212 |
blame
|
154 |
213 |
end
|
155 |
214 |
|
|
215 |
# The hg version version is exprimed either as a
|
|
216 |
# release number (eg 0.9.5 or 1.0) or as a revision
|
|
217 |
# id composed of 12 hexa characters.
|
|
218 |
def self.hgversion
|
|
219 |
@@hgversion ||= begin
|
|
220 |
theversion = self.hgversion_from_command_line
|
|
221 |
if theversion.match(/^\d+(\.\d+)+$/)
|
|
222 |
theversion.split(".").collect(&:to_i)
|
|
223 |
# elsif match = theversion.match(/[[:xdigit:]]{12}/)
|
|
224 |
# match[0]
|
|
225 |
else
|
|
226 |
"Unknown version"
|
|
227 |
end
|
|
228 |
end
|
|
229 |
end
|
|
230 |
|
|
231 |
def self.template_path
|
|
232 |
if self.hgversion.is_a?(String) or (self.hgversion <=> [0,9,5]) > 0
|
|
233 |
ver = "1.0"
|
|
234 |
else
|
|
235 |
ver = "0.9.5"
|
|
236 |
end
|
|
237 |
@@template ||= "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
|
|
238 |
end
|
|
239 |
|
|
240 |
|
156 |
241 |
private
|
157 |
242 |
|
158 |
243 |
# Builds a revision objet from the changeset returned by hg command
|
... | ... | |
161 |
246 |
|
162 |
247 |
# Changes
|
163 |
248 |
paths = (rev_id == 0) ?
|
164 |
|
# Can't get changes for revision 0 with hg status
|
165 |
|
changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
|
|
249 |
# Can't get changes for revision 0 with hg status
|
|
250 |
changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
|
166 |
251 |
status(rev_id)
|
167 |
252 |
|
168 |
253 |
Revision.new({:identifier => rev_id,
|
169 |
|
:scmid => changeset[:changeset].to_s.split(':').last,
|
170 |
|
:author => changeset[:user],
|
171 |
|
:time => Time.parse(changeset[:date]),
|
172 |
|
:message => changeset[:description],
|
173 |
|
:paths => paths
|
|
254 |
:scmid => changeset[:changeset].to_s.split(':').last,
|
|
255 |
:author => changeset[:user],
|
|
256 |
:time => Time.parse(changeset[:date]),
|
|
257 |
:message => changeset[:description],
|
|
258 |
:paths => paths
|
174 |
259 |
})
|
175 |
260 |
end
|
176 |
261 |
|
... | ... | |
193 |
278 |
end
|
194 |
279 |
result
|
195 |
280 |
end
|
|
281 |
|
|
282 |
def self.hgversion_from_command_line
|
|
283 |
%x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1]
|
|
284 |
end
|
196 |
285 |
end
|
197 |
286 |
end
|
198 |
287 |
end
|