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