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