Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: require 'redmine/scm/adapters/abstract_adapter' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@0: module Adapters Chris@0: class GitAdapter < AbstractAdapter Chris@0: # Git executable name Chris@0: GIT_BIN = "git" Chris@0: Chris@0: def info Chris@0: begin Chris@0: Info.new(:root_url => url, :lastrev => lastrev('',nil)) Chris@0: rescue Chris@0: nil Chris@0: end Chris@0: end Chris@0: Chris@0: def branches Chris@0: return @branches if @branches Chris@0: @branches = [] chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} branch --no-color" Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: @branches << line.match('\s*\*?\s*(.*)$')[1] Chris@0: end Chris@0: end Chris@0: @branches.sort! Chris@0: end Chris@0: Chris@0: def tags Chris@0: return @tags if @tags Chris@0: cmd = "#{GIT_BIN} --git-dir #{target('')} tag" Chris@0: shellout(cmd) do |io| Chris@0: @tags = io.readlines.sort!.map{|t| t.strip} Chris@0: end Chris@0: end Chris@0: Chris@0: def default_branch Chris@0: branches.include?('master') ? 'master' : branches.first Chris@0: end Chris@0: Chris@0: def entries(path=nil, identifier=nil) Chris@0: path ||= '' Chris@0: entries = Entries.new Chris@0: cmd = "#{GIT_BIN} --git-dir #{target('')} ls-tree -l " Chris@0: cmd << shell_quote("HEAD:" + path) if identifier.nil? Chris@0: cmd << shell_quote(identifier + ":" + path) if identifier Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: e = line.chomp.to_s chris@37: if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/ Chris@0: type = $1 Chris@0: sha = $2 Chris@0: size = $3 Chris@0: name = $4 Chris@0: full_path = path.empty? ? name : "#{path}/#{name}" Chris@0: entries << Entry.new({:name => name, Chris@0: :path => full_path, Chris@0: :kind => (type == "tree") ? 'dir' : 'file', Chris@0: :size => (type == "tree") ? nil : size, Chris@0: :lastrev => lastrev(full_path,identifier) Chris@0: }) unless entries.detect{|entry| entry.name == name} Chris@0: end Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: entries.sort_by_name Chris@0: end Chris@0: Chris@0: def lastrev(path,rev) Chris@0: return nil if path.nil? chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} log --no-color --date=iso --pretty=fuller --no-merges -n 1 " Chris@0: cmd << " #{shell_quote rev} " if rev chris@37: cmd << "-- #{shell_quote path} " unless path.empty? Chris@117: lines = [] Chris@117: shellout(cmd) { |io| lines = io.readlines } Chris@117: return nil if $? && $?.exitstatus != 0 Chris@117: begin Chris@117: id = lines[0].split[1] Chris@117: author = lines[1].match('Author:\s+(.*)$')[1] Chris@117: time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1]).localtime Chris@0: Chris@0: Revision.new({ Chris@0: :identifier => id, Chris@0: :scmid => id, Chris@0: :author => author, Chris@0: :time => time, Chris@0: :message => nil, Chris@0: :paths => nil Chris@0: }) Chris@117: rescue NoMethodError => e Chris@0: logger.error("The revision '#{path}' has a wrong format") Chris@0: return nil Chris@0: end Chris@0: end Chris@0: Chris@0: def revisions(path, identifier_from, identifier_to, options={}) Chris@0: revisions = Revisions.new Chris@0: chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} log --no-color --raw --date=iso --pretty=fuller " Chris@14: cmd << " --reverse " if options[:reverse] Chris@14: cmd << " --all " if options[:all] Chris@117: cmd << " -n #{options[:limit].to_i} " if options[:limit] Chris@14: cmd << "#{shell_quote(identifier_from + '..')}" if identifier_from Chris@14: cmd << "#{shell_quote identifier_to}" if identifier_to Chris@0: cmd << " --since=#{shell_quote(options[:since].strftime("%Y-%m-%d %H:%M:%S"))}" if options[:since] chris@37: cmd << " -- #{shell_quote path}" if path && !path.empty? Chris@0: Chris@0: shellout(cmd) do |io| Chris@0: files=[] Chris@0: changeset = {} Chris@0: parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files Chris@0: revno = 1 Chris@0: Chris@0: io.each_line do |line| Chris@0: if line =~ /^commit ([0-9a-f]{40})$/ Chris@0: key = "commit" Chris@0: value = $1 Chris@0: if (parsing_descr == 1 || parsing_descr == 2) Chris@0: parsing_descr = 0 Chris@0: revision = Revision.new({ Chris@0: :identifier => changeset[:commit], Chris@0: :scmid => changeset[:commit], Chris@0: :author => changeset[:author], Chris@0: :time => Time.parse(changeset[:date]), Chris@0: :message => changeset[:description], Chris@0: :paths => files Chris@0: }) Chris@0: if block_given? Chris@0: yield revision Chris@0: else Chris@0: revisions << revision Chris@0: end Chris@0: changeset = {} Chris@0: files = [] Chris@0: revno = revno + 1 Chris@0: end Chris@0: changeset[:commit] = $1 Chris@0: elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ Chris@0: key = $1 Chris@0: value = $2 Chris@0: if key == "Author" Chris@0: changeset[:author] = value Chris@0: elsif key == "CommitDate" Chris@0: changeset[:date] = value Chris@0: end Chris@0: elsif (parsing_descr == 0) && line.chomp.to_s == "" Chris@0: parsing_descr = 1 Chris@0: changeset[:description] = "" Chris@0: elsif (parsing_descr == 1 || parsing_descr == 2) \ chris@37: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/ Chris@0: parsing_descr = 2 Chris@0: fileaction = $1 Chris@0: filepath = $2 Chris@0: files << {:action => fileaction, :path => filepath} Chris@0: elsif (parsing_descr == 1 || parsing_descr == 2) \ chris@37: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/ Chris@0: parsing_descr = 2 Chris@0: fileaction = $1 Chris@0: filepath = $3 Chris@0: files << {:action => fileaction, :path => filepath} Chris@0: elsif (parsing_descr == 1) && line.chomp.to_s == "" Chris@0: parsing_descr = 2 Chris@0: elsif (parsing_descr == 1) Chris@0: changeset[:description] << line[4..-1] Chris@0: end Chris@0: end Chris@0: Chris@0: if changeset[:commit] Chris@0: revision = Revision.new({ Chris@0: :identifier => changeset[:commit], Chris@0: :scmid => changeset[:commit], Chris@0: :author => changeset[:author], Chris@0: :time => Time.parse(changeset[:date]), Chris@0: :message => changeset[:description], Chris@0: :paths => files Chris@0: }) Chris@0: Chris@0: if block_given? Chris@0: yield revision Chris@0: else Chris@0: revisions << revision Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: revisions Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path ||= '' Chris@0: Chris@0: if identifier_to chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} diff --no-color #{shell_quote identifier_to} #{shell_quote identifier_from}" Chris@0: else chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} show --no-color #{shell_quote identifier_from}" Chris@0: end Chris@0: Chris@0: cmd << " -- #{shell_quote path}" unless path.empty? Chris@0: diff = [] Chris@0: shellout(cmd) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: diff Chris@0: end Chris@0: Chris@0: def annotate(path, identifier=nil) Chris@0: identifier = 'HEAD' if identifier.blank? Chris@0: cmd = "#{GIT_BIN} --git-dir #{target('')} blame -p #{shell_quote identifier} -- #{shell_quote path}" Chris@0: blame = Annotate.new Chris@0: content = nil Chris@0: shellout(cmd) { |io| io.binmode; content = io.read } Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: # git annotates binary files Chris@0: return nil if content.is_binary_data? Chris@0: identifier = '' Chris@0: # git shows commit author on the first occurrence only Chris@0: authors_by_commit = {} Chris@0: content.split("\n").each do |line| Chris@0: if line =~ /^([0-9a-f]{39,40})\s.*/ Chris@0: identifier = $1 Chris@0: elsif line =~ /^author (.+)/ Chris@0: authors_by_commit[identifier] = $1.strip Chris@0: elsif line =~ /^\t(.*)/ Chris@0: blame.add_line($1, Revision.new(:identifier => identifier, :author => authors_by_commit[identifier])) Chris@0: identifier = '' Chris@0: author = '' Chris@0: end Chris@0: end Chris@0: blame Chris@0: end Chris@0: Chris@0: def cat(path, identifier=nil) Chris@0: if identifier.nil? Chris@0: identifier = 'HEAD' Chris@0: end chris@37: cmd = "#{GIT_BIN} --git-dir #{target('')} show --no-color #{shell_quote(identifier + ':' + path)}" Chris@0: cat = nil Chris@0: shellout(cmd) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: return nil if $? && $?.exitstatus != 0 Chris@0: cat Chris@0: end Chris@117: Chris@117: class Revision < Redmine::Scm::Adapters::Revision Chris@117: # Returns the readable identifier Chris@117: def format_identifier Chris@117: identifier[0,8] Chris@117: end Chris@117: end Chris@0: end Chris@0: end Chris@0: end Chris@0: end