Chris@0
|
1 # redMine - project management software
|
Chris@0
|
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 require 'redmine/scm/adapters/abstract_adapter'
|
Chris@119
|
19 require 'cgi'
|
Chris@0
|
20
|
Chris@0
|
21 module Redmine
|
Chris@0
|
22 module Scm
|
Chris@0
|
23 module Adapters
|
Chris@0
|
24 class MercurialAdapter < AbstractAdapter
|
Chris@119
|
25
|
Chris@0
|
26 # Mercurial executable name
|
Chris@210
|
27 HG_BIN = Redmine::Configuration['scm_mercurial_command'] || "hg"
|
Chris@0
|
28 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
|
Chris@0
|
29 TEMPLATE_NAME = "hg-template"
|
Chris@0
|
30 TEMPLATE_EXTENSION = "tmpl"
|
Chris@119
|
31
|
Chris@0
|
32 class << self
|
Chris@0
|
33 def client_version
|
Chris@0
|
34 @@client_version ||= (hgversion || [])
|
Chris@0
|
35 end
|
Chris@119
|
36
|
Chris@0
|
37 def hgversion
|
Chris@0
|
38 # The hg version is expressed either as a
|
Chris@0
|
39 # release number (eg 0.9.5 or 1.0) or as a revision
|
Chris@0
|
40 # id composed of 12 hexa characters.
|
Chris@0
|
41 theversion = hgversion_from_command_line
|
Chris@119
|
42 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
|
Chris@119
|
43 m[2].scan(%r{\d+}).collect(&:to_i)
|
Chris@0
|
44 end
|
Chris@0
|
45 end
|
Chris@119
|
46
|
Chris@0
|
47 def hgversion_from_command_line
|
Chris@119
|
48 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
|
Chris@0
|
49 end
|
Chris@119
|
50
|
Chris@0
|
51 def template_path
|
Chris@0
|
52 @@template_path ||= template_path_for(client_version)
|
Chris@0
|
53 end
|
Chris@119
|
54
|
Chris@0
|
55 def template_path_for(version)
|
Chris@0
|
56 if ((version <=> [0,9,5]) > 0) || version.empty?
|
Chris@0
|
57 ver = "1.0"
|
Chris@0
|
58 else
|
Chris@0
|
59 ver = "0.9.5"
|
Chris@0
|
60 end
|
Chris@0
|
61 "#{TEMPLATES_DIR}/#{TEMPLATE_NAME}-#{ver}.#{TEMPLATE_EXTENSION}"
|
Chris@0
|
62 end
|
Chris@0
|
63 end
|
Chris@119
|
64
|
Chris@0
|
65 def info
|
Chris@0
|
66 cmd = "#{HG_BIN} -R #{target('')} root"
|
Chris@0
|
67 root_url = nil
|
Chris@0
|
68 shellout(cmd) do |io|
|
Chris@0
|
69 root_url = io.read
|
Chris@0
|
70 end
|
Chris@0
|
71 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
72 info = Info.new({:root_url => root_url.chomp,
|
Chris@0
|
73 :lastrev => revisions(nil,nil,nil,{:limit => 1}).last
|
Chris@0
|
74 })
|
Chris@0
|
75 info
|
Chris@0
|
76 rescue CommandFailed
|
Chris@0
|
77 return nil
|
Chris@0
|
78 end
|
Chris@119
|
79
|
Chris@0
|
80 def entries(path=nil, identifier=nil)
|
Chris@0
|
81 path ||= ''
|
Chris@0
|
82 entries = Entries.new
|
Chris@0
|
83 cmd = "#{HG_BIN} -R #{target('')} --cwd #{target('')} locate"
|
Chris@119
|
84 cmd << " -r #{hgrev(identifier)}"
|
Chris@0
|
85 cmd << " " + shell_quote("path:#{path}") unless path.empty?
|
Chris@0
|
86 shellout(cmd) do |io|
|
Chris@0
|
87 io.each_line do |line|
|
Chris@0
|
88 # HG uses antislashs as separator on Windows
|
Chris@0
|
89 line = line.gsub(/\\/, "/")
|
Chris@0
|
90 if path.empty? or e = line.gsub!(%r{^#{with_trailling_slash(path)}},'')
|
Chris@0
|
91 e ||= line
|
Chris@0
|
92 e = e.chomp.split(%r{[\/\\]})
|
Chris@0
|
93 entries << Entry.new({:name => e.first,
|
Chris@0
|
94 :path => (path.nil? or path.empty? ? e.first : "#{with_trailling_slash(path)}#{e.first}"),
|
Chris@0
|
95 :kind => (e.size > 1 ? 'dir' : 'file'),
|
Chris@0
|
96 :lastrev => Revision.new
|
Chris@0
|
97 }) unless e.empty? || entries.detect{|entry| entry.name == e.first}
|
Chris@0
|
98 end
|
Chris@0
|
99 end
|
Chris@0
|
100 end
|
Chris@0
|
101 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
102 entries.sort_by_name
|
Chris@0
|
103 end
|
Chris@119
|
104
|
Chris@0
|
105 # Fetch the revisions by using a template file that
|
Chris@0
|
106 # makes Mercurial produce a xml output.
|
Chris@0
|
107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
Chris@0
|
108 revisions = Revisions.new
|
Chris@0
|
109 cmd = "#{HG_BIN} --debug --encoding utf8 -R #{target('')} log -C --style #{shell_quote self.class.template_path}"
|
Chris@0
|
110 if identifier_from && identifier_to
|
Chris@119
|
111 cmd << " -r #{hgrev(identifier_from)}:#{hgrev(identifier_to)}"
|
Chris@0
|
112 elsif identifier_from
|
Chris@119
|
113 cmd << " -r #{hgrev(identifier_from)}:"
|
Chris@0
|
114 end
|
Chris@0
|
115 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
Chris@119
|
116 cmd << " #{shell_quote path}" unless path.blank?
|
Chris@0
|
117 shellout(cmd) do |io|
|
Chris@0
|
118 begin
|
Chris@0
|
119 # HG doesn't close the XML Document...
|
Chris@0
|
120 doc = REXML::Document.new(io.read << "</log>")
|
Chris@0
|
121 doc.elements.each("log/logentry") do |logentry|
|
Chris@0
|
122 paths = []
|
Chris@0
|
123 copies = logentry.get_elements('paths/path-copied')
|
Chris@0
|
124 logentry.elements.each("paths/path") do |path|
|
Chris@0
|
125 # Detect if the added file is a copy
|
Chris@0
|
126 if path.attributes['action'] == 'A' and c = copies.find{ |e| e.text == path.text }
|
Chris@0
|
127 from_path = c.attributes['copyfrom-path']
|
Chris@0
|
128 from_rev = logentry.attributes['revision']
|
Chris@0
|
129 end
|
Chris@0
|
130 paths << {:action => path.attributes['action'],
|
Chris@119
|
131 :path => "/#{CGI.unescape(path.text)}",
|
Chris@119
|
132 :from_path => from_path ? "/#{CGI.unescape(from_path)}" : nil,
|
Chris@0
|
133 :from_revision => from_rev ? from_rev : nil
|
Chris@0
|
134 }
|
Chris@0
|
135 end
|
Chris@0
|
136 paths.sort! { |x,y| x[:path] <=> y[:path] }
|
Chris@119
|
137
|
Chris@0
|
138 revisions << Revision.new({:identifier => logentry.attributes['revision'],
|
Chris@0
|
139 :scmid => logentry.attributes['node'],
|
Chris@0
|
140 :author => (logentry.elements['author'] ? logentry.elements['author'].text : ""),
|
Chris@0
|
141 :time => Time.parse(logentry.elements['date'].text).localtime,
|
Chris@0
|
142 :message => logentry.elements['msg'].text,
|
Chris@0
|
143 :paths => paths
|
Chris@0
|
144 })
|
Chris@0
|
145 end
|
Chris@0
|
146 rescue
|
Chris@0
|
147 logger.debug($!)
|
Chris@0
|
148 end
|
Chris@0
|
149 end
|
Chris@0
|
150 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
151 revisions
|
Chris@0
|
152 end
|
Chris@119
|
153
|
Chris@0
|
154 def diff(path, identifier_from, identifier_to=nil)
|
Chris@0
|
155 path ||= ''
|
Chris@119
|
156 diff_args = ''
|
Chris@119
|
157 diff = []
|
Chris@0
|
158 if identifier_to
|
Chris@119
|
159 diff_args = "-r #{hgrev(identifier_to)} -r #{hgrev(identifier_from)}"
|
Chris@0
|
160 else
|
Chris@119
|
161 if self.class.client_version_above?([1, 2])
|
Chris@119
|
162 diff_args = "-c #{hgrev(identifier_from)}"
|
Chris@119
|
163 else
|
Chris@119
|
164 return []
|
Chris@119
|
165 end
|
Chris@0
|
166 end
|
Chris@119
|
167 cmd = "#{HG_BIN} -R #{target('')} --config diff.git=false diff --nodates #{diff_args}"
|
Chris@0
|
168 cmd << " -I #{target(path)}" unless path.empty?
|
Chris@0
|
169 shellout(cmd) do |io|
|
Chris@0
|
170 io.each_line do |line|
|
Chris@0
|
171 diff << line
|
Chris@0
|
172 end
|
Chris@0
|
173 end
|
Chris@0
|
174 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
175 diff
|
Chris@0
|
176 end
|
Chris@119
|
177
|
Chris@0
|
178 def cat(path, identifier=nil)
|
Chris@0
|
179 cmd = "#{HG_BIN} -R #{target('')} cat"
|
Chris@119
|
180 cmd << " -r #{hgrev(identifier)}"
|
Chris@0
|
181 cmd << " #{target(path)}"
|
Chris@0
|
182 cat = nil
|
Chris@0
|
183 shellout(cmd) do |io|
|
Chris@0
|
184 io.binmode
|
Chris@0
|
185 cat = io.read
|
Chris@0
|
186 end
|
Chris@0
|
187 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
188 cat
|
Chris@0
|
189 end
|
Chris@119
|
190
|
Chris@0
|
191 def annotate(path, identifier=nil)
|
Chris@0
|
192 path ||= ''
|
Chris@0
|
193 cmd = "#{HG_BIN} -R #{target('')}"
|
Chris@119
|
194 cmd << " annotate -ncu"
|
Chris@119
|
195 cmd << " -r #{hgrev(identifier)}"
|
Chris@0
|
196 cmd << " #{target(path)}"
|
Chris@0
|
197 blame = Annotate.new
|
Chris@0
|
198 shellout(cmd) do |io|
|
Chris@0
|
199 io.each_line do |line|
|
Chris@119
|
200 next unless line =~ %r{^([^:]+)\s(\d+)\s([0-9a-f]+):\s(.*)$}
|
Chris@119
|
201 r = Revision.new(:author => $1.strip, :revision => $2, :scmid => $3,
|
Chris@119
|
202 :identifier => $3)
|
Chris@119
|
203 blame.add_line($4.rstrip, r)
|
Chris@0
|
204 end
|
Chris@0
|
205 end
|
Chris@0
|
206 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
207 blame
|
Chris@0
|
208 end
|
Chris@119
|
209
|
Chris@119
|
210 class Revision < Redmine::Scm::Adapters::Revision
|
Chris@119
|
211 # Returns the readable identifier
|
Chris@119
|
212 def format_identifier
|
Chris@119
|
213 "#{revision}:#{scmid}"
|
Chris@119
|
214 end
|
Chris@119
|
215 end
|
Chris@119
|
216
|
Chris@119
|
217 # Returns correct revision identifier
|
Chris@119
|
218 def hgrev(identifier)
|
Chris@119
|
219 shell_quote(identifier.blank? ? 'tip' : identifier.to_s)
|
Chris@119
|
220 end
|
Chris@119
|
221 private :hgrev
|
Chris@0
|
222 end
|
Chris@0
|
223 end
|
Chris@0
|
224 end
|
Chris@0
|
225 end
|