Class: RJGit::Blob

Inherits:
Object
  • Object
show all
Defined in:
lib/blob.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository, mode, path, jblob) ⇒ Blob

Returns a new instance of Blob.


12
13
14
15
16
17
18
19
# File 'lib/blob.rb', line 12

def initialize(repository, mode, path, jblob)
  @jrepo = RJGit.repository_type(repository)
  @jblob = jblob
  @path = path
  @name = @path ? File.basename(@path) : nil
  @mode = mode
  @id = ObjectId.toString(jblob.get_id)
end

Instance Attribute Details

#idObject (readonly) Also known as: get_name

Returns the value of attribute id


8
9
10
# File 'lib/blob.rb', line 8

def id
  @id
end

#jblobObject (readonly)

Returns the value of attribute jblob


8
9
10
# File 'lib/blob.rb', line 8

def jblob
  @jblob
end

#modeObject (readonly)

Returns the value of attribute mode


8
9
10
# File 'lib/blob.rb', line 8

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name


8
9
10
# File 'lib/blob.rb', line 8

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path


8
9
10
# File 'lib/blob.rb', line 8

def path
  @path
end

Class Method Details

.find_blob(repository, file_path, revstring = Constants::HEAD) ⇒ Object

Finds a particular Blob in repository matching file_path


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/blob.rb', line 73

def self.find_blob(repository, file_path, revstring=Constants::HEAD)
  jrepo = RJGit.repository_type(repository)
  last_commit_hash = jrepo.resolve(revstring)
  return nil if last_commit_hash.nil?

  walk = RevWalk.new(jrepo)
  jcommit = walk.parse_commit(last_commit_hash)
  treewalk = TreeWalk.new(jrepo)
  jtree = jcommit.get_tree
  treewalk.add_tree(jtree)
  treewalk.set_recursive(true)
  treewalk.set_filter(PathFilter.create(file_path))
  if treewalk.next
    jblob = walk.lookup_blob(treewalk.get_object_id(0))
    if jblob
      mode = RJGit.get_file_mode_with_path(jrepo, file_path, jtree) 
      Blob.new(jrepo, mode, file_path, jblob)
    end
  else
    nil
  end
end

.mime_type(filename) ⇒ Object


60
61
62
63
# File 'lib/blob.rb', line 60

def self.mime_type(filename)
  guesses = MIME::Types.type_for(filename) rescue []
  guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
end

.new_from_string(repository, contents) ⇒ Object


65
66
67
68
69
70
# File 'lib/blob.rb', line 65

def self.new_from_string(repository, contents)
  repository = RJGit.repository_type(repository)
  blob_id = RJGit::Plumbing::TreeBuilder.new(repository).write_blob(contents, true)
  walk = RevWalk.new(repository)
  Blob.new(repository, REG_FILE_TYPE, nil, walk.lookup_blob(blob_id))
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)

46
47
48
# File 'lib/blob.rb', line 46

def binary?
  RawText.is_binary(self.data.to_java_bytes)
end

#blame(options = {}) ⇒ Object


32
33
34
# File 'lib/blob.rb', line 32

def blame(options={})
  @blame ||= RJGit::Porcelain.blame(@jrepo, @path, options)
end

#bytesizeObject

The size of this blob in bytes

Returns Integer


24
25
26
# File 'lib/blob.rb', line 24

def bytesize
  @bytesize ||= @jrepo.open(@jblob).get_size 
end

#dataObject

The binary contents of this blob. Returns String


38
39
40
# File 'lib/blob.rb', line 38

def data
  @data ||= RJGit::Porcelain.cat_file(@jrepo, @jblob) 
end

#is_symlink?Boolean

Returns:

  • (Boolean)

42
43
44
# File 'lib/blob.rb', line 42

def is_symlink?
  @mode == SYMLINK_TYPE
end

#line_countObject


50
51
52
# File 'lib/blob.rb', line 50

def line_count
  self.binary? ? 0 : self.data.split("\n").size
end

#mime_typeObject

The mime type of this file (based on the filename) Returns String


56
57
58
# File 'lib/blob.rb', line 56

def mime_type
  Blob.mime_type(self.name)
end

#sizeObject


28
29
30
# File 'lib/blob.rb', line 28

def size
  @size ||= bytesize
end