Sha256: 217e99f57833fe2db6153ad87bc3041208bc98eaf1cf473dd2cd6e5fbe86de80

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

require 'rugged'

module Technologist
  class GitRepository
    attr_reader :repository

    def initialize(repository_path)
      @repository = Rugged::Repository.new(repository_path)
    end

    def root_tree
      repository.head.target.tree
    end

    # Returns the file content.
    #
    # @param file_name [String] the file name
    #
    # @return [String] The content of the file or nil if the file cannot be found.
    def file_content(file_name)
      file = find_blob(file_name)

      file.content if file
    end

    # Recursively searches for the blob identified by `blob_name`
    # in all subdirectories in the repository. A blob is usually either
    # a file or a directory.
    #
    # @param blob_name [String] the blob name
    # @param current_tree [Rugged::Tree] the git directory tree in which to look for the blob.
    #   Defaults to the root tree (see `#root_tree`).
    #
    # @return [Rugged::Blob] The blob blob or nil if it cannot be found.
    def find_blob(blob_name, current_tree = root_tree)
      blob = current_tree[blob_name]

      if blob
        repository.lookup(blob[:oid])
      else
        current_tree.each_tree do |sub_tree|
          blob = find_blob(blob_name, repository.lookup(sub_tree[:oid]))
          break blob if blob
        end
      end
    end

    # Looks for a directory and returns true when the directory
    # can be found.
    #
    # @param directory_name [String] the directory name
    #
    # @return [Boolean] true if the directory can be found.
    def directory_exists?(directory_name)
      !!find_blob(directory_name)
    end

    # Looks for a file and returns true when the file
    # can be found.
    #
    # @param file_name [String] the file name
    #
    # @return [Boolean] true if the file can be found.
    def file_exists?(file_name)
      !!find_blob(file_name)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
technologist-0.3.0 lib/technologist/git_repository.rb
technologist-0.2.1 lib/technologist/git_repository.rb
technologist-0.2.0 lib/technologist/git_repository.rb