Class: Technologist::GitRepository

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository_path) ⇒ GitRepository

Returns a new instance of GitRepository.



7
8
9
# File 'lib/technologist/git_repository.rb', line 7

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

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



5
6
7
# File 'lib/technologist/git_repository.rb', line 5

def repository
  @repository
end

Instance Method Details

#directory_exists?(directory_name) ⇒ Boolean

Looks for a directory and returns true when the directory can be found.

Parameters:

  • directory_name (String)

    the directory name

Returns:

  • (Boolean)

    true if the directory can be found.



51
52
53
# File 'lib/technologist/git_repository.rb', line 51

def directory_exists?(directory_name)
  !!find_blob(directory_name)
end

#file_exists?(file_name) ⇒ Boolean

Looks for a file and returns true when the file can be found.

Parameters:

  • file_name (String)

    the file name

Returns:

  • (Boolean)

    true if the file can be found.



61
62
63
# File 'lib/technologist/git_repository.rb', line 61

def file_exists?(file_name)
  !!find_blob(file_name)
end

#file_with_content_exists?(file_name) ⇒ Boolean

Looks for a file and yields the file content to the block (if the file can be found). The block’s return value is used to determine if we’re done searching. If the return value is ‘false`, we keep searching for the file.

Parameters:

  • file_name (String)

    the file name

Returns:

  • (Boolean)

    true if the file can be found.



74
75
76
77
78
# File 'lib/technologist/git_repository.rb', line 74

def file_with_content_exists?(file_name)
  !!find_blob(file_name) do |file|
    yield file.content
  end
end

#find_blob(blob_name, current_tree = root_tree) {|Rugged::Blob| ... } ⇒ Rugged::Blob

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.

Parameters:

  • blob_name (String)

    the blob name

  • current_tree (Rugged::Tree) (defaults to: root_tree)

    the git directory tree in which to look for the blob. Defaults to the root tree (see ‘#root_tree`).

Yields:

  • (Rugged::Blob)

    Yields the found blob to an optional block. If the block returns ‘true` it means that the file is found and recursion is stopped. If the return value is `false`, the resursion continues.

Returns:

  • (Rugged::Blob)

    The blob blob or nil if it cannot be found.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/technologist/git_repository.rb', line 28

def find_blob(blob_name, current_tree = root_tree, &block)
  blob = current_tree[blob_name]

  if blob
    blob = repository.lookup(blob[:oid])
    if !block_given? || yield(blob)
      return blob
    end
  end

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

#root_treeObject



11
12
13
# File 'lib/technologist/git_repository.rb', line 11

def root_tree
  repository.head.target.tree
end