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
29 Info.new(:root_url => url, :lastrev => lastrev('',nil))
36 return @branches if @branches
38 cmd = "#{GIT_BIN} --git-dir #{target('')} branch --no-color"
40 io.each_line do |line|
41 @branches << line.match('\s*\*?\s*(.*)$')[1]
49 cmd = "#{GIT_BIN} --git-dir #{target('')} tag"
51 @tags = io.readlines.sort!.map{|t| t.strip}
56 branches.include?('master') ? 'master' : branches.first
59 def entries(path=nil, identifier=nil)
62 cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l "
63 cmd << shell_quote("HEAD:" + path) if identifier.nil?
64 cmd << shell_quote(identifier + ":" + path) if identifier
66 io.each_line do |line|
68 if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
73 full_path = path.empty? ? name : "#{path}/#{name}"
74 entries << Entry.new({:name => name,
76 :kind => (type == "tree") ? 'dir' : 'file',
77 :size => (type == "tree") ? nil : size,
78 :lastrev => lastrev(full_path,identifier)
79 }) unless entries.detect{|entry| entry.name == name}
83 return nil if $? && $?.exitstatus != 0
88 return nil if path.nil?
89 cmd = "#{GIT_BIN} --git-dir #{target('')} log --no-color --date=iso --pretty=fuller --no-merges -n 1 "
90 cmd << " #{shell_quote rev} " if rev
91 cmd << "-- #{shell_quote path} " unless path.empty?
95 author = io.gets.match('Author:\s+(.*)$')[1]
97 time = Time.parse(io.gets.match('CommitDate:\s+(.*)$')[1]).localtime
107 rescue NoMethodError => e
108 logger.error("The revision '#{path}' has a wrong format")
114 def revisions(path, identifier_from, identifier_to, options={})
115 revisions = Revisions.new
117 cmd = "#{GIT_BIN} --git-dir #{target('')} log --no-color --raw --date=iso --pretty=fuller "
118 cmd << " --reverse " if options[:reverse]
119 cmd << " --all " if options[:all]
120 cmd << " -n #{options[:limit]} " if options[:limit]
121 cmd << "#{shell_quote(identifier_from + '..')}" if identifier_from
122 cmd << "#{shell_quote identifier_to}" if identifier_to
123 cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since]
124 cmd << " -- #{shell_quote path}" if path && !path.empty?
126 shellout(cmd) do |io|
129 parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
132 io.each_line do |line|
133 if line =~ /^commit ([0-9a-f]{40})$/
136 if (parsing_descr == 1 || parsing_descr == 2)
138 revision = Revision.new({
139 :identifier => changeset[:commit],
140 :scmid => changeset[:commit],
141 :author => changeset[:author],
142 :time => Time.parse(changeset[:date]),
143 :message => changeset[:description],
149 revisions << revision
155 changeset[:commit] = $1
156 elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/
160 changeset[:author] = value
161 elsif key == "CommitDate"
162 changeset[:date] = value
164 elsif (parsing_descr == 0) && line.chomp.to_s == ""
166 changeset[:description] = ""
167 elsif (parsing_descr == 1 || parsing_descr == 2) \
168 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/
172 files << {:action => fileaction, :path => filepath}
173 elsif (parsing_descr == 1 || parsing_descr == 2) \
174 && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/
178 files << {:action => fileaction, :path => filepath}
179 elsif (parsing_descr == 1) && line.chomp.to_s == ""
181 elsif (parsing_descr == 1)
182 changeset[:description] << line[4..-1]
186 if changeset[:commit]
187 revision = Revision.new({
188 :identifier => changeset[:commit],
189 :scmid => changeset[:commit],
190 :author => changeset[:author],
191 :time => Time.parse(changeset[:date]),
192 :message => changeset[:description],
199 revisions << revision
204 return nil if $? && $?.exitstatus != 0
208 def diff(path, identifier_from, identifier_to=nil)
212 cmd = "#{GIT_BIN} --git-dir #{target('')} diff --no-color #{shell_quote identifier_to} #{shell_quote identifier_from}"
214 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote identifier_from}"
217 cmd << " -- #{shell_quote path}" unless path.empty?
219 shellout(cmd) do |io|
220 io.each_line do |line|
224 return nil if $? && $?.exitstatus != 0
228 def annotate(path, identifier=nil)
229 identifier = 'HEAD' if identifier.blank?
230 cmd = "#{GIT_BIN} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}"
233 shellout(cmd) { |io| io.binmode; content = io.read }
234 return nil if $? && $?.exitstatus != 0
235 # git annotates binary files
236 return nil if content.is_binary_data?
238 # git shows commit author on the first occurrence only
239 authors_by_commit = {}
240 content.split("\n").each do |line|
241 if line =~ /^([0-9a-f]{39,40})\s.*/
243 elsif line =~ /^author (.+)/
244 authors_by_commit[identifier] = $1.strip
245 elsif line =~ /^\t(.*)/
246 blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier]))
254 def cat(path, identifier=nil)
258 cmd = "#{GIT_BIN} --git-dir #{target('')} show #{shell_quote(identifier + ':' + path)}"
260 shellout(cmd) do |io|
264 return nil if $? && $?.exitstatus != 0