@@ -1,323 +1,322 | |||
|
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 | # Git executable name |
|
25 | 25 | GIT_BIN = Redmine::Configuration['scm_git_command'] || "git" |
|
26 | 26 | |
|
27 | 27 | # raised if scm command exited with error, e.g. unknown revision. |
|
28 | 28 | class ScmCommandAborted < CommandFailed; end |
|
29 | 29 | |
|
30 | 30 | class << self |
|
31 | 31 | def client_command |
|
32 | 32 | @@bin ||= GIT_BIN |
|
33 | 33 | end |
|
34 | 34 | |
|
35 | 35 | def sq_bin |
|
36 | 36 | @@sq_bin ||= shell_quote(GIT_BIN) |
|
37 | 37 | end |
|
38 | 38 | |
|
39 | 39 | def client_version |
|
40 | 40 | @@client_version ||= (scm_command_version || []) |
|
41 | 41 | end |
|
42 | 42 | |
|
43 | 43 | def client_available |
|
44 | 44 | !client_version.empty? |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def scm_command_version |
|
48 | 48 | scm_version = scm_version_from_command_line |
|
49 | 49 | if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) |
|
50 | 50 | m[2].scan(%r{\d+}).collect(&:to_i) |
|
51 | 51 | end |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | def scm_version_from_command_line |
|
55 | 55 | shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s |
|
56 | 56 | end |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | def info |
|
60 | 60 | begin |
|
61 | 61 | Info.new(:root_url => url, :lastrev => lastrev('',nil)) |
|
62 | 62 | rescue |
|
63 | 63 | nil |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def branches |
|
68 | 68 | return @branches if @branches |
|
69 | 69 | @branches = [] |
|
70 | 70 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} branch --no-color" |
|
71 | 71 | shellout(cmd) do |io| |
|
72 | 72 | io.each_line do |line| |
|
73 | 73 | @branches << line.match('\s*\*?\s*(.*)$')[1] |
|
74 | 74 | end |
|
75 | 75 | end |
|
76 | 76 | @branches.sort! |
|
77 | 77 | end |
|
78 | 78 | |
|
79 | 79 | def tags |
|
80 | 80 | return @tags if @tags |
|
81 | 81 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} tag" |
|
82 | 82 | shellout(cmd) do |io| |
|
83 | 83 | @tags = io.readlines.sort!.map{|t| t.strip} |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | |
|
87 | 87 | def default_branch |
|
88 | 88 | branches.include?('master') ? 'master' : branches.first |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | def entries(path=nil, identifier=nil) |
|
92 | 92 | path ||= '' |
|
93 | 93 | entries = Entries.new |
|
94 | 94 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} ls-tree -l " |
|
95 | 95 | cmd << shell_quote("HEAD:" + path) if identifier.nil? |
|
96 | 96 | cmd << shell_quote(identifier + ":" + path) if identifier |
|
97 | 97 | shellout(cmd) do |io| |
|
98 | 98 | io.each_line do |line| |
|
99 | 99 | e = line.chomp.to_s |
|
100 | 100 | if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/ |
|
101 | 101 | type = $1 |
|
102 | 102 | sha = $2 |
|
103 | 103 | size = $3 |
|
104 | 104 | name = $4 |
|
105 | 105 | full_path = path.empty? ? name : "#{path}/#{name}" |
|
106 | 106 | entries << Entry.new({:name => name, |
|
107 | 107 | :path => full_path, |
|
108 | 108 | :kind => (type == "tree") ? 'dir' : 'file', |
|
109 | 109 | :size => (type == "tree") ? nil : size, |
|
110 | 110 | :lastrev => lastrev(full_path,identifier) |
|
111 | 111 | }) unless entries.detect{|entry| entry.name == name} |
|
112 | 112 | end |
|
113 | 113 | end |
|
114 | 114 | end |
|
115 | 115 | return nil if $? && $?.exitstatus != 0 |
|
116 | 116 | entries.sort_by_name |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def lastrev(path,rev) |
|
120 | 120 | return nil if path.nil? |
|
121 | 121 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} log --no-color --date=iso --pretty=fuller --no-merges -n 1 " |
|
122 | 122 | cmd << " #{shell_quote rev} " if rev |
|
123 | 123 | cmd << "-- #{shell_quote path} " unless path.empty? |
|
124 | 124 | lines = [] |
|
125 | 125 | shellout(cmd) { |io| lines = io.readlines } |
|
126 | 126 | return nil if $? && $?.exitstatus != 0 |
|
127 | 127 | begin |
|
128 | 128 | id = lines[0].split[1] |
|
129 | 129 | author = lines[1].match('Author:\s+(.*)$')[1] |
|
130 | 130 | time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1]) |
|
131 | 131 | |
|
132 | 132 | Revision.new({ |
|
133 | 133 | :identifier => id, |
|
134 | 134 | :scmid => id, |
|
135 | 135 | :author => author, |
|
136 | 136 | :time => time, |
|
137 | 137 | :message => nil, |
|
138 | 138 | :paths => nil |
|
139 | 139 | }) |
|
140 | 140 | rescue NoMethodError => e |
|
141 | 141 | logger.error("The revision '#{path}' has a wrong format") |
|
142 | 142 | return nil |
|
143 | 143 | end |
|
144 | 144 | end |
|
145 | 145 | |
|
146 | 146 | def revisions(path, identifier_from, identifier_to, options={}) |
|
147 | 147 | revisions = Revisions.new |
|
148 | cmd_args = %w|log --raw --date=iso --pretty=fuller| | |
|
148 | cmd_args = %w|log --no-color --raw --date=iso --pretty=fuller| | |
|
149 | 149 | cmd_args << "--reverse" if options[:reverse] |
|
150 | 150 | cmd_args << "--all" if options[:all] |
|
151 | 151 | cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit] |
|
152 | 152 | from_to = "" |
|
153 | 153 | from_to << "#{identifier_from}.." if identifier_from |
|
154 | 154 | from_to << "#{identifier_to}" if identifier_to |
|
155 | 155 | cmd_args << from_to if !from_to.empty? |
|
156 | 156 | cmd_args << "--since=#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}" if options[:since] |
|
157 | 157 | cmd_args << "--" << "#{path}" if path && !path.empty? |
|
158 | 158 | |
|
159 | 159 | scm_cmd *cmd_args do |io| |
|
160 | 160 | files=[] |
|
161 | 161 | changeset = {} |
|
162 | 162 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files |
|
163 | 163 | revno = 1 |
|
164 | 164 | |
|
165 | 165 | io.each_line do |line| |
|
166 | 166 | if line =~ /^commit ([0-9a-f]{40})$/ |
|
167 | 167 | key = "commit" |
|
168 | 168 | value = $1 |
|
169 | 169 | if (parsing_descr == 1 || parsing_descr == 2) |
|
170 | 170 | parsing_descr = 0 |
|
171 | 171 | revision = Revision.new({ |
|
172 | 172 | :identifier => changeset[:commit], |
|
173 | 173 | :scmid => changeset[:commit], |
|
174 | 174 | :author => changeset[:author], |
|
175 | 175 | :time => Time.parse(changeset[:date]), |
|
176 | 176 | :message => changeset[:description], |
|
177 | 177 | :paths => files |
|
178 | 178 | }) |
|
179 | 179 | if block_given? |
|
180 | 180 | yield revision |
|
181 | 181 | else |
|
182 | 182 | revisions << revision |
|
183 | 183 | end |
|
184 | 184 | changeset = {} |
|
185 | 185 | files = [] |
|
186 | 186 | revno = revno + 1 |
|
187 | 187 | end |
|
188 | 188 | changeset[:commit] = $1 |
|
189 | 189 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ |
|
190 | 190 | key = $1 |
|
191 | 191 | value = $2 |
|
192 | 192 | if key == "Author" |
|
193 | 193 | changeset[:author] = value |
|
194 | 194 | elsif key == "CommitDate" |
|
195 | 195 | changeset[:date] = value |
|
196 | 196 | end |
|
197 | 197 | elsif (parsing_descr == 0) && line.chomp.to_s == "" |
|
198 | 198 | parsing_descr = 1 |
|
199 | 199 | changeset[:description] = "" |
|
200 | 200 | elsif (parsing_descr == 1 || parsing_descr == 2) \ |
|
201 | 201 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/ |
|
202 | 202 | parsing_descr = 2 |
|
203 | 203 | fileaction = $1 |
|
204 | 204 | filepath = $2 |
|
205 | 205 | files << {:action => fileaction, :path => filepath} |
|
206 | 206 | elsif (parsing_descr == 1 || parsing_descr == 2) \ |
|
207 | 207 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/ |
|
208 | 208 | parsing_descr = 2 |
|
209 | 209 | fileaction = $1 |
|
210 | 210 | filepath = $3 |
|
211 | 211 | files << {:action => fileaction, :path => filepath} |
|
212 | 212 | elsif (parsing_descr == 1) && line.chomp.to_s == "" |
|
213 | 213 | parsing_descr = 2 |
|
214 | 214 | elsif (parsing_descr == 1) |
|
215 | 215 | changeset[:description] << line[4..-1] |
|
216 | 216 | end |
|
217 | 217 | end |
|
218 | 218 | |
|
219 | 219 | if changeset[:commit] |
|
220 | 220 | revision = Revision.new({ |
|
221 | 221 | :identifier => changeset[:commit], |
|
222 | 222 | :scmid => changeset[:commit], |
|
223 | 223 | :author => changeset[:author], |
|
224 | 224 | :time => Time.parse(changeset[:date]), |
|
225 | 225 | :message => changeset[:description], |
|
226 | 226 | :paths => files |
|
227 | 227 | }) |
|
228 | 228 | |
|
229 | 229 | if block_given? |
|
230 | 230 | yield revision |
|
231 | 231 | else |
|
232 | 232 | revisions << revision |
|
233 | 233 | end |
|
234 | 234 | end |
|
235 | 235 | end |
|
236 | 236 | revisions |
|
237 | 237 | rescue ScmCommandAborted |
|
238 | 238 | revisions |
|
239 | 239 | end |
|
240 | 240 | |
|
241 | 241 | def diff(path, identifier_from, identifier_to=nil) |
|
242 | 242 | path ||= '' |
|
243 | 243 | |
|
244 | 244 | if identifier_to |
|
245 | 245 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} diff --no-color #{shell_quote identifier_to} #{shell_quote identifier_from}" |
|
246 | 246 | else |
|
247 | 247 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote identifier_from}" |
|
248 | 248 | end |
|
249 | 249 | |
|
250 | 250 | cmd << " -- #{shell_quote path}" unless path.empty? |
|
251 | 251 | diff = [] |
|
252 | 252 | shellout(cmd) do |io| |
|
253 | 253 | io.each_line do |line| |
|
254 | 254 | diff << line |
|
255 | 255 | end |
|
256 | 256 | end |
|
257 | 257 | return nil if $? && $?.exitstatus != 0 |
|
258 | 258 | diff |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | def annotate(path, identifier=nil) |
|
262 | 262 | identifier = 'HEAD' if identifier.blank? |
|
263 | 263 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}" |
|
264 | 264 | blame = Annotate.new |
|
265 | 265 | content = nil |
|
266 | 266 | shellout(cmd) { |io| io.binmode; content = io.read } |
|
267 | 267 | return nil if $? && $?.exitstatus != 0 |
|
268 | 268 | # git annotates binary files |
|
269 | 269 | return nil if content.is_binary_data? |
|
270 | 270 | identifier = '' |
|
271 | 271 | # git shows commit author on the first occurrence only |
|
272 | 272 | authors_by_commit = {} |
|
273 | 273 | content.split("\n").each do |line| |
|
274 | 274 | if line =~ /^([0-9a-f]{39,40})\s.*/ |
|
275 | 275 | identifier = $1 |
|
276 | 276 | elsif line =~ /^author (.+)/ |
|
277 | 277 | authors_by_commit[identifier] = $1.strip |
|
278 | 278 | elsif line =~ /^\t(.*)/ |
|
279 | 279 | blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier])) |
|
280 | 280 | identifier = '' |
|
281 | 281 | author = '' |
|
282 | 282 | end |
|
283 | 283 | end |
|
284 | 284 | blame |
|
285 | 285 | end |
|
286 | 286 | |
|
287 | 287 | def cat(path, identifier=nil) |
|
288 | 288 | if identifier.nil? |
|
289 | 289 | identifier = 'HEAD' |
|
290 | 290 | end |
|
291 | 291 | cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote(identifier + ':' + path)}" |
|
292 | 292 | cat = nil |
|
293 | 293 | shellout(cmd) do |io| |
|
294 | 294 | io.binmode |
|
295 | 295 | cat = io.read |
|
296 | 296 | end |
|
297 | 297 | return nil if $? && $?.exitstatus != 0 |
|
298 | 298 | cat |
|
299 | 299 | end |
|
300 | 300 | |
|
301 | 301 | class Revision < Redmine::Scm::Adapters::Revision |
|
302 | 302 | # Returns the readable identifier |
|
303 | 303 | def format_identifier |
|
304 | 304 | identifier[0,8] |
|
305 | 305 | end |
|
306 | 306 | end |
|
307 | 307 | |
|
308 | 308 | def scm_cmd(*args, &block) |
|
309 | 309 | repo_path = root_url || url |
|
310 | 310 | full_args = [GIT_BIN, '--git-dir', repo_path] |
|
311 | 311 | full_args += args |
|
312 | full_args << '--no-color' | |
|
313 | 312 | ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block) |
|
314 | 313 | if $? && $?.exitstatus != 0 |
|
315 | 314 | raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}" |
|
316 | 315 | end |
|
317 | 316 | ret |
|
318 | 317 | end |
|
319 | 318 | private :scm_cmd |
|
320 | 319 | end |
|
321 | 320 | end |
|
322 | 321 | end |
|
323 | 322 | end |
General Comments 0
You need to be logged in to leave comments.
Login now