Chris@441
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 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@441
|
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@441
|
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@1136
|
18 require_dependency 'redmine/scm/adapters/abstract_adapter'
|
Chris@0
|
19 require 'rexml/document'
|
Chris@0
|
20
|
Chris@0
|
21 module Redmine
|
Chris@0
|
22 module Scm
|
Chris@245
|
23 module Adapters
|
Chris@245
|
24 class DarcsAdapter < AbstractAdapter
|
Chris@0
|
25 # Darcs executable name
|
Chris@210
|
26 DARCS_BIN = Redmine::Configuration['scm_darcs_command'] || "darcs"
|
Chris@245
|
27
|
Chris@0
|
28 class << self
|
Chris@245
|
29 def client_command
|
Chris@245
|
30 @@bin ||= DARCS_BIN
|
Chris@245
|
31 end
|
Chris@245
|
32
|
Chris@245
|
33 def sq_bin
|
Chris@909
|
34 @@sq_bin ||= shell_quote_command
|
Chris@245
|
35 end
|
Chris@245
|
36
|
Chris@0
|
37 def client_version
|
Chris@0
|
38 @@client_version ||= (darcs_binary_version || [])
|
Chris@0
|
39 end
|
Chris@245
|
40
|
Chris@245
|
41 def client_available
|
Chris@245
|
42 !client_version.empty?
|
Chris@245
|
43 end
|
Chris@245
|
44
|
Chris@0
|
45 def darcs_binary_version
|
Chris@245
|
46 darcsversion = darcs_binary_version_from_command_line.dup
|
Chris@245
|
47 if darcsversion.respond_to?(:force_encoding)
|
Chris@245
|
48 darcsversion.force_encoding('ASCII-8BIT')
|
Chris@245
|
49 end
|
Chris@210
|
50 if m = darcsversion.match(%r{\A(.*?)((\d+\.)+\d+)})
|
Chris@210
|
51 m[2].scan(%r{\d+}).collect(&:to_i)
|
Chris@0
|
52 end
|
Chris@210
|
53 end
|
Chris@210
|
54
|
Chris@210
|
55 def darcs_binary_version_from_command_line
|
Chris@245
|
56 shellout("#{sq_bin} --version") { |io| io.read }.to_s
|
Chris@0
|
57 end
|
Chris@0
|
58 end
|
Chris@0
|
59
|
Chris@245
|
60 def initialize(url, root_url=nil, login=nil, password=nil,
|
Chris@245
|
61 path_encoding=nil)
|
Chris@0
|
62 @url = url
|
Chris@0
|
63 @root_url = url
|
Chris@0
|
64 end
|
Chris@0
|
65
|
Chris@0
|
66 def supports_cat?
|
Chris@0
|
67 # cat supported in darcs 2.0.0 and higher
|
Chris@0
|
68 self.class.client_version_above?([2, 0, 0])
|
Chris@0
|
69 end
|
Chris@0
|
70
|
Chris@0
|
71 # Get info about the darcs repository
|
Chris@0
|
72 def info
|
Chris@0
|
73 rev = revisions(nil,nil,nil,{:limit => 1})
|
Chris@0
|
74 rev ? Info.new({:root_url => @url, :lastrev => rev.last}) : nil
|
Chris@0
|
75 end
|
Chris@245
|
76
|
Chris@0
|
77 # Returns an Entries collection
|
Chris@0
|
78 # or nil if the given path doesn't exist in the repository
|
Chris@441
|
79 def entries(path=nil, identifier=nil, options={})
|
Chris@0
|
80 path_prefix = (path.blank? ? '' : "#{path}/")
|
Chris@210
|
81 if path.blank?
|
Chris@210
|
82 path = ( self.class.client_version_above?([2, 2, 0]) ? @url : '.' )
|
Chris@210
|
83 end
|
Chris@245
|
84 entries = Entries.new
|
Chris@245
|
85 cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --xml-output"
|
Chris@0
|
86 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
|
Chris@0
|
87 cmd << " #{shell_quote path}"
|
Chris@0
|
88 shellout(cmd) do |io|
|
Chris@0
|
89 begin
|
Chris@0
|
90 doc = REXML::Document.new(io)
|
Chris@0
|
91 if doc.root.name == 'directory'
|
Chris@0
|
92 doc.elements.each('directory/*') do |element|
|
Chris@0
|
93 next unless ['file', 'directory'].include? element.name
|
Chris@0
|
94 entries << entry_from_xml(element, path_prefix)
|
Chris@0
|
95 end
|
Chris@0
|
96 elsif doc.root.name == 'file'
|
Chris@0
|
97 entries << entry_from_xml(doc.root, path_prefix)
|
Chris@0
|
98 end
|
Chris@0
|
99 rescue
|
Chris@0
|
100 end
|
Chris@0
|
101 end
|
Chris@0
|
102 return nil if $? && $?.exitstatus != 0
|
Chris@1115
|
103 entries.compact!
|
Chris@1115
|
104 entries.sort_by_name
|
Chris@0
|
105 end
|
Chris@245
|
106
|
Chris@0
|
107 def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
|
Chris@0
|
108 path = '.' if path.blank?
|
Chris@0
|
109 revisions = Revisions.new
|
Chris@245
|
110 cmd = "#{self.class.sq_bin} changes --repodir #{shell_quote @url} --xml-output"
|
Chris@0
|
111 cmd << " --from-match #{shell_quote("hash #{identifier_from}")}" if identifier_from
|
Chris@0
|
112 cmd << " --last #{options[:limit].to_i}" if options[:limit]
|
Chris@0
|
113 shellout(cmd) do |io|
|
Chris@0
|
114 begin
|
Chris@0
|
115 doc = REXML::Document.new(io)
|
Chris@0
|
116 doc.elements.each("changelog/patch") do |patch|
|
Chris@0
|
117 message = patch.elements['name'].text
|
Chris@0
|
118 message << "\n" + patch.elements['comment'].text.gsub(/\*\*\*END OF DESCRIPTION\*\*\*.*\z/m, '') if patch.elements['comment']
|
Chris@0
|
119 revisions << Revision.new({:identifier => nil,
|
Chris@0
|
120 :author => patch.attributes['author'],
|
Chris@0
|
121 :scmid => patch.attributes['hash'],
|
Chris@0
|
122 :time => Time.parse(patch.attributes['local_date']),
|
Chris@0
|
123 :message => message,
|
Chris@0
|
124 :paths => (options[:with_path] ? get_paths_for_patch(patch.attributes['hash']) : nil)
|
Chris@0
|
125 })
|
Chris@0
|
126 end
|
Chris@0
|
127 rescue
|
Chris@0
|
128 end
|
Chris@0
|
129 end
|
Chris@0
|
130 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
131 revisions
|
Chris@0
|
132 end
|
Chris@245
|
133
|
Chris@0
|
134 def diff(path, identifier_from, identifier_to=nil)
|
Chris@0
|
135 path = '*' if path.blank?
|
Chris@245
|
136 cmd = "#{self.class.sq_bin} diff --repodir #{shell_quote @url}"
|
Chris@0
|
137 if identifier_to.nil?
|
Chris@0
|
138 cmd << " --match #{shell_quote("hash #{identifier_from}")}"
|
Chris@0
|
139 else
|
Chris@0
|
140 cmd << " --to-match #{shell_quote("hash #{identifier_from}")}"
|
Chris@0
|
141 cmd << " --from-match #{shell_quote("hash #{identifier_to}")}"
|
Chris@0
|
142 end
|
Chris@0
|
143 cmd << " -u #{shell_quote path}"
|
Chris@0
|
144 diff = []
|
Chris@0
|
145 shellout(cmd) do |io|
|
Chris@0
|
146 io.each_line do |line|
|
Chris@0
|
147 diff << line
|
Chris@0
|
148 end
|
Chris@0
|
149 end
|
Chris@0
|
150 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
151 diff
|
Chris@0
|
152 end
|
Chris@245
|
153
|
Chris@0
|
154 def cat(path, identifier=nil)
|
Chris@245
|
155 cmd = "#{self.class.sq_bin} show content --repodir #{shell_quote @url}"
|
Chris@0
|
156 cmd << " --match #{shell_quote("hash #{identifier}")}" if identifier
|
Chris@0
|
157 cmd << " #{shell_quote path}"
|
Chris@0
|
158 cat = nil
|
Chris@0
|
159 shellout(cmd) do |io|
|
Chris@0
|
160 io.binmode
|
Chris@0
|
161 cat = io.read
|
Chris@0
|
162 end
|
Chris@0
|
163 return nil if $? && $?.exitstatus != 0
|
Chris@0
|
164 cat
|
Chris@0
|
165 end
|
Chris@0
|
166
|
Chris@0
|
167 private
|
Chris@245
|
168
|
Chris@0
|
169 # Returns an Entry from the given XML element
|
Chris@0
|
170 # or nil if the entry was deleted
|
Chris@0
|
171 def entry_from_xml(element, path_prefix)
|
Chris@0
|
172 modified_element = element.elements['modified']
|
Chris@0
|
173 if modified_element.elements['modified_how'].text.match(/removed/)
|
Chris@0
|
174 return nil
|
Chris@0
|
175 end
|
Chris@245
|
176
|
Chris@0
|
177 Entry.new({:name => element.attributes['name'],
|
Chris@0
|
178 :path => path_prefix + element.attributes['name'],
|
Chris@0
|
179 :kind => element.name == 'file' ? 'file' : 'dir',
|
Chris@0
|
180 :size => nil,
|
Chris@0
|
181 :lastrev => Revision.new({
|
Chris@0
|
182 :identifier => nil,
|
Chris@0
|
183 :scmid => modified_element.elements['patch'].attributes['hash']
|
Chris@0
|
184 })
|
Chris@245
|
185 })
|
Chris@0
|
186 end
|
Chris@210
|
187
|
Chris@210
|
188 def get_paths_for_patch(hash)
|
Chris@210
|
189 paths = get_paths_for_patch_raw(hash)
|
Chris@210
|
190 if self.class.client_version_above?([2, 4])
|
Chris@210
|
191 orig_paths = paths
|
Chris@210
|
192 paths = []
|
Chris@210
|
193 add_paths = []
|
Chris@210
|
194 add_paths_name = []
|
Chris@210
|
195 mod_paths = []
|
Chris@210
|
196 other_paths = []
|
Chris@210
|
197 orig_paths.each do |path|
|
Chris@210
|
198 if path[:action] == 'A'
|
Chris@210
|
199 add_paths << path
|
Chris@210
|
200 add_paths_name << path[:path]
|
Chris@210
|
201 elsif path[:action] == 'M'
|
Chris@210
|
202 mod_paths << path
|
Chris@210
|
203 else
|
Chris@210
|
204 other_paths << path
|
Chris@210
|
205 end
|
Chris@210
|
206 end
|
Chris@210
|
207 add_paths_name.each do |add_path|
|
Chris@210
|
208 mod_paths.delete_if { |m| m[:path] == add_path }
|
Chris@210
|
209 end
|
Chris@210
|
210 paths.concat add_paths
|
Chris@210
|
211 paths.concat mod_paths
|
Chris@210
|
212 paths.concat other_paths
|
Chris@210
|
213 end
|
Chris@210
|
214 paths
|
Chris@210
|
215 end
|
Chris@245
|
216
|
Chris@0
|
217 # Retrieve changed paths for a single patch
|
Chris@210
|
218 def get_paths_for_patch_raw(hash)
|
Chris@245
|
219 cmd = "#{self.class.sq_bin} annotate --repodir #{shell_quote @url} --summary --xml-output"
|
Chris@0
|
220 cmd << " --match #{shell_quote("hash #{hash}")} "
|
Chris@0
|
221 paths = []
|
Chris@0
|
222 shellout(cmd) do |io|
|
Chris@0
|
223 begin
|
Chris@0
|
224 # Darcs xml output has multiple root elements in this case (tested with darcs 1.0.7)
|
Chris@0
|
225 # A root element is added so that REXML doesn't raise an error
|
Chris@0
|
226 doc = REXML::Document.new("<fake_root>" + io.read + "</fake_root>")
|
Chris@0
|
227 doc.elements.each('fake_root/summary/*') do |modif|
|
Chris@0
|
228 paths << {:action => modif.name[0,1].upcase,
|
Chris@0
|
229 :path => "/" + modif.text.chomp.gsub(/^\s*/, '')
|
Chris@0
|
230 }
|
Chris@0
|
231 end
|
Chris@0
|
232 rescue
|
Chris@0
|
233 end
|
Chris@0
|
234 end
|
Chris@0
|
235 paths
|
Chris@0
|
236 rescue CommandFailed
|
Chris@0
|
237 paths
|
Chris@0
|
238 end
|
Chris@0
|
239 end
|
Chris@0
|
240 end
|
Chris@0
|
241 end
|
Chris@0
|
242 end
|