@@ -1,259 +1,260 | |||||
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 | # Git executable name |
|
24 | # Git executable name | |
25 | GIT_BIN = "git" |
|
25 | GIT_BIN = "git" | |
26 |
|
26 | |||
27 | def info |
|
27 | def info | |
28 | begin |
|
28 | begin | |
29 | Info.new(:root_url => url, :lastrev => lastrev('',nil)) |
|
29 | Info.new(:root_url => url, :lastrev => lastrev('',nil)) | |
30 | rescue |
|
30 | rescue | |
31 | nil |
|
31 | nil | |
32 | end |
|
32 | end | |
33 | end |
|
33 | end | |
34 |
|
34 | |||
35 | def branches |
|
35 | def branches | |
36 |
|
|
36 | return @branches if @branches | |
|
37 | @branches = [] | |||
37 | cmd = "#{GIT_BIN} --git-dir #{target('')} branch" |
|
38 | cmd = "#{GIT_BIN} --git-dir #{target('')} branch" | |
38 | shellout(cmd) do |io| |
|
39 | shellout(cmd) do |io| | |
39 | io.each_line do |line| |
|
40 | io.each_line do |line| | |
40 | branches << line.match('\s*\*?\s*(.*)$')[1] |
|
41 | @branches << line.match('\s*\*?\s*(.*)$')[1] | |
41 | end |
|
42 | end | |
42 | end |
|
43 | end | |
43 | branches.sort! |
|
44 | @branches.sort! | |
44 | end |
|
45 | end | |
45 |
|
46 | |||
46 | def tags |
|
47 | def tags | |
47 |
|
|
48 | return @tags if @tags | |
48 | cmd = "#{GIT_BIN} --git-dir #{target('')} tag" |
|
49 | cmd = "#{GIT_BIN} --git-dir #{target('')} tag" | |
49 | shellout(cmd) do |io| |
|
50 | shellout(cmd) do |io| | |
50 | io.readlines.sort!.map{|t| t.strip} |
|
51 | @tags = io.readlines.sort!.map{|t| t.strip} | |
51 | end |
|
52 | end | |
52 | end |
|
53 | end | |
53 |
|
54 | |||
54 | def default_branch |
|
55 | def default_branch | |
55 | branches.include?('master') ? 'master' : branches.first |
|
56 | branches.include?('master') ? 'master' : branches.first | |
56 | end |
|
57 | end | |
57 |
|
58 | |||
58 | def entries(path=nil, identifier=nil) |
|
59 | def entries(path=nil, identifier=nil) | |
59 | path ||= '' |
|
60 | path ||= '' | |
60 | entries = Entries.new |
|
61 | entries = Entries.new | |
61 | cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l " |
|
62 | cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l " | |
62 | cmd << shell_quote("HEAD:" + path) if identifier.nil? |
|
63 | cmd << shell_quote("HEAD:" + path) if identifier.nil? | |
63 | cmd << shell_quote(identifier + ":" + path) if identifier |
|
64 | cmd << shell_quote(identifier + ":" + path) if identifier | |
64 | shellout(cmd) do |io| |
|
65 | shellout(cmd) do |io| | |
65 | io.each_line do |line| |
|
66 | io.each_line do |line| | |
66 | e = line.chomp.to_s |
|
67 | e = line.chomp.to_s | |
67 | if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/ |
|
68 | if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/ | |
68 | type = $1 |
|
69 | type = $1 | |
69 | sha = $2 |
|
70 | sha = $2 | |
70 | size = $3 |
|
71 | size = $3 | |
71 | name = $4 |
|
72 | name = $4 | |
72 | full_path = path.empty? ? name : "#{path}/#{name}" |
|
73 | full_path = path.empty? ? name : "#{path}/#{name}" | |
73 | entries << Entry.new({:name => name, |
|
74 | entries << Entry.new({:name => name, | |
74 | :path => full_path, |
|
75 | :path => full_path, | |
75 | :kind => (type == "tree") ? 'dir' : 'file', |
|
76 | :kind => (type == "tree") ? 'dir' : 'file', | |
76 | :size => (type == "tree") ? nil : size, |
|
77 | :size => (type == "tree") ? nil : size, | |
77 | :lastrev => lastrev(full_path,identifier) |
|
78 | :lastrev => lastrev(full_path,identifier) | |
78 | }) unless entries.detect{|entry| entry.name == name} |
|
79 | }) unless entries.detect{|entry| entry.name == name} | |
79 | end |
|
80 | end | |
80 | end |
|
81 | end | |
81 | end |
|
82 | end | |
82 | return nil if $? && $?.exitstatus != 0 |
|
83 | return nil if $? && $?.exitstatus != 0 | |
83 | entries.sort_by_name |
|
84 | entries.sort_by_name | |
84 | end |
|
85 | end | |
85 |
|
86 | |||
86 | def lastrev(path,rev) |
|
87 | def lastrev(path,rev) | |
87 | return nil if path.nil? |
|
88 | return nil if path.nil? | |
88 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=fuller --no-merges -n 1 " |
|
89 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=fuller --no-merges -n 1 " | |
89 | cmd << " #{shell_quote rev} " if rev |
|
90 | cmd << " #{shell_quote rev} " if rev | |
90 | cmd << "-- #{path} " unless path.empty? |
|
91 | cmd << "-- #{path} " unless path.empty? | |
91 | shellout(cmd) do |io| |
|
92 | shellout(cmd) do |io| | |
92 | begin |
|
93 | begin | |
93 | id = io.gets.split[1] |
|
94 | id = io.gets.split[1] | |
94 | author = io.gets.match('Author:\s+(.*)$')[1] |
|
95 | author = io.gets.match('Author:\s+(.*)$')[1] | |
95 | 2.times { io.gets } |
|
96 | 2.times { io.gets } | |
96 | time = io.gets.match('CommitDate:\s+(.*)$')[1] |
|
97 | time = io.gets.match('CommitDate:\s+(.*)$')[1] | |
97 |
|
98 | |||
98 | Revision.new({ |
|
99 | Revision.new({ | |
99 | :identifier => id, |
|
100 | :identifier => id, | |
100 | :scmid => id, |
|
101 | :scmid => id, | |
101 | :author => author, |
|
102 | :author => author, | |
102 | :time => time, |
|
103 | :time => time, | |
103 | :message => nil, |
|
104 | :message => nil, | |
104 | :paths => nil |
|
105 | :paths => nil | |
105 | }) |
|
106 | }) | |
106 | rescue NoMethodError => e |
|
107 | rescue NoMethodError => e | |
107 | logger.error("The revision '#{path}' has a wrong format") |
|
108 | logger.error("The revision '#{path}' has a wrong format") | |
108 | return nil |
|
109 | return nil | |
109 | end |
|
110 | end | |
110 | end |
|
111 | end | |
111 | end |
|
112 | end | |
112 |
|
113 | |||
113 | def revisions(path, identifier_from, identifier_to, options={}) |
|
114 | def revisions(path, identifier_from, identifier_to, options={}) | |
114 | revisions = Revisions.new |
|
115 | revisions = Revisions.new | |
115 |
|
116 | |||
116 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --find-copies-harder --raw --date=iso --pretty=fuller" |
|
117 | cmd = "#{GIT_BIN} --git-dir #{target('')} log --find-copies-harder --raw --date=iso --pretty=fuller" | |
117 | cmd << " --reverse" if options[:reverse] |
|
118 | cmd << " --reverse" if options[:reverse] | |
118 | cmd << " --all" if options[:all] |
|
119 | cmd << " --all" if options[:all] | |
119 | cmd << " -n #{options[:limit]} " if options[:limit] |
|
120 | cmd << " -n #{options[:limit]} " if options[:limit] | |
120 | cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from |
|
121 | cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from | |
121 | cmd << " #{shell_quote identifier_to} " if identifier_to |
|
122 | cmd << " #{shell_quote identifier_to} " if identifier_to | |
122 | cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since] |
|
123 | cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since] | |
123 | cmd << " -- #{path}" if path && !path.empty? |
|
124 | cmd << " -- #{path}" if path && !path.empty? | |
124 |
|
125 | |||
125 | shellout(cmd) do |io| |
|
126 | shellout(cmd) do |io| | |
126 | files=[] |
|
127 | files=[] | |
127 | changeset = {} |
|
128 | changeset = {} | |
128 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files |
|
129 | parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files | |
129 | revno = 1 |
|
130 | revno = 1 | |
130 |
|
131 | |||
131 | io.each_line do |line| |
|
132 | io.each_line do |line| | |
132 | if line =~ /^commit ([0-9a-f]{40})$/ |
|
133 | if line =~ /^commit ([0-9a-f]{40})$/ | |
133 | key = "commit" |
|
134 | key = "commit" | |
134 | value = $1 |
|
135 | value = $1 | |
135 | if (parsing_descr == 1 || parsing_descr == 2) |
|
136 | if (parsing_descr == 1 || parsing_descr == 2) | |
136 | parsing_descr = 0 |
|
137 | parsing_descr = 0 | |
137 | revision = Revision.new({ |
|
138 | revision = Revision.new({ | |
138 | :identifier => changeset[:commit], |
|
139 | :identifier => changeset[:commit], | |
139 | :scmid => changeset[:commit], |
|
140 | :scmid => changeset[:commit], | |
140 | :author => changeset[:author], |
|
141 | :author => changeset[:author], | |
141 | :time => Time.parse(changeset[:date]), |
|
142 | :time => Time.parse(changeset[:date]), | |
142 | :message => changeset[:description], |
|
143 | :message => changeset[:description], | |
143 | :paths => files |
|
144 | :paths => files | |
144 | }) |
|
145 | }) | |
145 | if block_given? |
|
146 | if block_given? | |
146 | yield revision |
|
147 | yield revision | |
147 | else |
|
148 | else | |
148 | revisions << revision |
|
149 | revisions << revision | |
149 | end |
|
150 | end | |
150 | changeset = {} |
|
151 | changeset = {} | |
151 | files = [] |
|
152 | files = [] | |
152 | revno = revno + 1 |
|
153 | revno = revno + 1 | |
153 | end |
|
154 | end | |
154 | changeset[:commit] = $1 |
|
155 | changeset[:commit] = $1 | |
155 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ |
|
156 | elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ | |
156 | key = $1 |
|
157 | key = $1 | |
157 | value = $2 |
|
158 | value = $2 | |
158 | if key == "Author" |
|
159 | if key == "Author" | |
159 | changeset[:author] = value |
|
160 | changeset[:author] = value | |
160 | elsif key == "CommitDate" |
|
161 | elsif key == "CommitDate" | |
161 | changeset[:date] = value |
|
162 | changeset[:date] = value | |
162 | end |
|
163 | end | |
163 | elsif (parsing_descr == 0) && line.chomp.to_s == "" |
|
164 | elsif (parsing_descr == 0) && line.chomp.to_s == "" | |
164 | parsing_descr = 1 |
|
165 | parsing_descr = 1 | |
165 | changeset[:description] = "" |
|
166 | changeset[:description] = "" | |
166 | elsif (parsing_descr == 1 || parsing_descr == 2) \ |
|
167 | elsif (parsing_descr == 1 || parsing_descr == 2) \ | |
167 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ |
|
168 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/ | |
168 | parsing_descr = 2 |
|
169 | parsing_descr = 2 | |
169 | fileaction = $1 |
|
170 | fileaction = $1 | |
170 | filepath = $2 |
|
171 | filepath = $2 | |
171 | files << {:action => fileaction, :path => filepath} |
|
172 | files << {:action => fileaction, :path => filepath} | |
172 | elsif (parsing_descr == 1 || parsing_descr == 2) \ |
|
173 | elsif (parsing_descr == 1 || parsing_descr == 2) \ | |
173 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\s+(.+)$/ |
|
174 | && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\s+(.+)$/ | |
174 | parsing_descr = 2 |
|
175 | parsing_descr = 2 | |
175 | fileaction = $1 |
|
176 | fileaction = $1 | |
176 | filepath = $3 |
|
177 | filepath = $3 | |
177 | files << {:action => fileaction, :path => filepath} |
|
178 | files << {:action => fileaction, :path => filepath} | |
178 | elsif (parsing_descr == 1) && line.chomp.to_s == "" |
|
179 | elsif (parsing_descr == 1) && line.chomp.to_s == "" | |
179 | parsing_descr = 2 |
|
180 | parsing_descr = 2 | |
180 | elsif (parsing_descr == 1) |
|
181 | elsif (parsing_descr == 1) | |
181 | changeset[:description] << line[4..-1] |
|
182 | changeset[:description] << line[4..-1] | |
182 | end |
|
183 | end | |
183 | end |
|
184 | end | |
184 |
|
185 | |||
185 | if changeset[:commit] |
|
186 | if changeset[:commit] | |
186 | revision = Revision.new({ |
|
187 | revision = Revision.new({ | |
187 | :identifier => changeset[:commit], |
|
188 | :identifier => changeset[:commit], | |
188 | :scmid => changeset[:commit], |
|
189 | :scmid => changeset[:commit], | |
189 | :author => changeset[:author], |
|
190 | :author => changeset[:author], | |
190 | :time => Time.parse(changeset[:date]), |
|
191 | :time => Time.parse(changeset[:date]), | |
191 | :message => changeset[:description], |
|
192 | :message => changeset[:description], | |
192 | :paths => files |
|
193 | :paths => files | |
193 | }) |
|
194 | }) | |
194 |
|
195 | |||
195 | if block_given? |
|
196 | if block_given? | |
196 | yield revision |
|
197 | yield revision | |
197 | else |
|
198 | else | |
198 | revisions << revision |
|
199 | revisions << revision | |
199 | end |
|
200 | end | |
200 | end |
|
201 | end | |
201 | end |
|
202 | end | |
202 |
|
203 | |||
203 | return nil if $? && $?.exitstatus != 0 |
|
204 | return nil if $? && $?.exitstatus != 0 | |
204 | revisions |
|
205 | revisions | |
205 | end |
|
206 | end | |
206 |
|
207 | |||
207 | def diff(path, identifier_from, identifier_to=nil) |
|
208 | def diff(path, identifier_from, identifier_to=nil) | |
208 | path ||= '' |
|
209 | path ||= '' | |
209 |
|
210 | |||
210 | if identifier_to |
|
211 | if identifier_to | |
211 | cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" |
|
212 | cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" | |
212 | else |
|
213 | else | |
213 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" |
|
214 | cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" | |
214 | end |
|
215 | end | |
215 |
|
216 | |||
216 | cmd << " -- #{shell_quote path}" unless path.empty? |
|
217 | cmd << " -- #{shell_quote path}" unless path.empty? | |
217 | diff = [] |
|
218 | diff = [] | |
218 | shellout(cmd) do |io| |
|
219 | shellout(cmd) do |io| | |
219 | io.each_line do |line| |
|
220 | io.each_line do |line| | |
220 | diff << line |
|
221 | diff << line | |
221 | end |
|
222 | end | |
222 | end |
|
223 | end | |
223 | return nil if $? && $?.exitstatus != 0 |
|
224 | return nil if $? && $?.exitstatus != 0 | |
224 | diff |
|
225 | diff | |
225 | end |
|
226 | end | |
226 |
|
227 | |||
227 | def annotate(path, identifier=nil) |
|
228 | def annotate(path, identifier=nil) | |
228 | identifier = 'HEAD' if identifier.blank? |
|
229 | identifier = 'HEAD' if identifier.blank? | |
229 | cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}" |
|
230 | cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}" | |
230 | blame = Annotate.new |
|
231 | blame = Annotate.new | |
231 | content = nil |
|
232 | content = nil | |
232 | shellout(cmd) { |io| io.binmode; content = io.read } |
|
233 | shellout(cmd) { |io| io.binmode; content = io.read } | |
233 | return nil if $? && $?.exitstatus != 0 |
|
234 | return nil if $? && $?.exitstatus != 0 | |
234 | # git annotates binary files |
|
235 | # git annotates binary files | |
235 | return nil if content.is_binary_data? |
|
236 | return nil if content.is_binary_data? | |
236 | content.split("\n").each do |line| |
|
237 | content.split("\n").each do |line| | |
237 | next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)/ |
|
238 | next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)/ | |
238 | blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip)) |
|
239 | blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip)) | |
239 | end |
|
240 | end | |
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 | end |
|
260 | end |
General Comments 0
You need to be logged in to leave comments.
Login now