Class: Grit::GitRuby::DirectoryEntry

Inherits:
Object
  • Object
show all
Defined in:
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


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

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

Instance Attribute Details

#modeObject

Returns the value of attribute mode


115
116
117
# File 'lib/grit/git-ruby/git_object.rb', line 115

def mode
  @mode
end

#nameObject

Returns the value of attribute name


115
116
117
# File 'lib/grit/git-ruby/git_object.rb', line 115

def name
  @name
end

#sha1Object

Returns the value of attribute sha1


115
116
117
# File 'lib/grit/git-ruby/git_object.rb', line 115

def sha1
  @sha1
end

Instance Method Details

#format_modeObject


171
172
173
# File 'lib/grit/git-ruby/git_object.rb', line 171

def format_mode
  "%06o" % @mode
end

#format_typeObject


158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/grit/git-ruby/git_object.rb', line 158

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

#rawObject


175
176
177
# File 'lib/grit/git-ruby/git_object.rb', line 175

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

#typeObject


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

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) ⇒ Object


143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/grit/git-ruby/git_object.rb', line 143

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