Class: Grit::GitRuby::DirectoryEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/git-ruby/object.rb,
lib/grit/git-ruby/git_object.rb

Constant Summary collapse

S_IFMT =
00170000
S_IFLNK =
0120000
S_IFREG =
0100000
S_IFDIR =
0040000
0160000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, filename, sha1o) ⇒ DirectoryEntry

Returns a new instance of DirectoryEntry.



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/grit/git-ruby/object.rb', line 105

def initialize(mode, filename, sha1o)
  @mode = 0
  mode.each_byte do |i|
    @mode = (@mode << 3) | (i-'0'[0])
  end
  @name = filename
  @sha1 = sha1o
  if ![S_IFLNK, S_IFDIR, S_IFREG].include?(@mode & S_IFMT)
    raise RuntimeError, "unknown type for directory entry"
  end
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



104
105
106
# File 'lib/grit/git-ruby/object.rb', line 104

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



104
105
106
# File 'lib/grit/git-ruby/object.rb', line 104

def name
  @name
end

#sha1Object

Returns the value of attribute sha1.



104
105
106
# File 'lib/grit/git-ruby/object.rb', line 104

def sha1
  @sha1
end

Instance Method Details

#format_modeObject



154
155
156
# File 'lib/grit/git-ruby/object.rb', line 154

def format_mode
  "%06o" % @mode
end

#format_typeObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/grit/git-ruby/object.rb', line 143

def format_type
  case type
  when :link
    'link'
  when :directory
    'tree'
  when :file
    'blob'
  end
end

#rawObject



158
159
160
# File 'lib/grit/git-ruby/object.rb', line 158

def raw
  "%o %s\0%s" % [@mode, @name, [@sha1].pack("H*")]
end

#typeObject



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/grit/git-ruby/object.rb', line 117

def type
  case @mode & S_IFMT
  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) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/grit/git-ruby/object.rb', line 130

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
  else
    raise RuntimeError, "invalid type"
  end
end