lib/technologist/git_repository.rb in technologist-0.1.0 vs lib/technologist/git_repository.rb in technologist-0.2.0

- old
+ new

@@ -16,32 +16,53 @@ # # @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_file(file_name) + file = find_blob(file_name) file.content if file end - # Recursively searches for the file identified by `file_name` - # in all subdirectories in the repository. + # 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 file_name [String] the file name - # @param current_tree [Rugged::Tree] the git directory tree in which to look for the file. + # @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 file blob or nil if it cannot be found. - def find_file(file_name, current_tree = root_tree) - file = current_tree[file_name] + # @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 file - repository.lookup(file[:oid]) + if blob + repository.lookup(blob[:oid]) else current_tree.each_tree do |sub_tree| - file = find_file(file_name, repository.lookup(sub_tree[:oid])) - break file if file + 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