1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 require 'redmine/scm/adapters/abstract_adapter'
23 class GitAdapter < AbstractAdapter
25 SCM_GIT_REPORT_LAST_COMMIT = true
28 GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
30 # raised if scm command exited with error, e.g. unknown revision.
31 class ScmCommandAborted < CommandFailed; end
39 @@sq_bin ||= shell_quote(GIT_BIN)
43 @@client_version ||= (scm_command_version || [])
47 !client_version.empty?
50 def scm_command_version
51 scm_version = scm_version_from_command_line.dup
52 if scm_version.respond_to?(:force_encoding)
53 scm_version.force_encoding('ASCII-8BIT')
55 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
56 m[2].scan(%r{\d+}).collect(&:to_i)
60 def scm_version_from_command_line
61 shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s
65 def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil)
67 @path_encoding = path_encoding || 'UTF-8'
68 @flag_report_last_commit = SCM_GIT_REPORT_LAST_COMMIT
73 Info.new(:root_url => url, :lastrev => lastrev('',nil))
80 return @branches if @branches
82 cmd_args = %w|branch --no-color|
83 scm_cmd(*cmd_args) do |io|
84 io.each_line do |line|
85 @branches << line.match('\s*\*?\s*(.*)$')[1]
89 rescue ScmCommandAborted
96 scm_cmd(*cmd_args) do |io|
97 @tags = io.readlines.sort!.map{|t| t.strip}
99 rescue ScmCommandAborted
105 return nil if bras.nil?
106 bras.include?('master') ? 'master' : bras.first
109 def entries(path=nil, identifier=nil)
111 p = scm_iconv(@path_encoding, 'UTF-8', path)
112 entries = Entries.new
113 cmd_args = %w|ls-tree -l|
114 cmd_args << "HEAD:#{p}" if identifier.nil?
115 cmd_args << "#{identifier}:#{p}" if identifier
116 scm_cmd(*cmd_args) do |io|
117 io.each_line do |line|
119 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
124 if name.respond_to?(:force_encoding)
125 name.force_encoding(@path_encoding)
127 full_path = p.empty? ? name : "#{p}/#{name}"
128 n = scm_iconv('UTF-8', @path_encoding, name)
129 full_p = scm_iconv('UTF-8', @path_encoding, full_path)
130 entries << Entry.new({:name => n,
132 :kind => (type == "tree") ? 'dir' : 'file',
133 :size => (type == "tree") ? nil : size,
134 :lastrev => @flag_report_last_commit ? lastrev(full_path, identifier) : Revision.new
135 }) unless entries.detect{|entry| entry.name == name}
140 rescue ScmCommandAborted
144 def lastrev(path, rev)
145 return nil if path.nil?
146 cmd_args = %w|log --no-decorate --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
147 cmd_args << rev if rev
148 cmd_args << "--" << path unless path.empty?
150 scm_cmd(*cmd_args) { |io| lines = io.readlines }
152 id = lines[0].split[1]
153 author = lines[1].match('Author:\s+(.*)$')[1]
154 time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
164 rescue NoMethodError => e
165 logger.error("The revision '#{path}' has a wrong format")
168 rescue ScmCommandAborted
172 def revisions(path, identifier_from, identifier_to, options={})
173 revisions = Revisions.new
174 cmd_args = %w|log --no-decorate --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller|
175 cmd_args << "--reverse" if options[:reverse]
176 cmd_args << "--all" if options[:all]
177 cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
179 from_to << "#{identifier_from}.." if identifier_from
180 from_to << "#{identifier_to}" if identifier_to
181 cmd_args << from_to if !from_to.empty?
182 cmd_args << "--since=#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}" if options[:since]
183 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
185 scm_cmd *cmd_args do |io|
188 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
190 io.each_line do |line|
191 if line =~ /^commit ([0-9a-f]{40})$/
194 if (parsing_descr == 1 || parsing_descr == 2)
196 revision = Revision.new({
197 :identifier => changeset[:commit],
198 :scmid => changeset[:commit],
199 :author => changeset[:author],
200 :time => Time.parse(changeset[:date]),
201 :message => changeset[:description],
207 revisions << revision
212 changeset[:commit] = $1
213 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
217 changeset[:author] = value
218 elsif key == "CommitDate"
219 changeset[:date] = value
221 elsif (parsing_descr == 0) && line.chomp.to_s == ""
223 changeset[:description] = ""
224 elsif (parsing_descr == 1 || parsing_descr == 2) \
225 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
229 p = scm_iconv('UTF-8', @path_encoding, filepath)
230 files << {:action => fileaction, :path => p}
231 elsif (parsing_descr == 1 || parsing_descr == 2) \
232 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
236 p = scm_iconv('UTF-8', @path_encoding, filepath)
237 files << {:action => fileaction, :path => p}
238 elsif (parsing_descr == 1) && line.chomp.to_s == ""
240 elsif (parsing_descr == 1)
241 changeset[:description] << line[4..-1]
245 if changeset[:commit]
246 revision = Revision.new({
247 :identifier => changeset[:commit],
248 :scmid => changeset[:commit],
249 :author => changeset[:author],
250 :time => Time.parse(changeset[:date]),
251 :message => changeset[:description],
258 revisions << revision
263 rescue ScmCommandAborted
267 def diff(path, identifier_from, identifier_to=nil)
271 cmd_args << "diff" << "--no-color" << identifier_to << identifier_from
273 cmd_args << "show" << "--no-color" << identifier_from
275 cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
277 scm_cmd *cmd_args do |io|
278 io.each_line do |line|
283 rescue ScmCommandAborted
287 def annotate(path, identifier=nil)
288 identifier = 'HEAD' if identifier.blank?
290 cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path)
293 scm_cmd(*cmd_args) { |io| io.binmode; content = io.read }
294 # git annotates binary files
295 return nil if content.is_binary_data?
297 # git shows commit author on the first occurrence only
298 authors_by_commit = {}
299 content.split("\n").each do |line|
300 if line =~ /^([0-9a-f]{39,40})\s.*/
302 elsif line =~ /^author (.+)/
303 authors_by_commit[identifier] = $1.strip
304 elsif line =~ /^\t(.*)/
305 blame.add_line($1, Revision.new(
306 :identifier => identifier,
307 :author => authors_by_commit[identifier]))
313 rescue ScmCommandAborted
317 def cat(path, identifier=nil)
321 cmd_args = %w|show --no-color|
322 cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
324 scm_cmd(*cmd_args) do |io|
329 rescue ScmCommandAborted
333 class Revision < Redmine::Scm::Adapters::Revision
334 # Returns the readable identifier
335 def format_identifier
340 def scm_cmd(*args, &block)
341 repo_path = root_url || url
342 full_args = [GIT_BIN, '--git-dir', repo_path]
343 if self.class.client_version_above?([1, 7, 2])
344 full_args << '-c' << 'core.quotepath=false'
347 ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
348 if $? && $?.exitstatus != 0
349 raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"