##// END OF EJS Templates
scm: git: switch "-c core.quotepath=false" in git version above 1.7.2 or not (#5251)....
Toshi MARUYAMA -
r4949:16be7f2e3009
parent child
Show More
@@ -1,352 +1,355
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require 'redmine/scm/adapters/abstract_adapter'
19 19
20 20 module Redmine
21 21 module Scm
22 22 module Adapters
23 23 class GitAdapter < AbstractAdapter
24 24
25 25 SCM_GIT_REPORT_LAST_COMMIT = true
26 26
27 27 # Git executable name
28 28 GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
29 29
30 30 # raised if scm command exited with error, e.g. unknown revision.
31 31 class ScmCommandAborted < CommandFailed; end
32 32
33 33 class << self
34 34 def client_command
35 35 @@bin ||= GIT_BIN
36 36 end
37 37
38 38 def sq_bin
39 39 @@sq_bin ||= shell_quote(GIT_BIN)
40 40 end
41 41
42 42 def client_version
43 43 @@client_version ||= (scm_command_version || [])
44 44 end
45 45
46 46 def client_available
47 47 !client_version.empty?
48 48 end
49 49
50 50 def scm_command_version
51 51 scm_version = scm_version_from_command_line.dup
52 52 if scm_version.respond_to?(:force_encoding)
53 53 scm_version.force_encoding('ASCII-8BIT')
54 54 end
55 55 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
56 56 m[2].scan(%r{\d+}).collect(&:to_i)
57 57 end
58 58 end
59 59
60 60 def scm_version_from_command_line
61 61 shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s
62 62 end
63 63 end
64 64
65 65 def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
66 66 super
67 67 @path_encoding = path_encoding || 'UTF-8'
68 68 @flag_report_last_commit = SCM_GIT_REPORT_LAST_COMMIT
69 69 end
70 70
71 71 def info
72 72 begin
73 73 Info.new(:root_url => url, :lastrev => lastrev('',nil))
74 74 rescue
75 75 nil
76 76 end
77 77 end
78 78
79 79 def branches
80 80 return @branches if @branches
81 81 @branches = []
82 82 cmd_args = %w|branch --no-color|
83 83 scm_cmd(*cmd_args) do |io|
84 84 io.each_line do |line|
85 85 @branches << line.match('\s*\*?\s*(.*)$')[1]
86 86 end
87 87 end
88 88 @branches.sort!
89 89 rescue ScmCommandAborted
90 90 nil
91 91 end
92 92
93 93 def tags
94 94 return @tags if @tags
95 95 cmd_args = %w|tag|
96 96 scm_cmd(*cmd_args) do |io|
97 97 @tags = io.readlines.sort!.map{|t| t.strip}
98 98 end
99 99 rescue ScmCommandAborted
100 100 nil
101 101 end
102 102
103 103 def default_branch
104 104 branches.include?('master') ? 'master' : branches.first
105 105 end
106 106
107 107 def entries(path=nil, identifier=nil)
108 108 path ||= ''
109 109 p = scm_iconv(@path_encoding, 'UTF-8', path)
110 110 entries = Entries.new
111 111 cmd_args = %w|ls-tree -l|
112 112 cmd_args << "HEAD:#{p}" if identifier.nil?
113 113 cmd_args << "#{identifier}:#{p}" if identifier
114 114 scm_cmd(*cmd_args) do |io|
115 115 io.each_line do |line|
116 116 e = line.chomp.to_s
117 117 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
118 118 type = $1
119 119 sha = $2
120 120 size = $3
121 121 name = $4
122 122 if name.respond_to?(:force_encoding)
123 123 name.force_encoding(@path_encoding)
124 124 end
125 125 full_path = p.empty? ? name : "#{p}/#{name}"
126 126 n = scm_iconv('UTF-8', @path_encoding, name)
127 127 full_p = scm_iconv('UTF-8', @path_encoding, full_path)
128 128 entries << Entry.new({:name => n,
129 129 :path => full_p,
130 130 :kind => (type == "tree") ? 'dir' : 'file',
131 131 :size => (type == "tree") ? nil : size,
132 132 :lastrev => @flag_report_last_commit ? lastrev(full_path, identifier) : Revision.new
133 133 }) unless entries.detect{|entry| entry.name == name}
134 134 end
135 135 end
136 136 end
137 137 entries.sort_by_name
138 138 rescue ScmCommandAborted
139 139 nil
140 140 end
141 141
142 142 def lastrev(path, rev)
143 143 return nil if path.nil?
144 144 cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
145 145 cmd_args << rev if rev
146 146 cmd_args << "--" << path unless path.empty?
147 147 lines = []
148 148 scm_cmd(*cmd_args) { |io| lines = io.readlines }
149 149 begin
150 150 id = lines[0].split[1]
151 151 author = lines[1].match('Author:\s+(.*)$')[1]
152 152 time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
153 153
154 154 Revision.new({
155 155 :identifier => id,
156 156 :scmid => id,
157 157 :author => author,
158 158 :time => time,
159 159 :message => nil,
160 160 :paths => nil
161 161 })
162 162 rescue NoMethodError => e
163 163 logger.error("The revision '#{path}' has a wrong format")
164 164 return nil
165 165 end
166 166 rescue ScmCommandAborted
167 167 nil
168 168 end
169 169
170 170 def revisions(path, identifier_from, identifier_to, options={})
171 171 revisions = Revisions.new
172 172 cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller|
173 173 cmd_args << "--reverse" if options[:reverse]
174 174 cmd_args << "--all" if options[:all]
175 175 cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
176 176 from_to = ""
177 177 from_to << "#{identifier_from}.." if identifier_from
178 178 from_to << "#{identifier_to}" if identifier_to
179 179 cmd_args << from_to if !from_to.empty?
180 180 cmd_args << "--since=#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}" if options[:since]
181 181 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
182 182
183 183 scm_cmd *cmd_args do |io|
184 184 files=[]
185 185 changeset = {}
186 186 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
187 187
188 188 io.each_line do |line|
189 189 if line =~ /^commit ([0-9a-f]{40})$/
190 190 key = "commit"
191 191 value = $1
192 192 if (parsing_descr == 1 || parsing_descr == 2)
193 193 parsing_descr = 0
194 194 revision = Revision.new({
195 195 :identifier => changeset[:commit],
196 196 :scmid => changeset[:commit],
197 197 :author => changeset[:author],
198 198 :time => Time.parse(changeset[:date]),
199 199 :message => changeset[:description],
200 200 :paths => files
201 201 })
202 202 if block_given?
203 203 yield revision
204 204 else
205 205 revisions << revision
206 206 end
207 207 changeset = {}
208 208 files = []
209 209 end
210 210 changeset[:commit] = $1
211 211 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
212 212 key = $1
213 213 value = $2
214 214 if key == "Author"
215 215 changeset[:author] = value
216 216 elsif key == "CommitDate"
217 217 changeset[:date] = value
218 218 end
219 219 elsif (parsing_descr == 0) && line.chomp.to_s == ""
220 220 parsing_descr = 1
221 221 changeset[:description] = ""
222 222 elsif (parsing_descr == 1 || parsing_descr == 2) \
223 223 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
224 224 parsing_descr = 2
225 225 fileaction = $1
226 226 filepath = $2
227 227 p = scm_iconv('UTF-8', @path_encoding, filepath)
228 228 files << {:action => fileaction, :path => p}
229 229 elsif (parsing_descr == 1 || parsing_descr == 2) \
230 230 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
231 231 parsing_descr = 2
232 232 fileaction = $1
233 233 filepath = $3
234 234 p = scm_iconv('UTF-8', @path_encoding, filepath)
235 235 files << {:action => fileaction, :path => p}
236 236 elsif (parsing_descr == 1) && line.chomp.to_s == ""
237 237 parsing_descr = 2
238 238 elsif (parsing_descr == 1)
239 239 changeset[:description] << line[4..-1]
240 240 end
241 241 end
242 242
243 243 if changeset[:commit]
244 244 revision = Revision.new({
245 245 :identifier => changeset[:commit],
246 246 :scmid => changeset[:commit],
247 247 :author => changeset[:author],
248 248 :time => Time.parse(changeset[:date]),
249 249 :message => changeset[:description],
250 250 :paths => files
251 251 })
252 252
253 253 if block_given?
254 254 yield revision
255 255 else
256 256 revisions << revision
257 257 end
258 258 end
259 259 end
260 260 revisions
261 261 rescue ScmCommandAborted
262 262 revisions
263 263 end
264 264
265 265 def diff(path, identifier_from, identifier_to=nil)
266 266 path ||= ''
267 267 cmd_args = []
268 268 if identifier_to
269 269 cmd_args << "diff" << "--no-color" << identifier_to << identifier_from
270 270 else
271 271 cmd_args << "show" << "--no-color" << identifier_from
272 272 end
273 273 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
274 274 diff = []
275 275 scm_cmd *cmd_args do |io|
276 276 io.each_line do |line|
277 277 diff << line
278 278 end
279 279 end
280 280 diff
281 281 rescue ScmCommandAborted
282 282 nil
283 283 end
284 284
285 285 def annotate(path, identifier=nil)
286 286 identifier = 'HEAD' if identifier.blank?
287 287 cmd_args = %w|blame|
288 288 cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path)
289 289 blame = Annotate.new
290 290 content = nil
291 291 scm_cmd(*cmd_args) { |io| io.binmode; content = io.read }
292 292 # git annotates binary files
293 293 return nil if content.is_binary_data?
294 294 identifier = ''
295 295 # git shows commit author on the first occurrence only
296 296 authors_by_commit = {}
297 297 content.split("\n").each do |line|
298 298 if line =~ /^([0-9a-f]{39,40})\s.*/
299 299 identifier = $1
300 300 elsif line =~ /^author (.+)/
301 301 authors_by_commit[identifier] = $1.strip
302 302 elsif line =~ /^\t(.*)/
303 303 blame.add_line($1, Revision.new(
304 304 :identifier => identifier,
305 305 :author => authors_by_commit[identifier]))
306 306 identifier = ''
307 307 author = ''
308 308 end
309 309 end
310 310 blame
311 311 rescue ScmCommandAborted
312 312 nil
313 313 end
314 314
315 315 def cat(path, identifier=nil)
316 316 if identifier.nil?
317 317 identifier = 'HEAD'
318 318 end
319 319 cmd_args = %w|show --no-color|
320 320 cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
321 321 cat = nil
322 322 scm_cmd(*cmd_args) do |io|
323 323 io.binmode
324 324 cat = io.read
325 325 end
326 326 cat
327 327 rescue ScmCommandAborted
328 328 nil
329 329 end
330 330
331 331 class Revision < Redmine::Scm::Adapters::Revision
332 332 # Returns the readable identifier
333 333 def format_identifier
334 334 identifier[0,8]
335 335 end
336 336 end
337 337
338 338 def scm_cmd(*args, &block)
339 339 repo_path = root_url || url
340 full_args = [GIT_BIN, '--git-dir', repo_path, '-c', 'core.quotepath=false']
340 full_args = [GIT_BIN, '--git-dir', repo_path]
341 if self.class.client_version_above?([1, 7, 2])
342 full_args << '-c' << 'core.quotepath=false'
343 end
341 344 full_args += args
342 345 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
343 346 if $? && $?.exitstatus != 0
344 347 raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
345 348 end
346 349 ret
347 350 end
348 351 private :scm_cmd
349 352 end
350 353 end
351 354 end
352 355 end
General Comments 0
You need to be logged in to leave comments. Login now