From 33b3cf7b34bfa6e2a2517023cd36cc98f5a79a6a Mon Sep 17 00:00:00 2001
From: Jeremy Bopp <jeremy@bopp.net>
Date: Thu, 15 Mar 2012 17:01:13 -0500
Subject: [PATCH 1/2] Pass revisions to git-log via stdin

* This avoids the risk of overflowing the command buffer with revision strings
* Invalid revisions for the repository are ignored
* Added an options hash for #shellout to optionally allow writes to the subprocess
* Set the encoding for the pipe opened by #shellout in a safer way
---
 lib/redmine/scm/adapters/abstract_adapter.rb       |   14 ++---
 lib/redmine/scm/adapters/git_adapter.rb            |   67 ++++++++++++++-----
 .../lib/redmine/scm/adapters/git_adapter_test.rb   |   30 +++++----
 3 files changed, 71 insertions(+), 40 deletions(-)

diff --git a/lib/redmine/scm/adapters/abstract_adapter.rb b/lib/redmine/scm/adapters/abstract_adapter.rb
index bb46a48..4ae93c3 100644
--- a/lib/redmine/scm/adapters/abstract_adapter.rb
+++ b/lib/redmine/scm/adapters/abstract_adapter.rb
@@ -206,15 +206,15 @@ module Redmine
           self.class.logger
         end
 
-        def shellout(cmd, &block)
-          self.class.shellout(cmd, &block)
+        def shellout(cmd, options = {}, &block)
+          self.class.shellout(cmd, options, &block)
         end
 
         def self.logger
           Rails.logger
         end
 
-        def self.shellout(cmd, &block)
+        def self.shellout(cmd, options = {}, &block)
           if logger && logger.debug?
             logger.debug "Shelling out: #{strip_credential(cmd)}"
           end
@@ -223,13 +223,9 @@ module Redmine
             cmd = "#{cmd} 2>>#{shell_quote(Rails.root.join('log/scm.stderr.log').to_s)}"
           end
           begin
-            if RUBY_VERSION < '1.9'
-              mode = "r+"
-            else
-              mode = "r+:ASCII-8BIT"
-            end
+            mode = (options[:mode] || 'r').to_s
             IO.popen(cmd, mode) do |io|
-              io.close_write
+              io.set_encoding("ASCII-8BIT") if io.respond_to?(:set_encoding)
               block.call(io) if block_given?
             end
           ## If scm command does not exist,
diff --git a/lib/redmine/scm/adapters/git_adapter.rb b/lib/redmine/scm/adapters/git_adapter.rb
index 44d2be8..301b6e7 100644
--- a/lib/redmine/scm/adapters/git_adapter.rb
+++ b/lib/redmine/scm/adapters/git_adapter.rb
@@ -82,7 +82,7 @@ module Redmine
           return @branches if @branches
           @branches = []
           cmd_args = %w|branch --no-color --verbose --no-abbrev|
-          scm_cmd(*cmd_args) do |io|
+          scm_cmd(cmd_args) do |io|
             io.each_line do |line|
               branch_rev = line.match('\s*(\*?)\s*(.*?)\s*([0-9a-f]{40}).*$')
               bran = GitBranch.new(branch_rev[2])
@@ -100,7 +100,7 @@ module Redmine
         def tags
           return @tags if @tags
           cmd_args = %w|tag|
-          scm_cmd(*cmd_args) do |io|
+          scm_cmd(cmd_args) do |io|
             @tags = io.readlines.sort!.map{|t| t.strip}
           end
         rescue ScmCommandAborted
@@ -138,7 +138,7 @@ module Redmine
           cmd_args = %w|ls-tree -l|
           cmd_args << "HEAD:#{p}"          if identifier.nil?
           cmd_args << "#{identifier}:#{p}" if identifier
-          scm_cmd(*cmd_args) do |io|
+          scm_cmd(cmd_args) do |io|
             io.each_line do |line|
               e = line.chomp.to_s
               if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
@@ -173,7 +173,7 @@ module Redmine
           cmd_args << rev if rev
           cmd_args << "--" << path unless path.empty?
           lines = []
-          scm_cmd(*cmd_args) { |io| lines = io.readlines }
+          scm_cmd(cmd_args) { |io| lines = io.readlines }
           begin
               id = lines[0].split[1]
               author = lines[1].match('Author:\s+(.*)$')[1]
@@ -197,24 +197,29 @@ module Redmine
 
         def revisions(path, identifier_from, identifier_to, options={})
           revs = Revisions.new
-          cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents|
+          cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller --parents --stdin|
           cmd_args << "--reverse" if options[:reverse]
           cmd_args << "-n" << "#{options[:limit].to_i}" if options[:limit]
-          from_to = ""
+          cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
+          revisions = []
           if identifier_from || identifier_to
-            from_to << "#{identifier_from}.." if identifier_from
-            from_to << "#{identifier_to}" if identifier_to
-            cmd_args << from_to if !from_to.empty?
+            revisions << ""
+            revisions[0] << "#{identifier_from}.." if identifier_from
+            revisions[0] << "#{identifier_to}" if identifier_to
           else
-            cmd_args += options[:includes] unless options[:includes].blank?
+            unless options[:includes].blank?
+              revisions += ignore_missing_revisions(options[:includes])
+            end
             unless options[:excludes].blank?
-              cmd_args << "--not"
-              cmd_args += options[:excludes]
+              revisions +=
+                ignore_missing_revisions(options[:excludes]).map{|r| "^#{r}"}
             end
           end
-          cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
 
-          scm_cmd *cmd_args do |io|
+          scm_cmd(cmd_args, :mode => "r+b") do |io|
+            io.puts(revisions.join("\n"))
+            io.close_write
+
             files=[]
             changeset = {}
             parsing_descr = 0  #0: not parsing desc or files, 1: parsing desc, 2: parsing files
@@ -317,7 +322,7 @@ module Redmine
           end
           cmd_args << "--" <<  scm_iconv(@path_encoding, 'UTF-8', path) unless path.empty?
           diff = []
-          scm_cmd *cmd_args do |io|
+          scm_cmd cmd_args do |io|
             io.each_line do |line|
               diff << line
             end
@@ -333,7 +338,7 @@ module Redmine
           cmd_args << "-p" << identifier << "--" <<  scm_iconv(@path_encoding, 'UTF-8', path)
           blame = Annotate.new
           content = nil
-          scm_cmd(*cmd_args) { |io| io.binmode; content = io.read }
+          scm_cmd(cmd_args) { |io| io.binmode; content = io.read }
           # git annotates binary files
           return nil if content.is_binary_data?
           identifier = ''
@@ -367,7 +372,7 @@ module Redmine
           cmd_args = %w|show --no-color|
           cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
           cat = nil
-          scm_cmd(*cmd_args) do |io|
+          scm_cmd(cmd_args) do |io|
             io.binmode
             cat = io.read
           end
@@ -383,7 +388,7 @@ module Redmine
           end
         end
 
-        def scm_cmd(*args, &block)
+        def scm_cmd(args, options = {}, &block)
           repo_path = root_url || url
           full_args = ['--git-dir', repo_path]
           if self.class.client_version_above?([1, 7, 2])
@@ -393,6 +398,7 @@ module Redmine
           full_args += args
           ret = shellout(
                    self.class.sq_bin + ' ' + full_args.map { |e| shell_quote e.to_s }.join(' '),
+                   options,
                    &block
                    )
           if $? && $?.exitstatus != 0
@@ -401,6 +407,31 @@ module Redmine
           ret
         end
         private :scm_cmd
+
+        # Return an array containing only the given revisions that exist in the
+        # repository.
+        #
+        # NOTE:
+        # This can be removed once the minimum supported Git version supports
+        # the --ignore-missing option for git-log and #revisions is modified to
+        # use that option.
+        def ignore_missing_revisions(revisions)
+          revisions.select do |revision|
+            begin
+              cmd_args = %w|rev-parse --verify --quiet|
+              cmd_args << revision
+              scm_cmd(cmd_args) do |io|
+                # Read and discard the output in order to make the command
+                # happy.
+                io.read
+              end
+              true
+            rescue ScmCommandAborted
+              false
+            end
+          end
+        end
+        private :ignore_missing_revisions
       end
     end
   end
diff --git a/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb b/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
index b73018f..2cdb14f 100644
--- a/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
+++ b/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
@@ -289,20 +289,24 @@ begin
       end
 
       def test_revisions_invalid_rev_excludes
-        assert_equal [],
-                     @adapter.revisions('', nil, nil,
-                                        {:reverse => true,
-                                         :includes => ['83ca5fd546063a3c7dc2e568ba3355661a9e2b2c'],
-                                         :excludes => ['0123abcd4567']})
-        assert_raise Redmine::Scm::Adapters::CommandFailed do
-          revs1 = []
-          @adapter.revisions('', nil, nil,
-                             {:reverse => true,
-                              :includes => ['83ca5fd546063a3c7dc2e568ba3355661a9e2b2c'],
-                              :excludes => ['0123abcd4567']}) do |rev|
-            revs1 << rev
-          end
+        revs1 = @adapter.revisions('', nil, nil,
+                                   {:reverse => true,
+                                    :includes => ['83ca5fd546063a3c7dc2e568ba3355661a9e2b2c'],
+                                    :excludes => ['0123abcd4567']})
+        assert_equal 15, revs1.length
+        assert_equal '83ca5fd546063a3c7dc2e568ba3355661a9e2b2c', revs1[-1].identifier
+        assert_equal '7234cb2750b63f47bff735edc50a1c0a433c2518', revs1[ 0].identifier
+
+        revs2 = []
+        @adapter.revisions('', nil, nil,
+                           {:reverse => true,
+                            :includes => ['83ca5fd546063a3c7dc2e568ba3355661a9e2b2c'],
+                            :excludes => ['0123abcd4567']}) do |rev|
+          revs2 << rev
         end
+        assert_equal 15, revs2.length
+        assert_equal '83ca5fd546063a3c7dc2e568ba3355661a9e2b2c', revs2[-1].identifier
+        assert_equal '7234cb2750b63f47bff735edc50a1c0a433c2518', revs2[ 0].identifier
       end
 
       def test_getting_revisions_with_spaces_in_filename
-- 
1.7.5.4

