Class: Bringit::Blob

Inherits:
Object
  • Object
show all
Includes:
EncodingHelper, Linguist::BlobHelper
Defined in:
lib/bringit/blob.rb

Constant Summary collapse

MAX_DATA_DISPLAY_SIZE =

This number is the maximum amount of data that we want to display to the user. We load as much as we can for encoding detection (Linguist) and LFS pointer parsing. All other cases where we need full blob data should use load_all_data!.

10485760

Constants included from EncodingHelper

EncodingHelper::ENCODING_CONFIDENCE_THRESHOLD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EncodingHelper

#encode!, #encode_utf8

Constructor Details

#initialize(repository, options) ⇒ Blob

Returns a new instance of Blob.



100
101
102
103
104
105
106
107
108
109
# File 'lib/bringit/blob.rb', line 100

def initialize(repository, options)
  @repository = repository
  %w(id name path size data mode commit_id binary).each do |key|
    self.send("#{key}=", options[key.to_sym])
  end

  @loaded_all_data = false
  # Retain the actual size before it is encoded
  @loaded_size = @data.bytesize if @data
end

Instance Attribute Details

#binaryObject

Returns the value of attribute binary.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def binary
  @binary
end

#commit_idObject

Returns the value of attribute commit_id.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def commit_id
  @commit_id
end

#dataObject

Returns the value of attribute data.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def data
  @data
end

#idObject

Returns the value of attribute id.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def id
  @id
end

#loaded_sizeObject

Returns the value of attribute loaded_size.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def loaded_size
  @loaded_size
end

#modeObject

Returns the value of attribute mode.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def name
  @name
end

#pathObject

Returns the value of attribute path.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def path
  @path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



12
13
14
# File 'lib/bringit/blob.rb', line 12

def repository
  @repository
end

#sizeObject

Returns the value of attribute size.



13
14
15
# File 'lib/bringit/blob.rb', line 13

def size
  @size
end

Class Method Details

.find(repository, sha, path) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bringit/blob.rb', line 16

def find(repository, sha, path)
  path = path&.sub(%r{\A/*}, '')
  commit = repository.lookup(sha)
  root_tree = commit.tree

  blob_entry = find_entry_by_path(repository, root_tree.oid, path)

  return nil unless blob_entry

  if blob_entry[:type] == :commit
    submodule_blob(repository, blob_entry, path, sha)
  else
    blob = repository.lookup(blob_entry[:oid])

    if blob
      new(repository,
        id: blob.oid,
        name: blob_entry[:name],
        size: blob.size,
        data: blob.content(MAX_DATA_DISPLAY_SIZE),
        mode: blob_entry[:filemode].to_s(8),
        path: Bringit::PathHelper.normalize_path(path).to_s,
        commit_id: sha,
        binary: blob.binary?
      )
    end
  end
end

.find_entry_by_path(repository, root_id, path) ⇒ Object

Recursive search of blob id by path

Ex.

blog/            # oid: 1a
  app/           # oid: 2a
    models/      # oid: 3a
    file.rb      # oid: 4a

Blob.find_entry_by_path(repo, ‘1a’, ‘app/file.rb’) # => ‘4a’



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bringit/blob.rb', line 67

def find_entry_by_path(repository, root_id, path)
  root_tree = repository.lookup(root_id)
  # Strip leading slashes
  path = path.sub(%r{\A/*}, '')
  path_arr = path.split('/')

  entry = root_tree.find do |entry|
    entry[:name] == path_arr[0]
  end

  return nil unless entry

  if path_arr.size > 1
    return nil unless entry[:type] == :tree
    path_arr.shift
    find_entry_by_path(repository, entry[:oid], path_arr.join('/'))
  else
    [:blob, :commit].include?(entry[:type]) ? entry : nil
  end
end

.raw(repository, sha) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/bringit/blob.rb', line 45

def raw(repository, sha)
  blob = repository.lookup(sha)

  new(repository,
    id: blob.oid,
    size: blob.size,
    data: blob.content(MAX_DATA_DISPLAY_SIZE),
    binary: blob.binary?
  )
end

.submodule_blob(repository, blob_entry, path, sha) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/bringit/blob.rb', line 88

def submodule_blob(repository, blob_entry, path, sha)
  path = path&.sub(%r{\A/*}, '')
  new(repository,
    id: blob_entry[:oid],
    name: blob_entry[:name],
    data: '',
    path: path,
    commit_id: sha,
  )
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/bringit/blob.rb', line 111

def binary?
  @binary.nil? ? super : @binary == true
end

#empty?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/bringit/blob.rb', line 115

def empty?
  !data || data == ''
end

#lfs_oidObject



147
148
149
150
151
152
153
154
# File 'lib/bringit/blob.rb', line 147

def lfs_oid
  if has_lfs_version_key?
    oid = data.match(/(?<=sha256:)([0-9a-f]{64})/)
    return oid[1] if oid
  end

  nil
end

#lfs_pointer?Boolean

Valid LFS object pointer is a text file consisting of version oid size see github.com/github/git-lfs/blob/v1.1.0/docs/spec.md#the-pointer

Returns:

  • (Boolean)


143
144
145
# File 'lib/bringit/blob.rb', line 143

def lfs_pointer?
  has_lfs_version_key? && lfs_oid.present? && lfs_size.present?
end

#lfs_sizeObject



156
157
158
159
160
161
162
163
# File 'lib/bringit/blob.rb', line 156

def lfs_size
  if has_lfs_version_key?
    size = data.match(/(?<=size )([0-9]+)/)
    return size[1] if size
  end

  nil
end

#load_all_data!Object

Load all blob data (not just the first MAX_DATA_DISPLAY_SIZE bytes) into memory as a Ruby string.



125
126
127
128
129
130
131
132
# File 'lib/bringit/blob.rb', line 125

def load_all_data!
  return if @data == '' # don't mess with submodule blobs
  return @data if @loaded_all_data

  @loaded_all_data = true
  @data = repository.lookup(id).content
  @loaded_size = @data.bytesize
end

#truncated?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/bringit/blob.rb', line 165

def truncated?
  size && (size > loaded_size)
end