Sha256: a0d45e7db4ddd81f4f39f33a149829ac3edf862444409ae94c4a7ae3b1d3c88a

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

require 'linguist/blob_helper'
require 'linguist/language'
require 'rugged'

module Linguist
  class LazyBlob
    GIT_ATTR = ['linguist-documentation',
                'linguist-language',
                'linguist-vendored',
                'linguist-generated']

    GIT_ATTR_OPTS = { :priority => [:index], :skip_system => true }
    GIT_ATTR_FLAGS = Rugged::Repository::Attributes.parse_opts(GIT_ATTR_OPTS)

    include BlobHelper

    MAX_SIZE = 128 * 1024

    attr_reader :repository
    attr_reader :oid
    attr_reader :path
    attr_reader :mode

    alias :name :path

    def initialize(repo, oid, path, mode = nil)
      @repository = repo
      @oid = oid
      @path = path
      @mode = mode
    end

    def git_attributes
      @git_attributes ||= repository.fetch_attributes(
        name, GIT_ATTR, GIT_ATTR_FLAGS)
    end

    def documentation?
      if attr = git_attributes['linguist-documentation']
        boolean_attribute(attr)
      else
        super
      end
    end

    def generated?
      if attr = git_attributes['linguist-generated']
        boolean_attribute(attr)
      else
        super
      end
    end

    def vendored?
      if attr = git_attributes['linguist-vendored']
        return boolean_attribute(attr)
      else
        super
      end
    end

    def language
      return @language if defined?(@language)

      @language = if lang = git_attributes['linguist-language']
        Language.find_by_alias(lang)
      else
        super
      end
    end

    def data
      load_blob!
      @data
    end

    def size
      load_blob!
      @size
    end

    def cleanup!
      @data.clear if @data
    end

    protected

    # Returns true if the attribute is present and not the string "false".
    def boolean_attribute(attribute)
      attribute != "false"
    end

    def load_blob!
      @data, @size = Rugged::Blob.to_buffer(repository, oid, MAX_SIZE) if @data.nil?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
github-linguist-4.8.0 lib/linguist/lazy_blob.rb
github-linguist-4.7.6 lib/linguist/lazy_blob.rb
github-linguist-4.7.5 lib/linguist/lazy_blob.rb
github-linguist-4.7.4 lib/linguist/lazy_blob.rb