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 GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
33 @@sq_bin ||= shell_quote(GIT_BIN)
37 @@client_version ||= (scm_command_version || [])
41 !client_version.empty?
44 def scm_command_version
45 scm_version = scm_version_from_command_line
46 if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)})
47 m[2].scan(%r{\d+}).collect(&:to_i)
51 def scm_version_from_command_line
52 shellout("#{sq_bin} --version") { |io| io.read }.to_s
58 Info.new(:root_url => url, :lastrev => lastrev('',nil))
65 return @branches if @branches
67 cmd = "#{self.class.sq_bin} --git-dir #{target('')} branch --no-color"
69 io.each_line do |line|
70 @branches << line.match('\s*\*?\s*(.*)$')[1]
78 cmd = "#{self.class.sq_bin} --git-dir #{target('')} tag"
80 @tags = io.readlines.sort!.map{|t| t.strip}
85 branches.include?('master') ? 'master' : branches.first
88 def entries(path=nil, identifier=nil)
91 cmd = "#{self.class.sq_bin} --git-dir #{target('')} ls-tree -l "
92 cmd << shell_quote("HEAD:" + path) if identifier.nil?
93 cmd << shell_quote(identifier + ":" + path) if identifier
95 io.each_line do |line|
97 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
102 full_path = path.empty? ? name : "#{path}/#{name}"
103 entries << Entry.new({:name => name,
105 :kind => (type == "tree") ? 'dir' : 'file',
106 :size => (type == "tree") ? nil : size,
107 :lastrev => lastrev(full_path,identifier)
108 }) unless entries.detect{|entry| entry.name == name}
112 return nil if $? && $?.exitstatus != 0
116 def lastrev(path,rev)
117 return nil if path.nil?
118 cmd = "#{self.class.sq_bin} --git-dir #{target('')} log --no-color --date=iso --pretty=fuller --no-merges -n 1 "
119 cmd << " #{shell_quote rev} " if rev
120 cmd << "-- #{shell_quote path} " unless path.empty?
122 shellout(cmd) { |io| lines = io.readlines }
123 return nil if $? && $?.exitstatus != 0
125 id = lines[0].split[1]
126 author = lines[1].match('Author:\s+(.*)$')[1]
127 time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1])
137 rescue NoMethodError => e
138 logger.error("The revision '#{path}' has a wrong format")
143 def revisions(path, identifier_from, identifier_to, options={})
144 revisions = Revisions.new
146 cmd = "#{self.class.sq_bin} --git-dir #{target('')} log --no-color --raw --date=iso --pretty=fuller "
147 cmd << " --reverse " if options[:reverse]
148 cmd << " --all " if options[:all]
149 cmd << " -n #{options[:limit].to_i} " if options[:limit]
150 cmd << "#{shell_quote(identifier_from + '..')}" if identifier_from
151 cmd << "#{shell_quote identifier_to}" if identifier_to
152 cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since]
153 cmd << " -- #{shell_quote path}" if path && !path.empty?
155 shellout(cmd) do |io|
158 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
161 io.each_line do |line|
162 if line =~ /^commit ([0-9a-f]{40})$/
165 if (parsing_descr == 1 || parsing_descr == 2)
167 revision = Revision.new({
168 :identifier => changeset[:commit],
169 :scmid => changeset[:commit],
170 :author => changeset[:author],
171 :time => Time.parse(changeset[:date]),
172 :message => changeset[:description],
178 revisions << revision
184 changeset[:commit] = $1
185 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
189 changeset[:author] = value
190 elsif key == "CommitDate"
191 changeset[:date] = value
193 elsif (parsing_descr == 0) && line.chomp.to_s == ""
195 changeset[:description] = ""
196 elsif (parsing_descr == 1 || parsing_descr == 2) \
197 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
201 files << {:action => fileaction, :path => filepath}
202 elsif (parsing_descr == 1 || parsing_descr == 2) \
203 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
207 files << {:action => fileaction, :path => filepath}
208 elsif (parsing_descr == 1) && line.chomp.to_s == ""
210 elsif (parsing_descr == 1)
211 changeset[:description] << line[4..-1]
215 if changeset[:commit]
216 revision = Revision.new({
217 :identifier => changeset[:commit],
218 :scmid => changeset[:commit],
219 :author => changeset[:author],
220 :time => Time.parse(changeset[:date]),
221 :message => changeset[:description],
228 revisions << revision
233 return nil if $? && $?.exitstatus != 0
237 def diff(path, identifier_from, identifier_to=nil)
241 cmd = "#{self.class.sq_bin} --git-dir #{target('')} diff --no-color #{shell_quote identifier_to} #{shell_quote identifier_from}"
243 cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote identifier_from}"
246 cmd << " -- #{shell_quote path}" unless path.empty?
248 shellout(cmd) do |io|
249 io.each_line do |line|
253 return nil if $? && $?.exitstatus != 0
257 def annotate(path, identifier=nil)
258 identifier = 'HEAD' if identifier.blank?
259 cmd = "#{self.class.sq_bin} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}"
262 shellout(cmd) { |io| io.binmode; content = io.read }
263 return nil if $? && $?.exitstatus != 0
264 # git annotates binary files
265 return nil if content.is_binary_data?
267 # git shows commit author on the first occurrence only
268 authors_by_commit = {}
269 content.split("\n").each do |line|
270 if line =~ /^([0-9a-f]{39,40})\s.*/
272 elsif line =~ /^author (.+)/
273 authors_by_commit[identifier] = $1.strip
274 elsif line =~ /^\t(.*)/
275 blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier]))
283 def cat(path, identifier=nil)
287 cmd = "#{self.class.sq_bin} --git-dir #{target('')} show --no-color #{shell_quote(identifier + ':' + path)}"
289 shellout(cmd) do |io|
293 return nil if $? && $?.exitstatus != 0
297 class Revision < Redmine::Scm::Adapters::Revision
298 # Returns the readable identifier
299 def format_identifier