Commit 21ae869d authored by Enrique García Cota's avatar Enrique García Cota

increased scm file download in scm views

parent c4f80d5e
......@@ -118,41 +118,48 @@ class RepositoriesController < ApplicationController
# If the entry is a dir, show the browser
(show; return) if @entry.is_dir?
@content = @repository.cat(@path, @rev)
(show_error_not_found; return) unless @content
if 'raw' == params[:format] ||
(@content.size && @content.size > Setting.file_max_size_displayed.to_i.kilobyte) ||
! is_entry_text_data?(@content, @path)
# Force the download
send_opt = { :filename => filename_for_content_disposition(@path.split('/').last) }
@repository.cat_to_tempfile(@path, @rev) do |f|
if params[:format] == 'raw' || is_too_large_to_show?(f) || is_binary?(f, @path)
send_type = Redmine::MimeType.of(@path)
send_opt[:type] = send_type.to_s if send_type
send_data @content, send_opt
options = {
:filename => filename_for_content_disposition(@path.split('/').last),
:disposition => 'attachment',
:x_sendfile => false # x_sendfile does not work well with tempfiles
}
options[:type] = send_type.to_s if send_type
send_file(f.path, options)
else
# Prevent empty lines when displaying a file with Windows style eol
# TODO: UTF-16
# Is this needs? AttachmentsController reads file simply.
@content.gsub!("\r\n", "\n")
@content = f.read
@changeset = @repository.find_changeset_by_name(@rev)
end
end
rescue Errno::ENOENT
show_error_not_found
end
def is_entry_text_data?(ent, path)
# UTF-16 contains "\x00".
# It is very strict that file contains less than 30% of ascii symbols
# in non Western Europe.
return true if Redmine::MimeType.is_type?('text', path)
# Ruby 1.8.6 has a bug of integer divisions.
# http://apidock.com/ruby/v1_8_6_287/String/is_binary_data%3F
if ent.respond_to?("is_binary_data?") && ent.is_binary_data? # Ruby 1.8.x and <1.9.2
return false
elsif ent.respond_to?(:force_encoding) && (ent.dup.force_encoding("UTF-8") != ent.dup.force_encoding("BINARY") ) # Ruby 1.9.2
# TODO: need to handle edge cases of non-binary content that isn't UTF-8
return false
def is_too_large_to_show?(f)
f.size > Setting.file_max_size_displayed.to_i.kilobyte
end
true
private :is_too_large_to_show?
def is_binary?(file, path)
return false if Redmine::MimeType.is_type?('text', path)
# First block of the file is examined for odd
# characters such as strange control codes or char-
# acters with the high bit set. If too many strange
# characters (>30%) are found, it's a binary file,
# otherwise it's a text file. Also, any file con-
# taining null in the first block is considered a
# binary file.
blk = file.read(Setting.file_max_size_displayed.to_i.kilobyte)
return blk.size == 0 ||
blk.count("^ -~", "^\r\n") / blk.size > 0.3 ||
blk.count("\x00") > 0
end
private :is_entry_text_data?
private :is_binary?
def annotate
@entry = @repository.entry(@path, @rev)
......
......@@ -104,6 +104,10 @@ class Repository < ActiveRecord::Base
scm.cat(path, identifier)
end
def cat_to_tempfile(path, identifier, &block)
scm.cat_to_tempfile(path, identifier, &block)
end
def diff(path, rev, rev_to)
scm.diff(path, rev, rev_to)
end
......
......@@ -69,7 +69,7 @@ module Redmine
end
def adapter_name
'Abstract'
self.class.name.gsub("Adapter","")
end
def supports_cat?
......@@ -143,6 +143,19 @@ module Redmine
return nil
end
def cat_to_tempfile(path, identifier, &block)
prefix = path.split("/").last
tmp_path = Rails.root.join('tmp')
Tempfile.open(prefix, tmp_path) do |f|
save_entry_in_file(f,path,identifier)
block.call(f)
end
end
def save_entry_in_file(file, path, identifier)
return nil
end
def with_leading_slash(path)
path ||= ''
(path[0,1]!="/") ? "/#{path}" : path
......@@ -183,34 +196,56 @@ module Redmine
self.class.logger
end
def shellout(cmd, &block)
self.class.shellout(cmd, &block)
def shellout(cmd, output_path=nil, &block)
self.class.shellout(cmd, output_path, &block)
end
def build_scm_cmd(cmd_args)
([ self.class.sq_bin ] + cmd_args).join(' ')
end
def scm_cmd(cmd_args, output_path=nil, &block)
cmd = build_scm_cmd(cmd_args)
begin
ret = shellout(cmd, output_path, &block)
rescue Exception => e
msg = strip_credential(e.message)
cmd = strip_credential(cmd)
logger.error("Error executing #{adapter_name} command [#{cmd}]: #{msg}")
end
return nil if $? && $?.exitstatus != 0
ret
end
def self.logger
RAILS_DEFAULT_LOGGER
end
def self.shellout(cmd, &block)
logger.debug "Shelling out: #{strip_credential(cmd)}" if logger && logger.debug?
if Rails.env == 'development'
# Capture stderr when running in dev environment
cmd = "#{cmd} 2>>#{RAILS_ROOT}/log/scm.stderr.log"
def self.process_cmd(cmd, output_path)
cmd = Rails.env == 'development' ? "#{cmd} 2>>#{RAILS_ROOT}/log/scm.stderr.log" : cmd
cmd = "#{cmd} >> #{output_path}" if output_path.present?
cmd
end
begin
if RUBY_VERSION < '1.9'
mode = "r+"
else
mode = "r+:ASCII-8BIT"
def self.get_reading_mode_for_ruby_version
RUBY_VERSION < '1.9' ? 'r+' : 'r+:ASCII-8BIT'
end
def self.shellout(cmd, output_path=nil, &block)
logger.debug("Shelling out: #{strip_credential(cmd)}") if logger && logger.respond_to?(:debug)
cmd = process_cmd(cmd, output_path)
mode = get_reading_mode_for_ruby_version
begin
result = nil
IO.popen(cmd, mode) do |io|
io.close_write
block.call(io) if block_given?
result = block.call(io) if block_given?
end
result
rescue Errno::ENOENT => e
msg = strip_credential(e.message)
# The command failed, log it and re-raise
logger.error("SCM command failed, make sure that your SCM binary (eg. svn) is in PATH (#{ENV['PATH']}): #{strip_credential(cmd)}\n with: #{msg}")
cmd = strip_credential(cmd)
logger.error("SCM command failed, make sure that your SCM binary (eg. svn) is in PATH (#{ENV['PATH']}): #{cmd}\n with: #{msg}")
raise CommandFailed.new(msg)
end
end
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
require 'rexml/document'
module Redmine
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
require 'find'
module Redmine
......@@ -91,6 +91,14 @@ module Redmine
raise CommandFailed.new(err.message)
end
def save_entry_in_file(f, path, identifier)
p = scm_iconv(@path_encoding, 'UTF-8', target(path))
FileUtils.cp(p, f.path)
rescue => err
logger.error "scm: filesystem: error: #{err.message}"
raise CommandFailed.new(err.message)
end
private
# AbstractAdapter::target is implicitly made to quote paths.
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
module Redmine
module Scm
......@@ -24,9 +24,6 @@ module Redmine
# Git executable name
GIT_BIN = Redmine::Configuration['scm_git_command'] || "git" unless defined?(GIT_BIN)
# raised if scm command exited with error, e.g. unknown revision.
class ScmCommandAborted < CommandFailed; end
class << self
def client_command
@@bin ||= GIT_BIN
......@@ -75,26 +72,22 @@ module Redmine
def branches
return @branches if @branches
@branches = []
cmd_args = %w|branch --no-color|
scm_cmd(*cmd_args) do |io|
scm_cmd(cmd_args) do |io|
@branches = []
io.each_line do |line|
@branches << line.match('\s*\*?\s*(.*)$')[1]
end
end
@branches.sort!
rescue ScmCommandAborted
nil
end
end
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
nil
end
def default_branch
......@@ -106,11 +99,14 @@ module Redmine
def entries(path=nil, identifier=nil)
path ||= ''
p = scm_iconv(@path_encoding, 'UTF-8', path)
entries = Entries.new
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|
entries = Entries.new
io.each_line do |line|
e = line.chomp.to_s
if e =~ /^\d+\s+(\w+)\s+([0-9a-f]{40})\s+([0-9-]+)\t(.+)$/
......@@ -124,18 +120,20 @@ module Redmine
full_path = p.empty? ? name : "#{p}/#{name}"
n = scm_iconv('UTF-8', @path_encoding, name)
full_p = scm_iconv('UTF-8', @path_encoding, full_path)
unless entries.detect{|entry| entry.name == name}
entries << Entry.new({:name => n,
:path => full_p,
:kind => (type == "tree") ? 'dir' : 'file',
:size => (type == "tree") ? nil : size,
:lastrev => @flag_report_last_commit ? lastrev(full_path, identifier) : Revision.new
}) unless entries.detect{|entry| entry.name == name}
})
end
end
end
entries.sort_by_name
rescue ScmCommandAborted
nil
end
end
def lastrev(path, rev)
......@@ -143,8 +141,8 @@ module Redmine
cmd_args = %w|log --no-color --encoding=UTF-8 --date=iso --pretty=fuller --no-merges -n 1|
cmd_args << rev if rev
cmd_args << "--" << path unless path.empty?
lines = []
scm_cmd(*cmd_args) { |io| lines = io.readlines }
lines = scm_cmd(cmd_args) { |io| io.readlines }
if lines
begin
id = lines[0].split[1]
author = lines[1].match('Author:\s+(.*)$')[1]
......@@ -162,12 +160,12 @@ module Redmine
logger.error("The revision '#{path}' has a wrong format")
return nil
end
rescue ScmCommandAborted
nil
end
end
def revisions(path, identifier_from, identifier_to, options={})
revisions = Revisions.new
cmd_args = %w|log --no-color --encoding=UTF-8 --raw --date=iso --pretty=fuller|
cmd_args << "--reverse" if options[:reverse]
cmd_args << "--all" if options[:all]
......@@ -179,7 +177,7 @@ module Redmine
cmd_args << "--since='#{options[:since].strftime("%Y-%m-%d %H:%M:%S")}'" if options[:since]
cmd_args << "--" << scm_iconv(@path_encoding, 'UTF-8', path) if path && !path.empty?
scm_cmd *cmd_args do |io|
scm_cmd cmd_args do |io|
files=[]
changeset = {}
parsing_descr = 0 #0: not parsing desc or files, 1: parsing desc, 2: parsing files
......@@ -255,11 +253,10 @@ module Redmine
revisions << revision
end
end
end
revisions
rescue ScmCommandAborted
revisions
end
end
def diff(path, identifier_from, identifier_to=nil)
path ||= ''
......@@ -271,14 +268,12 @@ 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
end
diff
rescue ScmCommandAborted
nil
end
end
def annotate(path, identifier=nil)
......@@ -286,8 +281,11 @@ module Redmine
cmd_args = %w|blame|
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 }
content = scm_cmd(cmd_args) do |io|
io.binmode
io.read
end
if content
# git annotates binary files
if content.respond_to?("is_binary_data?") && content.is_binary_data? # Ruby 1.8.x and <1.9.2
return nil
......@@ -312,8 +310,7 @@ module Redmine
end
end
blame
rescue ScmCommandAborted
nil
end
end
def cat(path, identifier=nil)
......@@ -322,14 +319,16 @@ module Redmine
end
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
io.read
end
cat
rescue ScmCommandAborted
nil
end
def save_entry_in_file(f, path, identifier)
cmd_args = %w|show --no-color|
cmd_args << "#{identifier}:#{scm_iconv(@path_encoding, 'UTF-8', path)}"
scm_cmd(cmd_args, f.path)
end
class Revision < Redmine::Scm::Adapters::Revision
......@@ -339,20 +338,15 @@ module Redmine
end
end
def scm_cmd(*args, &block)
repo_path = root_url || url
full_args = [GIT_BIN, '--git-dir', repo_path]
private
# returns the string that will represent the command for shelling out
def build_scm_cmd(args)
full_args = [GIT_BIN, '--git-dir', root_url || url ]
if self.class.client_version_above?([1, 7, 2])
full_args << '-c' << 'core.quotepath=false'
end
full_args += args
ret = shellout(full_args.map { |e| shell_quote e.to_s }.join(' '), &block)
if $? && $?.exitstatus != 0
raise ScmCommandAborted, "git exited with non-zero status: #{$?.exitstatus}"
end
ret
(full_args + args).map { |e| shell_quote e.to_s }.join(' ')
end
private :scm_cmd
end
end
end
......
......@@ -12,7 +12,7 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'redmine/scm/adapters/abstract_adapter'
require_dependency 'redmine/scm/adapters/abstract_adapter'
require 'uri'
module Redmine
......@@ -57,47 +57,37 @@ module Redmine
# Get info about the svn repository
def info
cmd = "#{self.class.sq_bin} info --xml #{target}"
cmd << credentials_string
info = nil
shellout(cmd) do |io|
cmd_args = ['info','--xml', target, credentials_string]
scm_cmd(cmd_args) do |io|
output = io.read
if output.respond_to?(:force_encoding)
output.force_encoding('UTF-8')
end
begin
doc = ActiveSupport::XmlMini.parse(output)
#root_url = doc.elements["info/entry/repository/root"].text
info = Info.new({:root_url => doc['info']['entry']['repository']['root']['__content__'],
Info.new({
:root_url => doc['info']['entry']['repository']['root']['__content__'],
:lastrev => Revision.new({
:identifier => doc['info']['entry']['commit']['revision'],
:time => Time.parse(doc['info']['entry']['commit']['date']['__content__']).localtime,
:author => (doc['info']['entry']['commit']['author'] ? doc['info']['entry']['commit']['author']['__content__'] : "")
})
})
rescue
end
end
return nil if $? && $?.exitstatus != 0
info
rescue CommandFailed
return nil
end
# Returns an Entries collection
# or nil if the given path doesn't exist in the repository
def entries(path=nil, identifier=nil)
path ||= ''
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
identifier = initialize_identifier(identifier)
entries = Entries.new
cmd = "#{self.class.sq_bin} list --xml #{target(path)}@#{identifier}"
cmd << credentials_string
shellout(cmd) do |io|
cmd_args = ['list', '--xml', "#{target(path)}@#{identifier}", credentials_string]
scm_cmd(cmd_args) do |io|
output = io.read
if output.respond_to?(:force_encoding)
output.force_encoding('UTF-8')
end
begin
doc = ActiveSupport::XmlMini.parse(output)
each_xml_element(doc['lists']['list'], 'entry') do |entry|
commit = entry['commit']
......@@ -117,67 +107,61 @@ module Redmine
})
})
end
rescue Exception => e
logger.error("Error parsing svn output: #{e.message}")
logger.error("Output was:\n #{output}")
end
end
return nil if $? && $?.exitstatus != 0
logger.debug("Found #{entries.size} entries in the repository for #{target(path)}") if logger && logger.debug?
entries.sort_by_name
end
end
def properties(path, identifier=nil)
# proplist xml output supported in svn 1.5.0 and higher
return nil unless self.class.client_version_above?([1, 5, 0])
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} proplist --verbose --xml #{target(path)}@#{identifier}"
cmd << credentials_string
identifier = initialize_identifier(identifier)
cmd_args = ['proplist', '--verbose', '--xml', "#{target(path)}@#{identifier}", credentials_string]
properties = {}
shellout(cmd) do |io|
scm_cmd(cmd_args) do |io|
output = io.read
if output.respond_to?(:force_encoding)
output.force_encoding('UTF-8')
end
begin
doc = ActiveSupport::XmlMini.parse(output)
each_xml_element(doc['properties']['target'], 'property') do |property|
properties[ property['name'] ] = property['__content__'].to_s
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
properties
end
end
def revisions(path=nil, identifier_from=nil, identifier_to=nil, options={})
path ||= ''
identifier_from = (identifier_from && identifier_from.to_i > 0) ? identifier_from.to_i : "HEAD"
identifier_to = (identifier_to && identifier_to.to_i > 0) ? identifier_to.to_i : 1
identifier_from = initialize_identifier(identifier_from)
identifier_to = initialize_identifier(identifier_to, 1)
cmd_args = ['log', '--xml', '-r', "#{identifier_from}:#{identifier_to}", credentials_string]
cmd_args << " --verbose " if options[:with_paths]
cmd_args << " --limit #{options[:limit].to_i}" if options[:limit]
cmd_args << target(path)
revisions = Revisions.new
cmd = "#{self.class.sq_bin} log --xml -r #{identifier_from}:#{identifier_to}"
cmd << credentials_string
cmd << " --verbose " if options[:with_paths]
cmd << " --limit #{options[:limit].to_i}" if options[:limit]
cmd << ' ' + target(path)
shellout(cmd) do |io|
scm_cmd(cmd_args) do |io|
output = io.read
if output.respond_to?(:force_encoding)
output.force_encoding('UTF-8')
end
begin
doc = ActiveSupport::XmlMini.parse(output)
each_xml_element(doc['log'], 'logentry') do |logentry|
paths = []
if logentry['paths'] && logentry['paths']['path']
each_xml_element(logentry['paths'], 'path') do |path|
paths << {:action => path['action'],
:path => path['__content__'],
:from_path => path['copyfrom-path'],
:from_revision => path['copyfrom-rev']
}
end if logentry['paths'] && logentry['paths']['path']
end
end
paths.sort! { |x,y| x[:path] <=> y[:path] }
revisions << Revision.new({:identifier => logentry['revision'],
......@@ -187,64 +171,64 @@ module Redmine
:paths => paths
})
end
rescue
end
end
return nil if $? && $?.exitstatus != 0
revisions
end
end
def diff(path, identifier_from, identifier_to=nil, type="inline")
path ||= ''
identifier_from = (identifier_from and identifier_from.to_i > 0) ? identifier_from.to_i : ''
identifier_to = (identifier_to and identifier_to.to_i > 0) ? identifier_to.to_i : (identifier_from.to_i - 1)
identifier_from = initialize_identifier(identifier_from, '')
identifier_to = initialize_identifier(identifier_to, identifier_from.to_i - 1)
cmd = "#{self.class.sq_bin} diff -r "
cmd << "#{identifier_to}:"
cmd << "#{identifier_from}"
cmd << " #{target(path)}@#{identifier_from}"
cmd << credentials_string
cmd_args = ["diff -r",
"#{identifier_to}:#{identifier_from}",
"#{target(path)}@#{identifier_from}",
credentials_string]
diff = []
shellout(cmd) do |io|
scm_cmd(cmd_args) do |io|
io.each_line do |line|
diff << line
end
end
return nil if $? && $?.exitstatus != 0
diff
end
end
def cat(path, identifier=nil)
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} cat #{target(path)}@#{identifier}"
cmd << credentials_string
cat = nil
shellout(cmd) do |io|
identifier = initialize_identifier(identifier)
cmd_args = ['cat', "#{target(path)}@#{identifier}", credentials_string]
scm_cmd(cmd_args) do |io|
io.binmode
cat = io.read
io.read
end
return nil if $? && $?.exitstatus != 0
cat
end
def save_entry_in_file(f, path, identifier)
identifier = initialize_identifier(identifier)
cmd_args = ['cat', "#{target(path)}@#{identifier}", credentials_string]
scm_cmd(cmd_args, f.path)
end
def annotate(path, identifier=nil)
identifier = (identifier and identifier.to_i > 0) ? identifier.to_i : "HEAD"
cmd = "#{self.class.sq_bin} blame #{target(path)}@#{identifier}"
cmd << credentials_string
identifier = initialize_identifier(identifier)
cmd_args = ['blame', "#{target(path)}@#{identifier}", credentials_string]
blame = Annotate.new
shellout(cmd) do |io|
scm_cmd(cmd_args) do |io|
io.each_line do |line|
next unless line =~ %r{^\s*(\d+)\s*(\S+)\s(.*)$}
blame.add_line($3.rstrip, Revision.new(:identifier => $1.to_i, :author => $2.strip))
end
end
return nil if $? && $?.exitstatus != 0
blame
end
end
private
def initialize_identifier(identifier, default="HEAD")
(identifier && identifier.to_i > 0) ? identifier.to_i : default
end
def credentials_string
str = ''
str << " --username #{shell_quote(@login)}" unless @login.blank?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment