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@117
|
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@0
|
25
|
Chris@0
|
26 # Mercurial executable name
|
Chris@0
|
27 HG_BIN = "hg"
|
Chris@0
|
28 TEMPLATES_DIR = File.dirname(__FILE__) + "/mercurial"
|
Chris@0
|
29 TEMPLATE_NAME = "hg-template"
|
Chris@0
|
30 TEMPLATE_EXTENSION = "tmpl"
|
Chris@0
|
31
|
Chris@0
|
32 class << self
|
Chris@0
|
33 def client_version
|
Chris@0
|
34 @@client_version ||= (hgversion || [])
|
Chris@0
|
35 end
|
Chris@0
|
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@117
|
42 if m = theversion.match(%r{\A(.*?)((\d+\.)+\d+)})
|
Chris@117
|
43 m[2].scan(%r{\d+}).collect(&:to_i)
|
Chris@0
|
44 end
|
Chris@0
|
45 end
|
Chris@0
|
46
|
Chris@0
|
47 def hgversion_from_command_line
|
Chris@117
|
48 shellout("#{HG_BIN} --version") { |io| io.read }.to_s
|
Chris@0
|
49 end
|
Chris@0
|
50
|
Chris@0
|
51 def template_path
|
Chris@0
|
52 @@template_path ||= template_path_for(client_version)
|
Chris@0
|
53 end
|
Chris@0
|
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@0
|
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@0
|
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@117
|
84 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
|
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@0
|
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@0
|
111 cmd << " -r #{identifier_from.to_i}:#{identifier_to.to_i}"
|
Chris@0
|
112 elsif identifier_from
|
Chris@0
|
113 cmd << " -r #{identifier_from.to_i}:"
|
Chris@0
|
114 end
|
Chris@0
|
115 cmd << " --limit #{options[:limit].to_i}" if options[:limit]
|
Chris@117
|
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@117
|
131 :path => "/#{CGI.unescape(path.text)}",
|
Chris@117
|
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@0
|
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@0
|
153
|
Chris@0
|
154 def diff(path, identifier_from, identifier_to=nil)
|
Chris@0
|
155 path ||= ''
|
Chris@0
|
156 if identifier_to
|
Chris@0
|
157 identifier_to = identifier_to.to_i
|
Chris@0
|
158 else
|
Chris@0
|
159 identifier_to = identifier_from.to_i - 1
|
Chris@0
|
160 end
|
Chris@117
|
161 if identifier_from
|
Chris@117
|
162 identifier_from = identifier_from.to_i
|
Chris@117
|
163 end
|
Chris@0
|
164 cmd = "#{HG_BIN} -R #{target('')} diff -r #{identifier_to} -r #{identifier_from} --nodates"
|
Chris@0
|
165 cmd << " -I #{target(path)}" unless path.empty?
|
Chris@0
|
166 diff = []
|
Chris@0
|
167 shellout(cmd) do |io|
|
Chris@0
|
168 io.each_line do |line|
|
Chris@0
|
169 diff << line
|
Chris@0
|
170 end
|
Chris@0
|
171 end
|
Chris@0
|
172 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
173 diff
|
Chris@0
|
174 end
|
Chris@0
|
175
|
Chris@0
|
176 def cat(path, identifier=nil)
|
Chris@0
|
177 cmd = "#{HG_BIN} -R #{target('')} cat"
|
Chris@117
|
178 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
|
Chris@0
|
179 cmd << " #{target(path)}"
|
Chris@0
|
180 cat = nil
|
Chris@0
|
181 shellout(cmd) do |io|
|
Chris@0
|
182 io.binmode
|
Chris@0
|
183 cat = io.read
|
Chris@0
|
184 end
|
Chris@0
|
185 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
186 cat
|
Chris@0
|
187 end
|
Chris@0
|
188
|
Chris@0
|
189 def annotate(path, identifier=nil)
|
Chris@0
|
190 path ||= ''
|
Chris@0
|
191 cmd = "#{HG_BIN} -R #{target('')}"
|
Chris@0
|
192 cmd << " annotate -n -u"
|
Chris@117
|
193 cmd << " -r " + shell_quote(identifier ? identifier.to_s : "tip")
|
Chris@0
|
194 cmd << " -r #{identifier.to_i}" if identifier
|
Chris@0
|
195 cmd << " #{target(path)}"
|
Chris@0
|
196 blame = Annotate.new
|
Chris@0
|
197 shellout(cmd) do |io|
|
Chris@0
|
198 io.each_line do |line|
|
Chris@0
|
199 next unless line =~ %r{^([^:]+)\s(\d+):(.*)$}
|
Chris@0
|
200 blame.add_line($3.rstrip, Revision.new(:identifier => $2.to_i, :author => $1.strip))
|
Chris@0
|
201 end
|
Chris@0
|
202 end
|
Chris@0
|
203 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
204 blame
|
Chris@0
|
205 end
|
Chris@0
|
206 end
|
Chris@0
|
207 end
|
Chris@0
|
208 end
|
Chris@0
|
209 end
|