Class: Bringit::Blob
- Inherits:
-
Object
- Object
- Bringit::Blob
- 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
-
#binary ⇒ Object
Returns the value of attribute binary.
-
#commit_id ⇒ Object
Returns the value of attribute commit_id.
-
#data ⇒ Object
Returns the value of attribute data.
-
#id ⇒ Object
Returns the value of attribute id.
-
#loaded_size ⇒ Object
Returns the value of attribute loaded_size.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path ⇒ Object
Returns the value of attribute path.
-
#repository ⇒ Object
readonly
Returns the value of attribute repository.
-
#size ⇒ Object
Returns the value of attribute size.
Class Method Summary collapse
- .find(repository, sha, path) ⇒ Object
-
.find_entry_by_path(repository, root_id, path) ⇒ Object
Recursive search of blob id by path.
- .raw(repository, sha) ⇒ Object
- .submodule_blob(repository, blob_entry, path, sha) ⇒ Object
Instance Method Summary collapse
- #binary? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(repository, options) ⇒ Blob
constructor
A new instance of Blob.
- #lfs_oid ⇒ Object
-
#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.
- #lfs_size ⇒ Object
-
#load_all_data! ⇒ Object
Load all blob data (not just the first MAX_DATA_DISPLAY_SIZE bytes) into memory as a Ruby string.
- #truncated? ⇒ Boolean
Methods included from EncodingHelper
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, ) @repository = repository %w(id name path size data mode commit_id binary).each do |key| self.send("#{key}=", [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
#binary ⇒ Object
Returns the value of attribute binary.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def binary @binary end |
#commit_id ⇒ Object
Returns the value of attribute commit_id.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def commit_id @commit_id end |
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def data @data end |
#id ⇒ Object
Returns the value of attribute id.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def id @id end |
#loaded_size ⇒ Object
Returns the value of attribute loaded_size.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def loaded_size @loaded_size end |
#mode ⇒ Object
Returns the value of attribute mode.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def mode @mode end |
#name ⇒ Object
Returns the value of attribute name.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def name @name end |
#path ⇒ Object
Returns the value of attribute path.
13 14 15 |
# File 'lib/bringit/blob.rb', line 13 def path @path end |
#repository ⇒ Object (readonly)
Returns the value of attribute repository.
12 13 14 |
# File 'lib/bringit/blob.rb', line 12 def repository @repository end |
#size ⇒ Object
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
111 112 113 |
# File 'lib/bringit/blob.rb', line 111 def binary? @binary.nil? ? super : @binary == true end |
#empty? ⇒ Boolean
115 116 117 |
# File 'lib/bringit/blob.rb', line 115 def empty? !data || data == '' end |
#lfs_oid ⇒ Object
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
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_size ⇒ Object
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
165 166 167 |
# File 'lib/bringit/blob.rb', line 165 def truncated? size && (size > loaded_size) end |