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