class Grit::GitRuby::DirectoryEntry

Constants

S_IFDIR
S_IFLNK
S_IFMT
S_IFREG

Attributes

mode[RW]
name[RW]
sha1[RW]

Public Class Methods

new(mode, filename, sha1o) click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 115
def initialize(mode, filename, sha1o)
  @mode = 0
  mode.each_byte do |i|
    @mode = (@mode << 3) | (i-'0'.getord(0))
  end
  @name = filename
  @sha1 = sha1o
  if ![S_IFLNK, S_IFDIR, S_IFREG, S_IFGITLINK].include?(@mode & S_IFMT)
    raise RuntimeError, "unknown type for directory entry"
  end
end

Public Instance Methods

format_mode() click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 170
def format_mode
  "%06o" % @mode
end
format_type() click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 157
def format_type
  case type
  when :link
    'link'
  when :directory
    'tree'
  when :file
    'blob'
  when :submodule
    'commit'
  end
end
raw() click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 174
def raw
  "%o %s\00%%s" % [@mode, @name, [@sha1].pack("H*")]
end
type() click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 127
def type
  case @mode & S_IFMT
  when S_IFGITLINK
    @type = :submodule
  when S_IFLNK
    @type = :link
  when S_IFDIR
    @type = :directory
  when S_IFREG
    @type = :file
  else
    raise RuntimeError, "unknown type for directory entry"
  end
end
type=(type) click to toggle source
# File lib/grit/git-ruby/git_object.rb, line 142
def type=(type)
  case @type
  when :link
    @mode = (@mode & ~S_IFMT) | S_IFLNK
  when :directory
    @mode = (@mode & ~S_IFMT) | S_IFDIR
  when :file
    @mode = (@mode & ~S_IFMT) | S_IFREG
  when :submodule
    @mode = (@mode & ~S_IFMT) | S_IFGITLINK
  else
    raise RuntimeError, "invalid type"
  end
end