##// END OF EJS Templates
scm: git: convert path encoding in "git log" (#5251)....
Toshi MARUYAMA -
r4916:08ee6a39325f
parent child
Show More
@@ -1,333 +1,335
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 = "#{self.class.sq_bin} --git-dir #{target('')} branch --no-color"
83 83 shellout(cmd) 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 end
90 90
91 91 def tags
92 92 return @tags if @tags
93 93 cmd = "#{self.class.sq_bin} --git-dir #{target('')} tag"
94 94 shellout(cmd) do |io|
95 95 @tags = io.readlines.sort!.map{|t| t.strip}
96 96 end
97 97 end
98 98
99 99 def default_branch
100 100 branches.include?('master') ? 'master' : branches.first
101 101 end
102 102
103 103 def entries(path=nil, identifier=nil)
104 104 path ||= ''
105 105 entries = Entries.new
106 106 cmd = "#{self.class.sq_bin} --git-dir #{target('')} ls-tree -l "
107 107 cmd << shell_quote("HEAD:" + path) if identifier.nil?
108 108 cmd << shell_quote(identifier + ":" + path) if identifier
109 109 shellout(cmd) do |io|
110 110 io.each_line do |line|
111 111 e = line.chomp.to_s
112 112 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
113 113 type = $1
114 114 sha = $2
115 115 size = $3
116 116 name = $4
117 117 full_path = path.empty? ? name : "#{path}/#{name}"
118 118 entries << Entry.new({:name => name,
119 119 :path => full_path,
120 120 :kind => (type == "tree") ? 'dir' : 'file',
121 121 :size => (type == "tree") ? nil : size,
122 122 :lastrev => @flag_report_last_commit ? lastrev(full_path,identifier) : Revision.new
123 123 }) unless entries.detect{|entry| entry.name == name}
124 124 end
125 125 end
126 126 end
127 127 return nil if $? && $?.exitstatus != 0
128 128 entries.sort_by_name
129 129 end
130 130
131 131 def lastrev(path, rev)
132 132 return nil if path.nil?
133 133 cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
134 134 cmd_args << rev if rev
135 135 cmd_args << "--" << path unless path.empty?
136 136 lines = []
137 137 scm_cmd(*cmd_args) { |io| lines = io.readlines }
138 138 begin
139 139 id = lines[0].split[1]
140 140 author = lines[1].match('Author:\s+(.*)$')[1]
141 141 time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
142 142
143 143 Revision.new({
144 144 :identifier => id,
145 145 :scmid => id,
146 146 :author => author,
147 147 :time => time,
148 148 :message => nil,
149 149 :paths => nil
150 150 })
151 151 rescue NoMethodError => e
152 152 logger.error("The revision '#{path}' has a wrong format")
153 153 return nil
154 154 end
155 155 rescue ScmCommandAborted
156 156 nil
157 157 end
158 158
159 159 def revisions(path, identifier_from, identifier_to, options={})
160 160 revisions = Revisions.new
161 161 cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller|
162 162 cmd_args << "--reverse" if options[:reverse]
163 163 cmd_args << "--all" if options[:all]
164 164 cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
165 165 from_to = ""
166 166 from_to << "#{identifier_from}.." if identifier_from
167 167 from_to << "#{identifier_to}" if identifier_to
168 168 cmd_args << from_to if !from_to.empty?
169 169 cmd_args << "--since=#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}" if options[:since]
170 170 cmd_args << "--" << path if path && !path.empty?
171 171
172 172 scm_cmd *cmd_args do |io|
173 173 files=[]
174 174 changeset = {}
175 175 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
176 176
177 177 io.each_line do |line|
178 178 if line =~ /^commit ([0-9a-f]{40})$/
179 179 key = "commit"
180 180 value = $1
181 181 if (parsing_descr == 1 || parsing_descr == 2)
182 182 parsing_descr = 0
183 183 revision = Revision.new({
184 184 :identifier => changeset[:commit],
185 185 :scmid => changeset[:commit],
186 186 :author => changeset[:author],
187 187 :time => Time.parse(changeset[:date]),
188 188 :message => changeset[:description],
189 189 :paths => files
190 190 })
191 191 if block_given?
192 192 yield revision
193 193 else
194 194 revisions << revision
195 195 end
196 196 changeset = {}
197 197 files = []
198 198 end
199 199 changeset[:commit] = $1
200 200 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
201 201 key = $1
202 202 value = $2
203 203 if key == "Author"
204 204 changeset[:author] = value
205 205 elsif key == "CommitDate"
206 206 changeset[:date] = value
207 207 end
208 208 elsif (parsing_descr == 0) && line.chomp.to_s == ""
209 209 parsing_descr = 1
210 210 changeset[:description] = ""
211 211 elsif (parsing_descr == 1 || parsing_descr == 2) \
212 212 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
213 213 parsing_descr = 2
214 214 fileaction = $1
215 215 filepath = $2
216 files << {:action => fileaction, :path => filepath}
216 p = scm_iconv('UTF-8', @path_encoding, filepath)
217 files << {:action => fileaction, :path => p}
217 218 elsif (parsing_descr == 1 || parsing_descr == 2) \
218 219 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
219 220 parsing_descr = 2
220 221 fileaction = $1
221 222 filepath = $3
222 files << {:action => fileaction, :path => filepath}
223 p = scm_iconv('UTF-8', @path_encoding, filepath)
224 files << {:action => fileaction, :path => p}
223 225 elsif (parsing_descr == 1) && line.chomp.to_s == ""
224 226 parsing_descr = 2
225 227 elsif (parsing_descr == 1)
226 228 changeset[:description] << line[4..-1]
227 229 end
228 230 end
229 231
230 232 if changeset[:commit]
231 233 revision = Revision.new({
232 234 :identifier => changeset[:commit],
233 235 :scmid => changeset[:commit],
234 236 :author => changeset[:author],
235 237 :time => Time.parse(changeset[:date]),
236 238 :message => changeset[:description],
237 239 :paths => files
238 240 })
239 241
240 242 if block_given?
241 243 yield revision
242 244 else
243 245 revisions << revision
244 246 end
245 247 end
246 248 end
247 249 revisions
248 250 rescue ScmCommandAborted
249 251 revisions
250 252 end
251 253
252 254 def diff(path, identifier_from, identifier_to=nil)
253 255 path ||= ''
254 256
255 257 if identifier_to
256 258 cmd = "#{self.class.sq_bin} --git-dir #{target('')} diff --no-color #{shell_quote identifier_to} #{shell_quote identifier_from}"
257 259 else
258 260 cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote identifier_from}"
259 261 end
260 262
261 263 cmd << " -- #{shell_quote path}" unless path.empty?
262 264 diff = []
263 265 shellout(cmd) do |io|
264 266 io.each_line do |line|
265 267 diff << line
266 268 end
267 269 end
268 270 return nil if $? && $?.exitstatus != 0
269 271 diff
270 272 end
271 273
272 274 def annotate(path, identifier=nil)
273 275 identifier = 'HEAD' if identifier.blank?
274 276 cmd = "#{self.class.sq_bin} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}"
275 277 blame = Annotate.new
276 278 content = nil
277 279 shellout(cmd) { |io| io.binmode; content = io.read }
278 280 return nil if $? && $?.exitstatus != 0
279 281 # git annotates binary files
280 282 return nil if content.is_binary_data?
281 283 identifier = ''
282 284 # git shows commit author on the first occurrence only
283 285 authors_by_commit = {}
284 286 content.split("\n").each do |line|
285 287 if line =~ /^([0-9a-f]{39,40})\s.*/
286 288 identifier = $1
287 289 elsif line =~ /^author (.+)/
288 290 authors_by_commit[identifier] = $1.strip
289 291 elsif line =~ /^\t(.*)/
290 292 blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier]))
291 293 identifier = ''
292 294 author = ''
293 295 end
294 296 end
295 297 blame
296 298 end
297 299
298 300 def cat(path, identifier=nil)
299 301 if identifier.nil?
300 302 identifier = 'HEAD'
301 303 end
302 304 cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote(identifier + ':' + path)}"
303 305 cat = nil
304 306 shellout(cmd) do |io|
305 307 io.binmode
306 308 cat = io.read
307 309 end
308 310 return nil if $? && $?.exitstatus != 0
309 311 cat
310 312 end
311 313
312 314 class Revision < Redmine::Scm::Adapters::Revision
313 315 # Returns the readable identifier
314 316 def format_identifier
315 317 identifier[0,8]
316 318 end
317 319 end
318 320
319 321 def scm_cmd(*args, &block)
320 322 repo_path = root_url || url
321 323 full_args = [GIT_BIN, '--git-dir', repo_path]
322 324 full_args += args
323 325 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
324 326 if $? && $?.exitstatus != 0
325 327 raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
326 328 end
327 329 ret
328 330 end
329 331 private :scm_cmd
330 332 end
331 333 end
332 334 end
333 335 end
General Comments 0
You need to be logged in to leave comments. Login now