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.



54
55
56
# File 'lib/technologist/git_repository.rb', line 54

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

#file_content(file_name) ⇒ String

Returns the file content.

Parameters:

  • file_name (String)

    the file name

Returns:

  • (String)

    The content of the file or nil if the file cannot be found.



20
21
22
23
24
# File 'lib/technologist/git_repository.rb', line 20

def file_content(file_name)
  file = find_blob(file_name)

  file.content if file
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.



64
65
66
# File 'lib/technologist/git_repository.rb', line 64

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

#find_blob(blob_name, current_tree = root_tree) ⇒ 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).

Returns:

  • (Rugged::Blob)

    The blob blob or nil if it cannot be found.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/technologist/git_repository.rb', line 35

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

#root_treeObject



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

def root_tree
  repository.head.target.tree
end