@@ -1,261 +1,262 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require 'redmine/scm/adapters/abstract_adapter' |
|
18 | require 'redmine/scm/adapters/abstract_adapter' | |
19 |
|
19 | |||
20 | module Redmine |
|
20 | module Redmine | |
21 | module Scm |
|
21 | module Scm | |
22 | module Adapters |
|
22 | module Adapters | |
23 | class GitAdapter < AbstractAdapter |
|
23 | class GitAdapter < AbstractAdapter | |
24 |
|
24 | |||
25 | # Git executable name |
|
25 | # Git executable name | |
26 | GIT_BIN = "git" |
|
26 | GIT_BIN = "git" | |
27 |
|
27 | |||
28 | # Get the revision of a particuliar file |
|
28 | # Get the revision of a particuliar file | |
29 | def get_rev (rev,path) |
|
29 | def get_rev (rev,path) | |
30 | cmd="git --git-dir #{target('')} show #{shell_quote rev} -- #{shell_quote path}" if rev!='latest' and (! rev.nil?) |
|
30 | cmd="git --git-dir #{target('')} show #{shell_quote rev} -- #{shell_quote path}" if rev!='latest' and (! rev.nil?) | |
31 | cmd="git --git-dir #{target('')} log -1 master -- #{shell_quote path}" if |
|
31 | cmd="git --git-dir #{target('')} log -1 master -- #{shell_quote path}" if | |
32 | rev=='latest' or rev.nil? |
|
32 | rev=='latest' or rev.nil? | |
33 | rev=[] |
|
33 | rev=[] | |
34 | i=0 |
|
34 | i=0 | |
35 | shellout(cmd) do |io| |
|
35 | shellout(cmd) do |io| | |
36 | files=[] |
|
36 | files=[] | |
37 | changeset = {} |
|
37 | changeset = {} | |
38 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files |
|
38 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files | |
39 |
|
39 | |||
40 | io.each_line do |line| |
|
40 | io.each_line do |line| | |
41 | if line =~ /^commit ([0-9a-f]{40})$/ |
|
41 | if line =~ /^commit ([0-9a-f]{40})$/ | |
42 | key = "commit" |
|
42 | key = "commit" | |
43 | value = $1 |
|
43 | value = $1 | |
44 | if (parsing_descr == 1 || parsing_descr == 2) |
|
44 | if (parsing_descr == 1 || parsing_descr == 2) | |
45 | parsing_descr = 0 |
|
45 | parsing_descr = 0 | |
46 | rev = Revision.new({:identifier => changeset[:commit], |
|
46 | rev = Revision.new({:identifier => changeset[:commit], | |
47 | :scmid => changeset[:commit], |
|
47 | :scmid => changeset[:commit], | |
48 | :author => changeset[:author], |
|
48 | :author => changeset[:author], | |
49 | :time => Time.parse(changeset[:date]), |
|
49 | :time => Time.parse(changeset[:date]), | |
50 | :message => changeset[:description], |
|
50 | :message => changeset[:description], | |
51 | :paths => files |
|
51 | :paths => files | |
52 | }) |
|
52 | }) | |
53 | changeset = {} |
|
53 | changeset = {} | |
54 | files = [] |
|
54 | files = [] | |
55 | end |
|
55 | end | |
56 | changeset[:commit] = $1 |
|
56 | changeset[:commit] = $1 | |
57 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ |
|
57 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ | |
58 | key = $1 |
|
58 | key = $1 | |
59 | value = $2 |
|
59 | value = $2 | |
60 | if key == "Author" |
|
60 | if key == "Author" | |
61 | changeset[:author] = value |
|
61 | changeset[:author] = value | |
62 | elsif key == "Date" |
|
62 | elsif key == "Date" | |
63 | changeset[:date] = value |
|
63 | changeset[:date] = value | |
64 | end |
|
64 | end | |
65 | elsif (parsing_descr == 0) && line.chomp.to_s == "" |
|
65 | elsif (parsing_descr == 0) && line.chomp.to_s == "" | |
66 | parsing_descr = 1 |
|
66 | parsing_descr = 1 | |
67 | changeset[:description] = "" |
|
67 | changeset[:description] = "" | |
68 | elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ |
|
68 | elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ | |
69 | parsing_descr = 2 |
|
69 | parsing_descr = 2 | |
70 | fileaction = $1 |
|
70 | fileaction = $1 | |
71 | filepath = $2 |
|
71 | filepath = $2 | |
72 | files << {:action => fileaction, :path => filepath} |
|
72 | files << {:action => fileaction, :path => filepath} | |
73 | elsif (parsing_descr == 1) && line.chomp.to_s == "" |
|
73 | elsif (parsing_descr == 1) && line.chomp.to_s == "" | |
74 | parsing_descr = 2 |
|
74 | parsing_descr = 2 | |
75 | elsif (parsing_descr == 1) |
|
75 | elsif (parsing_descr == 1) | |
76 | changeset[:description] << line |
|
76 | changeset[:description] << line | |
77 | end |
|
77 | end | |
78 | end |
|
78 | end | |
79 | rev = Revision.new({:identifier => changeset[:commit], |
|
79 | rev = Revision.new({:identifier => changeset[:commit], | |
80 | :scmid => changeset[:commit], |
|
80 | :scmid => changeset[:commit], | |
81 | :author => changeset[:author], |
|
81 | :author => changeset[:author], | |
82 | :time => Time.parse(changeset[:date]), |
|
82 | :time => Time.parse(changeset[:date]), | |
83 | :message => changeset[:description], |
|
83 | :message => changeset[:description], | |
84 | :paths => files |
|
84 | :paths => files | |
85 | }) |
|
85 | }) | |
86 |
|
86 | |||
87 | end |
|
87 | end | |
88 |
|
88 | |||
89 | get_rev('latest',path) if rev == [] |
|
89 | get_rev('latest',path) if rev == [] | |
90 |
|
90 | |||
91 | return nil if $? && $?.exitstatus != 0 |
|
91 | return nil if $? && $?.exitstatus != 0 | |
92 | return rev |
|
92 | return rev | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | def info |
|
96 | def info | |
97 | root_url = target('') |
|
97 | revs = revisions(url,nil,nil,{:limit => 1}) | |
98 | info = Info.new({:root_url => target(''), |
|
98 | if revs && revs.any? | |
99 | :lastrev => revisions(root_url,nil,nil,{:limit => 1}).first |
|
99 | Info.new(:root_url => url, :lastrev => revs.first) | |
100 | }) |
|
100 | else | |
101 |
|
|
101 | nil | |
|
102 | end | |||
102 | rescue Errno::ENOENT => e |
|
103 | rescue Errno::ENOENT => e | |
103 | return nil |
|
104 | return nil | |
104 | end |
|
105 | end | |
105 |
|
106 | |||
106 | def entries(path=nil, identifier=nil) |
|
107 | def entries(path=nil, identifier=nil) | |
107 | path ||= '' |
|
108 | path ||= '' | |
108 | entries = Entries.new |
|
109 | entries = Entries.new | |
109 | cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l " |
|
110 | cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l " | |
110 | cmd << shell_quote("HEAD:" + path) if identifier.nil? |
|
111 | cmd << shell_quote("HEAD:" + path) if identifier.nil? | |
111 | cmd << shell_quote(identifier + ":" + path) if identifier |
|
112 | cmd << shell_quote(identifier + ":" + path) if identifier | |
112 | shellout(cmd) do |io| |
|
113 | shellout(cmd) do |io| | |
113 | io.each_line do |line| |
|
114 | io.each_line do |line| | |
114 | e = line.chomp.to_s |
|
115 | e = line.chomp.to_s | |
115 | if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/ |
|
116 | if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/ | |
116 | type = $1 |
|
117 | type = $1 | |
117 | sha = $2 |
|
118 | sha = $2 | |
118 | size = $3 |
|
119 | size = $3 | |
119 | name = $4 |
|
120 | name = $4 | |
120 | entries << Entry.new({:name => name, |
|
121 | entries << Entry.new({:name => name, | |
121 | :path => (path.empty? ? name : "#{path}/#{name}"), |
|
122 | :path => (path.empty? ? name : "#{path}/#{name}"), | |
122 | :kind => ((type == "tree") ? 'dir' : 'file'), |
|
123 | :kind => ((type == "tree") ? 'dir' : 'file'), | |
123 | :size => ((type == "tree") ? nil : size), |
|
124 | :size => ((type == "tree") ? nil : size), | |
124 | :lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}")) |
|
125 | :lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}")) | |
125 |
|
126 | |||
126 | }) unless entries.detect{|entry| entry.name == name} |
|
127 | }) unless entries.detect{|entry| entry.name == name} | |
127 | end |
|
128 | end | |
128 | end |
|
129 | end | |
129 | end |
|
130 | end | |
130 | return nil if $? && $?.exitstatus != 0 |
|
131 | return nil if $? && $?.exitstatus != 0 | |
131 | entries.sort_by_name |
|
132 | entries.sort_by_name | |
132 | end |
|
133 | end | |
133 |
|
134 | |||
134 | def entry(path=nil, identifier=nil) |
|
135 | def entry(path=nil, identifier=nil) | |
135 | path ||= '' |
|
136 | path ||= '' | |
136 | search_path = path.split('/')[0..-2].join('/') |
|
137 | search_path = path.split('/')[0..-2].join('/') | |
137 | entry_name = path.split('/').last |
|
138 | entry_name = path.split('/').last | |
138 | e = entries(search_path, identifier) |
|
139 | e = entries(search_path, identifier) | |
139 | e ? e.detect{|entry| entry.name == entry_name} : nil |
|
140 | e ? e.detect{|entry| entry.name == entry_name} : nil | |
140 | end |
|
141 | end | |
141 |
|
142 | |||
142 | def revisions(path, identifier_from, identifier_to, options={}) |
|
143 | def revisions(path, identifier_from, identifier_to, options={}) | |
143 | revisions = Revisions.new |
|
144 | revisions = Revisions.new | |
144 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw " |
|
145 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw " | |
145 | cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit] |
|
146 | cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit] | |
146 | cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from |
|
147 | cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from | |
147 | cmd << " #{shell_quote identifier_to} " if identifier_to |
|
148 | cmd << " #{shell_quote identifier_to} " if identifier_to | |
148 | #cmd << " HEAD " if !identifier_to |
|
149 | #cmd << " HEAD " if !identifier_to | |
149 | shellout(cmd) do |io| |
|
150 | shellout(cmd) do |io| | |
150 | files=[] |
|
151 | files=[] | |
151 | changeset = {} |
|
152 | changeset = {} | |
152 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files |
|
153 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files | |
153 | revno = 1 |
|
154 | revno = 1 | |
154 |
|
155 | |||
155 | io.each_line do |line| |
|
156 | io.each_line do |line| | |
156 | if line =~ /^commit ([0-9a-f]{40})$/ |
|
157 | if line =~ /^commit ([0-9a-f]{40})$/ | |
157 | key = "commit" |
|
158 | key = "commit" | |
158 | value = $1 |
|
159 | value = $1 | |
159 | if (parsing_descr == 1 || parsing_descr == 2) |
|
160 | if (parsing_descr == 1 || parsing_descr == 2) | |
160 | parsing_descr = 0 |
|
161 | parsing_descr = 0 | |
161 | revisions << Revision.new({:identifier => changeset[:commit], |
|
162 | revisions << Revision.new({:identifier => changeset[:commit], | |
162 | :scmid => changeset[:commit], |
|
163 | :scmid => changeset[:commit], | |
163 | :author => changeset[:author], |
|
164 | :author => changeset[:author], | |
164 | :time => Time.parse(changeset[:date]), |
|
165 | :time => Time.parse(changeset[:date]), | |
165 | :message => changeset[:description], |
|
166 | :message => changeset[:description], | |
166 | :paths => files |
|
167 | :paths => files | |
167 | }) |
|
168 | }) | |
168 | changeset = {} |
|
169 | changeset = {} | |
169 | files = [] |
|
170 | files = [] | |
170 | revno = revno + 1 |
|
171 | revno = revno + 1 | |
171 | end |
|
172 | end | |
172 | changeset[:commit] = $1 |
|
173 | changeset[:commit] = $1 | |
173 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ |
|
174 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ | |
174 | key = $1 |
|
175 | key = $1 | |
175 | value = $2 |
|
176 | value = $2 | |
176 | if key == "Author" |
|
177 | if key == "Author" | |
177 | changeset[:author] = value |
|
178 | changeset[:author] = value | |
178 | elsif key == "Date" |
|
179 | elsif key == "Date" | |
179 | changeset[:date] = value |
|
180 | changeset[:date] = value | |
180 | end |
|
181 | end | |
181 | elsif (parsing_descr == 0) && line.chomp.to_s == "" |
|
182 | elsif (parsing_descr == 0) && line.chomp.to_s == "" | |
182 | parsing_descr = 1 |
|
183 | parsing_descr = 1 | |
183 | changeset[:description] = "" |
|
184 | changeset[:description] = "" | |
184 | elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ |
|
185 | elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ | |
185 | parsing_descr = 2 |
|
186 | parsing_descr = 2 | |
186 | fileaction = $1 |
|
187 | fileaction = $1 | |
187 | filepath = $2 |
|
188 | filepath = $2 | |
188 | files << {:action => fileaction, :path => filepath} |
|
189 | files << {:action => fileaction, :path => filepath} | |
189 | elsif (parsing_descr == 1) && line.chomp.to_s == "" |
|
190 | elsif (parsing_descr == 1) && line.chomp.to_s == "" | |
190 | parsing_descr = 2 |
|
191 | parsing_descr = 2 | |
191 | elsif (parsing_descr == 1) |
|
192 | elsif (parsing_descr == 1) | |
192 | changeset[:description] << line[4..-1] |
|
193 | changeset[:description] << line[4..-1] | |
193 | end |
|
194 | end | |
194 | end |
|
195 | end | |
195 |
|
196 | |||
196 | revisions << Revision.new({:identifier => changeset[:commit], |
|
197 | revisions << Revision.new({:identifier => changeset[:commit], | |
197 | :scmid => changeset[:commit], |
|
198 | :scmid => changeset[:commit], | |
198 | :author => changeset[:author], |
|
199 | :author => changeset[:author], | |
199 | :time => Time.parse(changeset[:date]), |
|
200 | :time => Time.parse(changeset[:date]), | |
200 | :message => changeset[:description], |
|
201 | :message => changeset[:description], | |
201 | :paths => files |
|
202 | :paths => files | |
202 | }) if changeset[:commit] |
|
203 | }) if changeset[:commit] | |
203 |
|
204 | |||
204 | end |
|
205 | end | |
205 |
|
206 | |||
206 | return nil if $? && $?.exitstatus != 0 |
|
207 | return nil if $? && $?.exitstatus != 0 | |
207 | revisions |
|
208 | revisions | |
208 | end |
|
209 | end | |
209 |
|
210 | |||
210 | def diff(path, identifier_from, identifier_to=nil, type="inline") |
|
211 | def diff(path, identifier_from, identifier_to=nil, type="inline") | |
211 | path ||= '' |
|
212 | path ||= '' | |
212 | if !identifier_to |
|
213 | if !identifier_to | |
213 | identifier_to = nil |
|
214 | identifier_to = nil | |
214 | end |
|
215 | end | |
215 |
|
216 | |||
216 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil? |
|
217 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil? | |
217 | cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil? |
|
218 | cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil? | |
218 | cmd << " -- #{shell_quote path}" unless path.empty? |
|
219 | cmd << " -- #{shell_quote path}" unless path.empty? | |
219 | diff = [] |
|
220 | diff = [] | |
220 | shellout(cmd) do |io| |
|
221 | shellout(cmd) do |io| | |
221 | io.each_line do |line| |
|
222 | io.each_line do |line| | |
222 | diff << line |
|
223 | diff << line | |
223 | end |
|
224 | end | |
224 | end |
|
225 | end | |
225 | return nil if $? && $?.exitstatus != 0 |
|
226 | return nil if $? && $?.exitstatus != 0 | |
226 | DiffTableList.new diff, type |
|
227 | DiffTableList.new diff, type | |
227 | end |
|
228 | end | |
228 |
|
229 | |||
229 | def annotate(path, identifier=nil) |
|
230 | def annotate(path, identifier=nil) | |
230 | identifier = 'HEAD' if identifier.blank? |
|
231 | identifier = 'HEAD' if identifier.blank? | |
231 | cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}" |
|
232 | cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}" | |
232 | blame = Annotate.new |
|
233 | blame = Annotate.new | |
233 | shellout(cmd) do |io| |
|
234 | shellout(cmd) do |io| | |
234 | io.each_line do |line| |
|
235 | io.each_line do |line| | |
235 | next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)$/ |
|
236 | next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)$/ | |
236 | blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip)) |
|
237 | blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip)) | |
237 | end |
|
238 | end | |
238 | end |
|
239 | end | |
239 | return nil if $? && $?.exitstatus != 0 |
|
240 | return nil if $? && $?.exitstatus != 0 | |
240 | blame |
|
241 | blame | |
241 | end |
|
242 | end | |
242 |
|
243 | |||
243 | def cat(path, identifier=nil) |
|
244 | def cat(path, identifier=nil) | |
244 | if identifier.nil? |
|
245 | if identifier.nil? | |
245 | identifier = 'HEAD' |
|
246 | identifier = 'HEAD' | |
246 | end |
|
247 | end | |
247 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}" |
|
248 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}" | |
248 | cat = nil |
|
249 | cat = nil | |
249 | shellout(cmd) do |io| |
|
250 | shellout(cmd) do |io| | |
250 | io.binmode |
|
251 | io.binmode | |
251 | cat = io.read |
|
252 | cat = io.read | |
252 | end |
|
253 | end | |
253 | return nil if $? && $?.exitstatus != 0 |
|
254 | return nil if $? && $?.exitstatus != 0 | |
254 | cat |
|
255 | cat | |
255 | end |
|
256 | end | |
256 | end |
|
257 | end | |
257 | end |
|
258 | end | |
258 | end |
|
259 | end | |
259 |
|
260 | |||
260 | end |
|
261 | end | |
261 |
|
262 |
General Comments 0
You need to be logged in to leave comments.
Login now