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"
|
... | ... | |
40 |
43 |
return nil
|
41 |
44 |
end
|
42 |
45 |
|
43 |
|
def entries(path=nil, identifier=nil)
|
44 |
|
path ||= ''
|
|
46 |
def entries(path="", identifier=nil)
|
45 |
47 |
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:**')
|
|
48 |
cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
|
|
49 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
|
50 |
cmd << " " + shell_quote("path:#{path}") unless path.empty?
|
49 |
51 |
shellout(cmd) do |io|
|
50 |
52 |
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}
|
|
53 |
# HG uses antislashs as separator on Windows
|
|
54 |
line = line.gsub(/\\/, "/")
|
|
55 |
if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
|
|
56 |
e ||= line
|
|
57 |
e = e.chomp.split(%r{[\/\\]})
|
|
58 |
entries << Entry.new({:name => e.first,
|
|
59 |
:path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
|
|
60 |
:kind => (e.size > 1 ? 'dir' : 'file'),
|
|
61 |
:lastrev => Revision.new
|
|
62 |
}) unless entries.detect{|entry| entry.name == e.first}
|
|
63 |
end
|
57 |
64 |
end
|
58 |
65 |
end
|
59 |
66 |
return nil if $? && $?.exitstatus != 0
|
60 |
67 |
entries.sort_by_name
|
61 |
68 |
end
|
62 |
69 |
|
|
70 |
|
|
71 |
# Fetch the revisions by using a template file that make Mercurial producing a xml output.
|
63 |
72 |
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
64 |
73 |
revisions = Revisions.new
|
|
74 |
cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{self.class.template_path}"
|
|
75 |
if identifier_from && identifier_to
|
|
76 |
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
|
77 |
elsif identifier_from
|
|
78 |
cmd << " -r #{identifier_from.to_i}:"
|
|
79 |
end
|
|
80 |
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
|
81 |
cmd << " #{path}" if path
|
|
82 |
shellout(cmd) do |io|
|
|
83 |
begin
|
|
84 |
# HG doesn't close the XML Document...
|
|
85 |
doc = REXML::Document.new(io.read << "</log>")
|
|
86 |
doc.elements.each("log/logentry") do |logentry|
|
|
87 |
paths = []
|
|
88 |
copies = logentry.get_elements('paths/path-copied')
|
|
89 |
logentry.elements.each("paths/path") do |path|
|
|
90 |
# Detect if the added file is a copy
|
|
91 |
if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
|
|
92 |
from_path = c.attributes['copyfrom-path']
|
|
93 |
from_rev = logentry.attributes['revision']
|
|
94 |
end
|
|
95 |
paths << {:action => path.attributes['action'],
|
|
96 |
:path => "/#{path.text}",
|
|
97 |
:from_path => from_path ? "/#{from_path}" : nil,
|
|
98 |
:from_revision => from_rev ? from_rev : nil
|
|
99 |
}
|
|
100 |
end
|
|
101 |
paths.sort! { |x,y| x[:path] <=> y[:path] }
|
|
102 |
|
|
103 |
revisions << Revision.new({:identifier => logentry.attributes['revision'],
|
|
104 |
:scmid => logentry.attributes['node'],
|
|
105 |
:author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
|
|
106 |
:time => Time.parse(logentry.elements['date'].text).localtime,
|
|
107 |
:message => logentry.elements['msg'].text,
|
|
108 |
:paths => paths
|
|
109 |
})
|
|
110 |
end
|
|
111 |
rescue
|
|
112 |
logger.debug($!)
|
|
113 |
end
|
|
114 |
end
|
|
115 |
return nil if $? && $?.exitstatus != 0
|
|
116 |
revisions
|
|
117 |
end
|
|
118 |
|
|
119 |
|
|
120 |
def revisions_old(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
|
121 |
revisions = Revisions.new
|
65 |
122 |
cmd = "#{HG_BIN} -v --encoding utf8 -R #{target('')} log"
|
66 |
123 |
if identifier_from && identifier_to
|
67 |
124 |
cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
... | ... | |
125 |
182 |
|
126 |
183 |
def cat(path, identifier=nil)
|
127 |
184 |
cmd = "#{HG_BIN} -R #{target('')} cat"
|
128 |
|
cmd << " -r #{identifier.to_i}" if identifier
|
|
185 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
129 |
186 |
cmd << " #{target(path)}"
|
130 |
187 |
cat = nil
|
131 |
188 |
shellout(cmd) do |io|
|
... | ... | |
140 |
197 |
path ||= ''
|
141 |
198 |
cmd = "#{HG_BIN} -R #{target('')}"
|
142 |
199 |
cmd << " annotate -n -u"
|
143 |
|
cmd << " -r #{identifier.to_i}" if identifier
|
|
200 |
cmd << " -r " + (identifier ? identifier.to_s : "tip")
|
144 |
201 |
cmd << " #{target(path)}"
|
145 |
202 |
blame = Annotate.new
|
146 |
203 |
shellout(cmd) do |io|
|
... | ... | |
161 |
218 |
|
162 |
219 |
# Changes
|
163 |
220 |
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}"}} :
|
|
221 |
# Can't get changes for revision 0 with hg status
|
|
222 |
changeset[:files].to_s.split.collect{|path| {:action => 'A', :path => "/#{path}"}} :
|
166 |
223 |
status(rev_id)
|
167 |
224 |
|
168 |
225 |
Revision.new({:identifier => rev_id,
|
... | ... | |
193 |
250 |
end
|
194 |
251 |
result
|
195 |
252 |
end
|
|
253 |
|
|
254 |
def self.hgversion
|
|
255 |
@@hgversion ||= %x{#{HG_BIN} --version}.match(/\(version (.*)\)/)[1].split(".").collect(&:to_i)
|
|
256 |
end
|
|
257 |
|
|
258 |
def self.template_path
|
|
259 |
if (self.hgversion <=> [0,9,5]) > 0
|
|
260 |
ver = "1.0"
|
|
261 |
else
|
|
262 |
ver = "0.9.5"
|
|
263 |
end
|
|
264 |
@@template ||= "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
|
|
265 |
end
|
|
266 |
|
196 |
267 |
end
|
197 |
268 |
end
|
198 |
269 |
end
|