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