Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: require 'redmine/scm/adapters/abstract_adapter' Chris@1295: Chris@1295: module Redmine Chris@1295: module Scm Chris@1295: module Adapters Chris@1295: class GitAdapter < AbstractAdapter Chris@1295: Chris@1295: # Git executable name Chris@1295: GIT_BIN = Redmine::Configuration['scm_git_command'] || "git" Chris@1295: Chris@1295: class GitBranch < Branch Chris@1295: attr_accessor :is_default Chris@1295: end Chris@1295: Chris@1295: class << self Chris@1295: def client_command Chris@1295: @@bin ||= GIT_BIN Chris@1295: end Chris@1295: Chris@1295: def sq_bin Chris@1295: @@sq_bin ||= shell_quote_command Chris@1295: end Chris@1295: Chris@1295: def client_version Chris@1295: @@client_version ||= (scm_command_version || []) Chris@1295: end Chris@1295: Chris@1295: def client_available Chris@1295: !client_version.empty? Chris@1295: end Chris@1295: Chris@1295: def scm_command_version Chris@1295: scm_version = scm_version_from_command_line.dup Chris@1295: if scm_version.respond_to?(:force_encoding) Chris@1295: scm_version.force_encoding('ASCII-8BIT') Chris@1295: end Chris@1295: if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@1295: m[2].scan(%r{\d+}).collect(&:to_i) Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def scm_version_from_command_line Chris@1295: shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) Chris@1295: super Chris@1295: @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding Chris@1295: end Chris@1295: Chris@1295: def path_encoding Chris@1295: @path_encoding Chris@1295: end Chris@1295: Chris@1295: def info Chris@1295: begin Chris@1295: Info.new(:root_url => url, :lastrev => lastrev('',nil)) Chris@1295: rescue Chris@1295: nil Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def branches Chris@1295: return @branches if @branches Chris@1295: @branches = [] Chris@1295: cmd_args = %w|branch --no-color --verbose --no-abbrev| Chris@1295: git_cmd(cmd_args) do |io| Chris@1295: io.each_line do |line| Chris@1295: branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$') Chris@1295: bran = GitBranch.new(branch_rev[2]) Chris@1295: bran.revision = branch_rev[3] Chris@1295: bran.scmid = branch_rev[3] Chris@1295: bran.is_default = ( branch_rev[1] == '*' ) Chris@1295: @branches << bran Chris@1295: end Chris@1295: end Chris@1295: @branches.sort! Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def tags Chris@1295: return @tags if @tags Chris@1295: cmd_args = %w|tag| Chris@1295: git_cmd(cmd_args) do |io| Chris@1295: @tags = io.readlines.sort!.map{|t| t.strip} Chris@1295: end Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def default_branch Chris@1295: bras = self.branches Chris@1295: return nil if bras.nil? Chris@1295: default_bras = bras.select{|x| x.is_default == true} Chris@1295: return default_bras.first.to_s if ! default_bras.empty? Chris@1295: master_bras = bras.select{|x| x.to_s == 'master'} Chris@1295: master_bras.empty? ? bras.first.to_s : 'master' Chris@1295: end Chris@1295: Chris@1295: def entry(path=nil, identifier=nil) Chris@1295: parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?} Chris@1295: search_path = parts[0..-2].join('/') Chris@1295: search_name = parts[-1] Chris@1295: if search_path.blank? && search_name.blank? Chris@1295: # Root entry Chris@1295: Entry.new(:path => '', :kind => 'dir') Chris@1295: else Chris@1295: # Search for the entry in the parent directory Chris@1295: es = entries(search_path, identifier, Chris@1295: options = {:report_last_commit => false}) Chris@1295: es ? es.detect {|e| e.name == search_name} : nil Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def entries(path=nil, identifier=nil, options={}) Chris@1295: path ||= '' Chris@1295: p = scm_iconv(@path_encoding, 'UTF-8', path) Chris@1295: entries = Entries.new Chris@1295: cmd_args = %w|ls-tree -l| Chris@1295: cmd_args << "HEAD:#{p}" if identifier.nil? Chris@1295: cmd_args << "#{identifier}:#{p}" if identifier Chris@1295: git_cmd(cmd_args) do |io| Chris@1295: io.each_line do |line| Chris@1295: e = line.chomp.to_s Chris@1295: if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/ Chris@1295: type = $1 Chris@1295: sha = $2 Chris@1295: size = $3 Chris@1295: name = $4 Chris@1295: if name.respond_to?(:force_encoding) Chris@1295: name.force_encoding(@path_encoding) Chris@1295: end Chris@1295: full_path = p.empty? ? name : "#{p}/#{name}" Chris@1295: n = scm_iconv('UTF-8', @path_encoding, name) Chris@1295: full_p = scm_iconv('UTF-8', @path_encoding, full_path) Chris@1295: entries << Entry.new({:name => n, Chris@1295: :path => full_p, Chris@1295: :kind => (type == "tree") ? 'dir' : 'file', Chris@1295: :size => (type == "tree") ? nil : size, Chris@1295: :lastrev => options[:report_last_commit] ? Chris@1295: lastrev(full_path, identifier) : Revision.new Chris@1295: }) unless entries.detect{|entry| entry.name == name} Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: entries.sort_by_name Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def lastrev(path, rev) Chris@1295: return nil if path.nil? Chris@1295: cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1| Chris@1295: cmd_args << rev if rev Chris@1295: cmd_args << "--" << path unless path.empty? Chris@1295: lines = [] Chris@1295: git_cmd(cmd_args) { |io| lines = io.readlines } Chris@1295: begin Chris@1295: id = lines[0].split[1] Chris@1295: author = lines[1].match('Author:\s+(.*)$')[1] Chris@1295: time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1]) Chris@1295: Chris@1295: Revision.new({ Chris@1295: :identifier => id, Chris@1295: :scmid => id, Chris@1295: :author => author, Chris@1295: :time => time, Chris@1295: :message => nil, Chris@1295: :paths => nil Chris@1295: }) Chris@1295: rescue NoMethodError => e Chris@1295: logger.error("The revision '#{path}' has a wrong format") Chris@1295: return nil Chris@1295: end Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def revisions(path, identifier_from, identifier_to, options={}) Chris@1295: revs = Revisions.new Chris@1295: cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin| Chris@1295: cmd_args << "--reverse" if options[:reverse] Chris@1295: cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit] Chris@1295: cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty? Chris@1295: revisions = [] Chris@1295: if identifier_from || identifier_to Chris@1295: revisions << "" Chris@1295: revisions[0] << "#{identifier_from}.." if identifier_from Chris@1295: revisions[0] << "#{identifier_to}" if identifier_to Chris@1295: else Chris@1295: unless options[:includes].blank? Chris@1295: revisions += options[:includes] Chris@1295: end Chris@1295: unless options[:excludes].blank? Chris@1295: revisions += options[:excludes].map{|r| "^#{r}"} Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: git_cmd(cmd_args, {:write_stdin => true}) do |io| Chris@1295: io.binmode Chris@1295: io.puts(revisions.join("\n")) Chris@1295: io.close_write Chris@1295: files=[] Chris@1295: changeset = {} Chris@1295: parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files Chris@1295: Chris@1295: io.each_line do |line| Chris@1295: if line =~ /^commit ([0-9a-f]{40})(( [0-9a-f]{40})*)$/ Chris@1295: key = "commit" Chris@1295: value = $1 Chris@1295: parents_str = $2 Chris@1295: if (parsing_descr == 1 || parsing_descr == 2) Chris@1295: parsing_descr = 0 Chris@1295: revision = Revision.new({ Chris@1295: :identifier => changeset[:commit], Chris@1295: :scmid => changeset[:commit], Chris@1295: :author => changeset[:author], Chris@1295: :time => Time.parse(changeset[:date]), Chris@1295: :message => changeset[:description], Chris@1295: :paths => files, Chris@1295: :parents => changeset[:parents] Chris@1295: }) Chris@1295: if block_given? Chris@1295: yield revision Chris@1295: else Chris@1295: revs << revision Chris@1295: end Chris@1295: changeset = {} Chris@1295: files = [] Chris@1295: end Chris@1295: changeset[:commit] = $1 Chris@1295: unless parents_str.nil? or parents_str == "" Chris@1295: changeset[:parents] = parents_str.strip.split(' ') Chris@1295: end Chris@1295: elsif (parsing_descr == 0) && line =~ /^(\w+):\s*(.*)$/ Chris@1295: key = $1 Chris@1295: value = $2 Chris@1295: if key == "Author" Chris@1295: changeset[:author] = value Chris@1295: elsif key == "CommitDate" Chris@1295: changeset[:date] = value Chris@1295: end Chris@1295: elsif (parsing_descr == 0) && line.chomp.to_s == "" Chris@1295: parsing_descr = 1 Chris@1295: changeset[:description] = "" Chris@1295: elsif (parsing_descr == 1 || parsing_descr == 2) \ Chris@1295: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/ Chris@1295: parsing_descr = 2 Chris@1295: fileaction = $1 Chris@1295: filepath = $2 Chris@1295: p = scm_iconv('UTF-8', @path_encoding, filepath) Chris@1295: files << {:action => fileaction, :path => p} Chris@1295: elsif (parsing_descr == 1 || parsing_descr == 2) \ Chris@1295: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/ Chris@1295: parsing_descr = 2 Chris@1295: fileaction = $1 Chris@1295: filepath = $3 Chris@1295: p = scm_iconv('UTF-8', @path_encoding, filepath) Chris@1295: files << {:action => fileaction, :path => p} Chris@1295: elsif (parsing_descr == 1) && line.chomp.to_s == "" Chris@1295: parsing_descr = 2 Chris@1295: elsif (parsing_descr == 1) Chris@1295: changeset[:description] << line[4..-1] Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: if changeset[:commit] Chris@1295: revision = Revision.new({ Chris@1295: :identifier => changeset[:commit], Chris@1295: :scmid => changeset[:commit], Chris@1295: :author => changeset[:author], Chris@1295: :time => Time.parse(changeset[:date]), Chris@1295: :message => changeset[:description], Chris@1295: :paths => files, Chris@1295: :parents => changeset[:parents] Chris@1295: }) Chris@1295: if block_given? Chris@1295: yield revision Chris@1295: else Chris@1295: revs << revision Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: revs Chris@1295: rescue ScmCommandAborted => e Chris@1295: err_msg = "git log error: #{e.message}" Chris@1295: logger.error(err_msg) Chris@1295: if block_given? Chris@1295: raise CommandFailed, err_msg Chris@1295: else Chris@1295: revs Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def diff(path, identifier_from, identifier_to=nil) Chris@1295: path ||= '' Chris@1295: cmd_args = [] Chris@1295: if identifier_to Chris@1295: cmd_args << "diff" << "--no-color" << identifier_to << identifier_from Chris@1295: else Chris@1295: cmd_args << "show" << "--no-color" << identifier_from Chris@1295: end Chris@1295: cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty? Chris@1295: diff = [] Chris@1295: git_cmd(cmd_args) do |io| Chris@1295: io.each_line do |line| Chris@1295: diff << line Chris@1295: end Chris@1295: end Chris@1295: diff Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def annotate(path, identifier=nil) Chris@1295: identifier = 'HEAD' if identifier.blank? Chris@1295: cmd_args = %w|blame| Chris@1295: cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path) Chris@1295: blame = Annotate.new Chris@1295: content = nil Chris@1295: git_cmd(cmd_args) { |io| io.binmode; content = io.read } Chris@1295: # git annotates binary files Chris@1295: return nil if content.is_binary_data? Chris@1295: identifier = '' Chris@1295: # git shows commit author on the first occurrence only Chris@1295: authors_by_commit = {} Chris@1295: content.split("\n").each do |line| Chris@1295: if line =~ /^([0-9a-f]{39,40})\s.*/ Chris@1295: identifier = $1 Chris@1295: elsif line =~ /^author (.+)/ Chris@1295: authors_by_commit[identifier] = $1.strip Chris@1295: elsif line =~ /^\t(.*)/ Chris@1295: blame.add_line($1, Revision.new( Chris@1295: :identifier => identifier, Chris@1295: :revision => identifier, Chris@1295: :scmid => identifier, Chris@1295: :author => authors_by_commit[identifier] Chris@1295: )) Chris@1295: identifier = '' Chris@1295: author = '' Chris@1295: end Chris@1295: end Chris@1295: blame Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: def cat(path, identifier=nil) Chris@1295: if identifier.nil? Chris@1295: identifier = 'HEAD' Chris@1295: end Chris@1295: cmd_args = %w|show --no-color| Chris@1295: cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}" Chris@1295: cat = nil Chris@1295: git_cmd(cmd_args) do |io| Chris@1295: io.binmode Chris@1295: cat = io.read Chris@1295: end Chris@1295: cat Chris@1295: rescue ScmCommandAborted Chris@1295: nil Chris@1295: end Chris@1295: Chris@1295: class Revision < Redmine::Scm::Adapters::Revision Chris@1295: # Returns the readable identifier Chris@1295: def format_identifier Chris@1295: identifier[0,8] Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def git_cmd(args, options = {}, &block) Chris@1295: repo_path = root_url || url Chris@1295: full_args = ['--git-dir', repo_path] Chris@1295: if self.class.client_version_above?([1, 7, 2]) Chris@1295: full_args << '-c' << 'core.quotepath=false' Chris@1295: full_args << '-c' << 'log.decorate=no' Chris@1295: end Chris@1295: full_args += args Chris@1295: ret = shellout( Chris@1295: self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '), Chris@1295: options, Chris@1295: &block Chris@1295: ) Chris@1295: if $? && $?.exitstatus != 0 Chris@1295: raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}" Chris@1295: end Chris@1295: ret Chris@1295: end Chris@1295: private :git_cmd Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: end