Chris@441: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 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@441: # 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@441: # 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@1136: require_dependency 'redmine/scm/adapters/abstract_adapter' Chris@0: Chris@0: module Redmine Chris@0: module Scm Chris@245: module Adapters Chris@0: class GitAdapter < AbstractAdapter Chris@245: Chris@0: # Git executable name Chris@210: GIT_BIN = Redmine::Configuration['scm_git_command'] || "git" Chris@0: Chris@1115: class GitBranch < Branch Chris@1115: attr_accessor :is_default Chris@1115: end Chris@1115: Chris@245: class << self Chris@245: def client_command Chris@245: @@bin ||= GIT_BIN Chris@245: end Chris@245: Chris@245: def sq_bin Chris@909: @@sq_bin ||= shell_quote_command Chris@245: end Chris@245: Chris@245: def client_version Chris@245: @@client_version ||= (scm_command_version || []) Chris@245: end Chris@245: Chris@245: def client_available Chris@245: !client_version.empty? Chris@245: end Chris@245: Chris@245: def scm_command_version Chris@245: scm_version = scm_version_from_command_line.dup Chris@245: if scm_version.respond_to?(:force_encoding) Chris@245: scm_version.force_encoding('ASCII-8BIT') Chris@245: end Chris@245: if m = scm_version.match(%r{\A(.*?)((\d+\.)+\d+)}) Chris@245: m[2].scan(%r{\d+}).collect(&:to_i) Chris@245: end Chris@245: end Chris@245: Chris@245: def scm_version_from_command_line Chris@245: shellout("#{sq_bin} --version --no-color") { |io| io.read }.to_s Chris@245: end Chris@245: end Chris@245: Chris@245: def initialize(url, root_url=nil, login=nil, password=nil, path_encoding=nil) Chris@245: super Chris@441: @path_encoding = path_encoding.blank? ? 'UTF-8' : path_encoding Chris@441: end Chris@441: Chris@441: def path_encoding Chris@441: @path_encoding Chris@245: end Chris@245: 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@909: cmd_args = %w|branch --no-color --verbose --no-abbrev| Chris@1115: git_cmd(cmd_args) do |io| Chris@0: io.each_line do |line| Chris@1115: branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$') Chris@1115: bran = GitBranch.new(branch_rev[2]) Chris@1115: bran.revision = branch_rev[3] Chris@1115: bran.scmid = branch_rev[3] Chris@1115: bran.is_default = ( branch_rev[1] == '*' ) Chris@909: @branches << bran Chris@0: end Chris@0: end Chris@0: @branches.sort! Chris@441: rescue ScmCommandAborted Chris@441: nil Chris@0: end Chris@0: Chris@0: def tags Chris@0: return @tags if @tags Chris@441: cmd_args = %w|tag| Chris@1115: git_cmd(cmd_args) do |io| Chris@0: @tags = io.readlines.sort!.map{|t| t.strip} Chris@0: end Chris@441: rescue ScmCommandAborted Chris@441: nil Chris@441: end Chris@441: Chris@441: def default_branch Chris@441: bras = self.branches Chris@441: return nil if bras.nil? Chris@1115: default_bras = bras.select{|x| x.is_default == true} Chris@1115: return default_bras.first.to_s if ! default_bras.empty? Chris@1115: master_bras = bras.select{|x| x.to_s == 'master'} Chris@1115: master_bras.empty? ? bras.first.to_s : 'master' Chris@441: end Chris@441: Chris@441: def entry(path=nil, identifier=nil) Chris@441: parts = path.to_s.split(%r{[\/\\]}).select {|n| !n.blank?} Chris@441: search_path = parts[0..-2].join('/') Chris@441: search_name = parts[-1] Chris@441: if search_path.blank? && search_name.blank? Chris@441: # Root entry Chris@441: Entry.new(:path => '', :kind => 'dir') Chris@441: else Chris@441: # Search for the entry in the parent directory Chris@441: es = entries(search_path, identifier, Chris@441: options = {:report_last_commit => false}) Chris@441: es ? es.detect {|e| e.name == search_name} : nil Chris@441: end Chris@0: end Chris@0: Chris@441: def entries(path=nil, identifier=nil, options={}) Chris@0: path ||= '' Chris@441: p = scm_iconv(@path_encoding, 'UTF-8', path) Chris@0: entries = Entries.new Chris@441: cmd_args = %w|ls-tree -l| Chris@441: cmd_args << "HEAD:#{p}" if identifier.nil? Chris@441: cmd_args << "#{identifier}:#{p}" if identifier Chris@1115: git_cmd(cmd_args) 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@441: sha = $2 Chris@0: size = $3 Chris@0: name = $4 Chris@441: if name.respond_to?(:force_encoding) Chris@441: name.force_encoding(@path_encoding) Chris@441: end Chris@441: full_path = p.empty? ? name : "#{p}/#{name}" Chris@441: n = scm_iconv('UTF-8', @path_encoding, name) Chris@441: full_p = scm_iconv('UTF-8', @path_encoding, full_path) Chris@441: entries << Entry.new({:name => n, Chris@441: :path => full_p, Chris@0: :kind => (type == "tree") ? 'dir' : 'file', Chris@0: :size => (type == "tree") ? nil : size, Chris@441: :lastrev => options[:report_last_commit] ? Chris@441: lastrev(full_path, identifier) : Revision.new Chris@0: }) unless entries.detect{|entry| entry.name == name} Chris@0: end Chris@0: end Chris@0: end Chris@0: entries.sort_by_name Chris@441: rescue ScmCommandAborted Chris@441: nil Chris@0: end Chris@0: Chris@245: def lastrev(path, rev) Chris@0: return nil if path.nil? Chris@245: cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1| Chris@441: cmd_args << rev if rev Chris@245: cmd_args << "--" << path unless path.empty? Chris@117: lines = [] Chris@1115: git_cmd(cmd_args) { |io| lines = io.readlines } Chris@117: begin Chris@117: id = lines[0].split[1] Chris@117: author = lines[1].match('Author:\s+(.*)$')[1] Chris@245: time = Time.parse(lines[4].match('CommitDate:\s+(.*)$')[1]) Chris@0: Chris@0: Revision.new({ Chris@0: :identifier => id, Chris@441: :scmid => id, Chris@441: :author => author, Chris@441: :time => time, Chris@441: :message => nil, Chris@441: :paths => nil Chris@245: }) Chris@117: rescue NoMethodError => e Chris@0: logger.error("The revision '#{path}' has a wrong format") Chris@0: return nil Chris@0: end Chris@245: rescue ScmCommandAborted Chris@245: nil Chris@0: end Chris@0: Chris@0: def revisions(path, identifier_from, identifier_to, options={}) Chris@441: revs = Revisions.new Chris@1115: cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin| Chris@245: cmd_args << "--reverse" if options[:reverse] Chris@245: cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit] Chris@441: cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty? Chris@1115: revisions = [] Chris@1115: if identifier_from || identifier_to Chris@1115: revisions << "" Chris@1115: revisions[0] << "#{identifier_from}.." if identifier_from Chris@1115: revisions[0] << "#{identifier_to}" if identifier_to Chris@1115: else Chris@1115: unless options[:includes].blank? Chris@1115: revisions += options[:includes] Chris@1115: end Chris@1115: unless options[:excludes].blank? Chris@1115: revisions += options[:excludes].map{|r| "^#{r}"} Chris@1115: end Chris@1115: end Chris@0: Chris@1115: git_cmd(cmd_args, {:write_stdin => true}) do |io| Chris@1115: io.binmode Chris@1115: io.puts(revisions.join("\n")) Chris@1115: io.close_write 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: Chris@0: io.each_line do |line| Chris@909: if line =~ /^commit ([0-9a-f]{40})(( [0-9a-f]{40})*)$/ Chris@0: key = "commit" Chris@0: value = $1 Chris@909: parents_str = $2 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@441: :scmid => changeset[:commit], Chris@441: :author => changeset[:author], Chris@441: :time => Time.parse(changeset[:date]), Chris@441: :message => changeset[:description], Chris@909: :paths => files, Chris@909: :parents => changeset[:parents] Chris@0: }) Chris@0: if block_given? Chris@0: yield revision Chris@0: else Chris@441: revs << revision Chris@0: end Chris@0: changeset = {} Chris@0: files = [] Chris@0: end Chris@0: changeset[:commit] = $1 Chris@909: unless parents_str.nil? or parents_str == "" Chris@909: changeset[:parents] = parents_str.strip.split(' ') Chris@909: end 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@441: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\t(.+)$/ Chris@0: parsing_descr = 2 Chris@441: fileaction = $1 Chris@441: filepath = $2 Chris@441: p = scm_iconv('UTF-8', @path_encoding, filepath) Chris@441: files << {:action => fileaction, :path => p} Chris@0: elsif (parsing_descr == 1 || parsing_descr == 2) \ Chris@441: && line =~ /^:\d+\s+\d+\s+[0-9a-f.]+\s+[0-9a-f.]+\s+(\w)\d+\s+(\S+)\t(.+)$/ Chris@0: parsing_descr = 2 Chris@441: fileaction = $1 Chris@441: filepath = $3 Chris@441: p = scm_iconv('UTF-8', @path_encoding, filepath) Chris@441: files << {:action => fileaction, :path => p} 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@441: end Chris@0: Chris@0: if changeset[:commit] Chris@0: revision = Revision.new({ Chris@0: :identifier => changeset[:commit], Chris@441: :scmid => changeset[:commit], Chris@441: :author => changeset[:author], Chris@441: :time => Time.parse(changeset[:date]), Chris@441: :message => changeset[:description], Chris@909: :paths => files, Chris@909: :parents => changeset[:parents] Chris@441: }) Chris@0: if block_given? Chris@0: yield revision Chris@0: else Chris@441: revs << revision Chris@0: end Chris@0: end Chris@0: end Chris@441: revs Chris@441: rescue ScmCommandAborted => e Chris@1115: err_msg = "git log error: #{e.message}" Chris@1115: logger.error(err_msg) Chris@1115: if block_given? Chris@1115: raise CommandFailed, err_msg Chris@1115: else Chris@1115: revs Chris@1115: end Chris@0: end Chris@0: Chris@0: def diff(path, identifier_from, identifier_to=nil) Chris@0: path ||= '' Chris@441: cmd_args = [] Chris@0: if identifier_to Chris@441: cmd_args << "diff" << "--no-color" << identifier_to << identifier_from Chris@0: else Chris@441: cmd_args << "show" << "--no-color" << identifier_from Chris@0: end Chris@441: cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty? Chris@0: diff = [] Chris@1115: git_cmd(cmd_args) do |io| Chris@0: io.each_line do |line| Chris@0: diff << line Chris@0: end Chris@0: end Chris@0: diff Chris@441: rescue ScmCommandAborted Chris@441: nil Chris@0: end Chris@441: Chris@0: def annotate(path, identifier=nil) Chris@0: identifier = 'HEAD' if identifier.blank? Chris@441: cmd_args = %w|blame| Chris@441: cmd_args << "-p" << identifier << "--" << scm_iconv(@path_encoding, 'UTF-8', path) Chris@0: blame = Annotate.new Chris@0: content = nil Chris@1115: git_cmd(cmd_args) { |io| io.binmode; content = io.read } 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@441: blame.add_line($1, Revision.new( Chris@441: :identifier => identifier, Chris@441: :revision => identifier, Chris@441: :scmid => identifier, Chris@441: :author => authors_by_commit[identifier] Chris@441: )) Chris@0: identifier = '' Chris@0: author = '' Chris@0: end Chris@0: end Chris@0: blame Chris@441: rescue ScmCommandAborted Chris@441: nil Chris@0: end Chris@245: Chris@0: def cat(path, identifier=nil) Chris@0: if identifier.nil? Chris@0: identifier = 'HEAD' Chris@0: end Chris@441: cmd_args = %w|show --no-color| Chris@441: cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}" Chris@0: cat = nil Chris@1115: git_cmd(cmd_args) do |io| Chris@0: io.binmode Chris@0: cat = io.read Chris@0: end Chris@0: cat Chris@441: rescue ScmCommandAborted Chris@441: nil 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@245: Chris@1115: def git_cmd(args, options = {}, &block) Chris@245: repo_path = root_url || url Chris@909: full_args = ['--git-dir', repo_path] Chris@441: if self.class.client_version_above?([1, 7, 2]) Chris@441: full_args << '-c' << 'core.quotepath=false' Chris@441: full_args << '-c' << 'log.decorate=no' Chris@441: end Chris@245: full_args += args Chris@909: ret = shellout( Chris@909: self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '), Chris@1115: options, Chris@909: &block Chris@909: ) Chris@245: if $? && $?.exitstatus != 0 Chris@245: raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}" Chris@245: end Chris@245: ret Chris@245: end Chris@1115: private :git_cmd Chris@0: end Chris@0: end Chris@0: end Chris@0: end