2012-03-22 17:11:12 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
2012-03-07 01:20:35 +00:00
|
|
|
module Grit
|
2013-03-21 18:37:22 +00:00
|
|
|
class Commit
|
|
|
|
|
|
|
|
# Fix: NoMethodError: undefined method 'touch' for Grit::Commit
|
|
|
|
# see: model Comment belongs_to :commentable
|
|
|
|
def touch
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-10 18:58:43 +01:00
|
|
|
class Submodule
|
|
|
|
def binary?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-07 01:20:35 +00:00
|
|
|
class Blob
|
2012-07-17 09:02:56 +01:00
|
|
|
include Linguist::BlobHelper
|
2012-03-07 01:20:35 +00:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
MAX_VIEW_SIZE = 2.megabytes
|
|
|
|
MAX_DATA_SIZE = 50.megabytes
|
|
|
|
|
|
|
|
def data_with_limit
|
2012-07-31 18:32:49 +01:00
|
|
|
!huge? ? data_without_limit : nil # 'Error: blob is too big'
|
2012-07-17 09:02:56 +01:00
|
|
|
end
|
|
|
|
alias_method_chain :data, :limit
|
|
|
|
|
|
|
|
def large?
|
|
|
|
size.to_i > MAX_VIEW_SIZE
|
|
|
|
end
|
|
|
|
|
2012-07-31 18:32:49 +01:00
|
|
|
def huge?
|
|
|
|
size.to_i > MAX_DATA_SIZE
|
|
|
|
end
|
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
def render_as
|
|
|
|
@render_as ||= case
|
|
|
|
when large?; :binary
|
|
|
|
when image?; :image
|
|
|
|
when text?; :text
|
|
|
|
else
|
|
|
|
:binary
|
|
|
|
end
|
|
|
|
end
|
2012-03-07 01:20:35 +00:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
# def file_mime_type
|
|
|
|
# @file_mime_type ||= data.file_type(:mime_type)
|
|
|
|
# end
|
2012-10-26 15:03:46 +01:00
|
|
|
#
|
2012-07-17 09:02:56 +01:00
|
|
|
# def text?
|
|
|
|
# file_mime_type =~ /^text\// # not binary?
|
|
|
|
# end
|
2012-10-26 15:03:46 +01:00
|
|
|
#
|
2012-07-17 09:02:56 +01:00
|
|
|
# def binary?
|
|
|
|
# not text? # file_mime_type !~ /^text\//
|
|
|
|
# # s = data.split(//); ((s.size - s.grep(" ".."~").size) / s.size.to_f) > 0.30 # works only for latin chars
|
|
|
|
# end
|
2012-10-26 15:03:46 +01:00
|
|
|
#
|
2012-07-17 09:02:56 +01:00
|
|
|
# def image?
|
|
|
|
# mime_type.match(/image/)
|
|
|
|
# end
|
|
|
|
|
|
|
|
DEFAULT_RAW_MIME_TYPE = MIME::Types[DEFAULT_MIME_TYPE].first
|
2012-03-07 01:20:35 +00:00
|
|
|
|
|
|
|
def mime_type_with_class_store
|
|
|
|
set_associated_mimes
|
|
|
|
@associated_mimes.first.simplified
|
|
|
|
end
|
|
|
|
alias_method_chain :mime_type, :class_store
|
|
|
|
|
|
|
|
attr_accessor :raw_mime_type
|
|
|
|
def raw_mime_type
|
|
|
|
set_associated_mimes
|
|
|
|
@raw_mime_type = @associated_mimes.first || DEFAULT_RAW_MIME_TYPE
|
|
|
|
@raw_mime_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_mime_types
|
|
|
|
set_associated_mimes
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
2012-07-26 22:15:57 +01:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
# store all associated MIME::Types inside class
|
|
|
|
def set_associated_mimes
|
|
|
|
@associated_mimes ||= []
|
|
|
|
if @associated_mimes.empty?
|
|
|
|
guesses = MIME::Types.type_for(self.name) rescue [DEFAULT_RAW_MIME_TYPE]
|
|
|
|
guesses = [DEFAULT_RAW_MIME_TYPE] if guesses.empty?
|
2012-03-07 01:20:35 +00:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
@associated_mimes = guesses.sort{|a,b| mime_sort(a, b)}
|
2012-03-07 13:36:39 +00:00
|
|
|
end
|
2012-07-17 09:02:56 +01:00
|
|
|
@associated_mimes
|
|
|
|
end
|
2012-03-07 13:36:39 +00:00
|
|
|
|
2012-07-17 09:02:56 +01:00
|
|
|
# TODO make more clever function
|
|
|
|
def mime_sort(a,b)
|
|
|
|
return 0 if a.media_type == b.media_type and a.registered? == b.registered?
|
|
|
|
return -1 if a.media_type == 'text' and !a.registered?
|
|
|
|
return 1
|
|
|
|
end
|
2012-03-07 01:20:35 +00:00
|
|
|
end
|
2012-07-26 22:24:14 +01:00
|
|
|
|
|
|
|
class Repo
|
|
|
|
def branches_and_tags
|
|
|
|
branches + tags # @branches_and_tags ||= # ???
|
|
|
|
end
|
2012-11-19 19:15:15 +00:00
|
|
|
|
|
|
|
def diff(a, b, *paths)
|
|
|
|
diff = self.git.native('diff', {:M => true}, "#{a}...#{b}", '--', *paths)
|
|
|
|
|
|
|
|
if diff =~ /diff --git a/
|
|
|
|
diff = diff.sub(/.*?(diff --git a)/m, '\1')
|
|
|
|
else
|
|
|
|
diff = ''
|
|
|
|
end
|
|
|
|
Diff.list_from_string(self, diff)
|
|
|
|
end
|
|
|
|
|
|
|
|
# The diff stats for the given treeish
|
|
|
|
# git diff --numstat -M a...b
|
|
|
|
#
|
|
|
|
# +a+ is the base treeish
|
|
|
|
# +b+ is the head treeish
|
|
|
|
#
|
|
|
|
# Returns Grit::DiffStat[]
|
|
|
|
def diff_stats(a,b)
|
|
|
|
stats = []
|
|
|
|
Dir.chdir(path) do
|
|
|
|
lines = self.git.native(:diff, {:numstat => true, :M => true}, "#{a}...#{b}").split("\n")
|
|
|
|
while !lines.empty?
|
|
|
|
files = []
|
|
|
|
while lines.first =~ /^([-\d]+)\s+([-\d]+)\s+(.+)/
|
|
|
|
additions, deletions, filename = lines.shift.gsub(' => ', '=>').split
|
|
|
|
additions, deletions = additions.to_i, deletions.to_i
|
|
|
|
stat = DiffStat.new filename, additions, deletions
|
|
|
|
stats << stat
|
|
|
|
end
|
|
|
|
end
|
|
|
|
stats
|
|
|
|
end
|
|
|
|
end
|
2012-07-26 22:24:14 +01:00
|
|
|
end
|
2012-03-07 01:20:35 +00:00
|
|
|
end
|
2012-03-29 21:34:22 +01:00
|
|
|
|
|
|
|
Grit::Git.git_timeout = 60
|
2012-07-17 09:02:56 +01:00
|
|
|
# Grit::Git.git_max_size = 5.megabytes
|
2012-03-29 21:34:22 +01:00
|
|
|
# Grit.debug = true
|
|
|
|
GAP_REPO_PATH = '/tmp/gap_repo.git'
|
|
|
|
unless File.directory? GAP_REPO_PATH
|
|
|
|
Grit::Repo.init_bare(GAP_REPO_PATH)
|
|
|
|
# FileUtils.chmod "a-w", GAP_REPO_PATH
|
|
|
|
end
|