##// END OF EJS Templates
Fixed: Redmine::Scm::Adapters::GitAdapter#get_rev ignored GIT_BIN constant (#1207)....
Jean-Philippe Lang -
r1413:835cc7ed7c6b
parent child
Show More
@@ -1,256 +1,256
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 --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
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 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 => (changeset[:date] ? Time.parse(changeset[:date]) : nil),
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 97 revs = revisions(url,nil,nil,{:limit => 1})
98 98 if revs && revs.any?
99 99 Info.new(:root_url => url, :lastrev => revs.first)
100 100 else
101 101 nil
102 102 end
103 103 rescue Errno::ENOENT => e
104 104 return nil
105 105 end
106 106
107 107 def entries(path=nil, identifier=nil)
108 108 path ||= ''
109 109 entries = Entries.new
110 110 cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
111 111 cmd << shell_quote("HEAD:" + path) if identifier.nil?
112 112 cmd << shell_quote(identifier + ":" + path) if identifier
113 113 shellout(cmd) do |io|
114 114 io.each_line do |line|
115 115 e = line.chomp.to_s
116 116 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\s+(.+)$/
117 117 type = $1
118 118 sha = $2
119 119 size = $3
120 120 name = $4
121 121 entries << Entry.new({:name => name,
122 122 :path => (path.empty? ? name : "#{path}/#{name}"),
123 123 :kind => ((type == "tree") ? 'dir' : 'file'),
124 124 :size => ((type == "tree") ? nil : size),
125 125 :lastrev => get_rev(identifier,(path.empty? ? name : "#{path}/#{name}"))
126 126
127 127 }) unless entries.detect{|entry| entry.name == name}
128 128 end
129 129 end
130 130 end
131 131 return nil if $? && $?.exitstatus != 0
132 132 entries.sort_by_name
133 133 end
134 134
135 135 def revisions(path, identifier_from, identifier_to, options={})
136 136 revisions = Revisions.new
137 137 cmd = "#{GIT_BIN} --git-dir #{target('')} log --raw "
138 138 cmd << " -n #{options[:limit].to_i} " if (!options.nil?) && options[:limit]
139 139 cmd << " #{shell_quote(identifier_from + '..')} " if identifier_from
140 140 cmd << " #{shell_quote identifier_to} " if identifier_to
141 141 #cmd << " HEAD " if !identifier_to
142 142 shellout(cmd) do |io|
143 143 files=[]
144 144 changeset = {}
145 145 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
146 146 revno = 1
147 147
148 148 io.each_line do |line|
149 149 if line =~ /^commit ([0-9a-f]{40})$/
150 150 key = "commit"
151 151 value = $1
152 152 if (parsing_descr == 1 || parsing_descr == 2)
153 153 parsing_descr = 0
154 154 revisions << Revision.new({:identifier => changeset[:commit],
155 155 :scmid => changeset[:commit],
156 156 :author => changeset[:author],
157 157 :time => Time.parse(changeset[:date]),
158 158 :message => changeset[:description],
159 159 :paths => files
160 160 })
161 161 changeset = {}
162 162 files = []
163 163 revno = revno + 1
164 164 end
165 165 changeset[:commit] = $1
166 166 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
167 167 key = $1
168 168 value = $2
169 169 if key == "Author"
170 170 changeset[:author] = value
171 171 elsif key == "Date"
172 172 changeset[:date] = value
173 173 end
174 174 elsif (parsing_descr == 0) && line.chomp.to_s == ""
175 175 parsing_descr = 1
176 176 changeset[:description] = ""
177 177 elsif (parsing_descr == 1 || parsing_descr == 2) && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\s+(.+)$/
178 178 parsing_descr = 2
179 179 fileaction = $1
180 180 filepath = $2
181 181 files << {:action => fileaction, :path => filepath}
182 182 elsif (parsing_descr == 1) && line.chomp.to_s == ""
183 183 parsing_descr = 2
184 184 elsif (parsing_descr == 1)
185 185 changeset[:description] << line[4..-1]
186 186 end
187 187 end
188 188
189 189 revisions << Revision.new({:identifier => changeset[:commit],
190 190 :scmid => changeset[:commit],
191 191 :author => changeset[:author],
192 192 :time => Time.parse(changeset[:date]),
193 193 :message => changeset[:description],
194 194 :paths => files
195 195 }) if changeset[:commit]
196 196
197 197 end
198 198
199 199 return nil if $? && $?.exitstatus != 0
200 200 revisions
201 201 end
202 202
203 203 def diff(path, identifier_from, identifier_to=nil, type="inline")
204 204 path ||= ''
205 205 if !identifier_to
206 206 identifier_to = nil
207 207 end
208 208
209 209 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}" if identifier_to.nil?
210 210 cmd = "#{GIT_BIN} --git-dir #{target('')} diff #{shell_quote identifier_to} #{shell_quote identifier_from}" if !identifier_to.nil?
211 211 cmd << " -- #{shell_quote path}" unless path.empty?
212 212 diff = []
213 213 shellout(cmd) do |io|
214 214 io.each_line do |line|
215 215 diff << line
216 216 end
217 217 end
218 218 return nil if $? && $?.exitstatus != 0
219 219 DiffTableList.new diff, type
220 220 end
221 221
222 222 def annotate(path, identifier=nil)
223 223 identifier = 'HEAD' if identifier.blank?
224 224 cmd = "#{GIT_BIN} --git-dir #{target('')} blame -l #{shell_quote identifier} -- #{shell_quote path}"
225 225 blame = Annotate.new
226 226 content = nil
227 227 shellout(cmd) { |io| io.binmode; content = io.read }
228 228 return nil if $? && $?.exitstatus != 0
229 229 # git annotates binary files
230 230 return nil if content.is_binary_data?
231 231 content.split("\n").each do |line|
232 232 next unless line =~ /([0-9a-f]{39,40})\s\((\w*)[^\)]*\)(.*)/
233 233 blame.add_line($3.rstrip, Revision.new(:identifier => $1, :author => $2.strip))
234 234 end
235 235 blame
236 236 end
237 237
238 238 def cat(path, identifier=nil)
239 239 if identifier.nil?
240 240 identifier = 'HEAD'
241 241 end
242 242 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
243 243 cat = nil
244 244 shellout(cmd) do |io|
245 245 io.binmode
246 246 cat = io.read
247 247 end
248 248 return nil if $? && $?.exitstatus != 0
249 249 cat
250 250 end
251 251 end
252 252 end
253 253 end
254 254
255 255 end
256 256
General Comments 0
You need to be logged in to leave comments. Login now