class GitObjectBrowser::Models::GitObject

Attributes

contents[R]
entries[R]
message[R]
properties[R]
sha1[R]
size[R]
type[R]

Public Class Methods

new(input) click to toggle source
Calls superclass method GitObjectBrowser::Models::Bindata::new
# File lib/git-object-browser/models/git_object.rb, line 10
def initialize(input)
  super(input)
end
path?(relpath) click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 57
def self.path?(relpath)
  relpath =~ %r{\Aobjects/[0-9a-f]{2}/[0-9a-f]{38}\z}
end

Public Instance Methods

parse() click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 14
def parse
  content = Zlib::Inflate.inflate(@in.read(nil))
  parse_inflated(content)
  self
end
parse_inflated(content) click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 20
def parse_inflated(content)
  @sha1 = Digest::SHA1.hexdigest(content)
  @in   = StringIO.new(content)

  @type = find_char ' '
  @size = find_char "\0"

  @type = type
  @size = size

  if @type == 'tree'
    @entries = parse_tree_entries
  else
    @content = @in.read(nil)
    if @type == 'commit' or @type == 'tag'
      (@properties, @message) = parse_contents
    end

    @content = force_utf8(@content)
    @content = @content[0, 3000] + "\n..." if @content.length > 3000
  end

  self
end
to_hash() click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 45
def to_hash
  return {
    :type       => @type,
    :sha1       => @sha1,
    :size       => @size,
    :entries    => @entries,
    :content    => @content,
    :properties => @properties,
    :message    => @message
  }
end

Private Instance Methods

epoch(sec, timezone) click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 113
def epoch(sec, timezone)
  DateTime.strptime(sec.to_s, '%s').new_offset(parse_timezone(timezone))
end
force_utf8(str) click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 123
def force_utf8(str)
  str = str.force_encoding('UTF-8')
  str.valid_encoding? ? str : '(not UTF-8)'
end
parse_contents() click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 79
def parse_contents
  lines = @content.split /\n/
  line = ''
  properties = []
  message = ''
  while ! lines.empty?
    line = lines.shift
    break if line.empty?
    prop = {}
    (prop[:key], prop[:value]) = line.split(/ /, 2)
    if prop[:value] =~ /\A([0-9a-f]{2})([0-9a-f]{38})\z/
      prop[:type] = 'sha1'
      prop[:path] = "objects/#{ $1 }/#{ $2 }"
    elsif %w{author committer tagger}.include?(prop[:key]) &&
        # couldn't find the spec...
        prop[:value].to_s =~ /\A(.*) <(.*)> (\d+)(?: ((?:(?:\+|-)(?:\d{4}|\d{2}:\d{2}))|Z))?\z/
      prop[:type]  = 'user'
      prop[:name]  = force_utf8($1)
      prop[:email] = force_utf8($2)
      prop[:unixtime] = $3
      prop[:timezone] = $4
      prop[:date] = GitDate.new($3, $4).to_s

      prop[:value] = force_utf8(prop[:value])
    else
      prop[:type] = 'text'
    end
    properties << prop
  end
  message = force_utf8(lines.join("\n"))

  [properties, message]
end
parse_timezone(timezone) click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 117
def parse_timezone(timezone)
  timezone = '+00:00' if timezone == 'Z'
  return Rational(0, 24) unless timezone =~ /(\+|-)?(\d\d):?(\d\d)/
  Rational($2.to_i, 24) + Rational($3, 60) * (($1 == '-') ? -1 : 1)
end
parse_tree_entries() click to toggle source
# File lib/git-object-browser/models/git_object.rb, line 64
def parse_tree_entries
  @content = ''
  entries = []
  loop do
    entry = {}
    entry[:mode]     = find_char ' '
    break if entry[:mode].empty?
    entry[:filename] = force_utf8(find_char("\0"))
    entry[:sha1]     = hex(20)
    @content += "#{entry[:mode]} #{entry[:filename]}\\0\\#{entry[:sha1]}\n"
    entries << entry
  end
  return entries
end