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