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

Returns a new instance of 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



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

def format_mode
  "%06o" % @mode
end

#format_typeObject



163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/grit/git-ruby/git_object.rb', line 163

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

#rawObject



180
181
182
# File 'lib/grit/git-ruby/git_object.rb', line 180

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

#safe_nameObject

Filenames can have weird characters that throw grit’s text parsing



129
130
131
# File 'lib/grit/git-ruby/git_object.rb', line 129

def safe_name
  name.gsub(/[\r\n\0]/, '')
end

#typeObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/grit/git-ruby/git_object.rb', line 133

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



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/grit/git-ruby/git_object.rb', line 148

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