Class: Toys::Utils::GitCache

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/utils/git_cache.rb

Overview

This object provides cached access to remote git data. Given a remote repository, a path, and a commit, it makes the files availble in the local filesystem. Access is cached, so repeated requests do not hit the remote repository again.

This class is used by the Loader to load tools from git. Tools can also use the :git_cache mixin for direct access to this class.

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache_dir: nil) ⇒ GitCache

Access a git cache.

Parameters:

  • cache_dir (String) (defaults to: nil)

    The path to the cache directory. Defaults to a specific directory in the user's XDG cache.



51
52
53
54
# File 'lib/toys/utils/git_cache.rb', line 51

def initialize(cache_dir: nil)
  @cache_dir = ::File.expand_path(cache_dir || default_cache_dir)
  @exec = Utils::Exec.new(out: :capture, err: :capture)
end

Instance Attribute Details

#cache_dirString (readonly)

The cache directory.

Returns:

  • (String)


86
87
88
# File 'lib/toys/utils/git_cache.rb', line 86

def cache_dir
  @cache_dir
end

Instance Method Details

#find(remote, path: nil, commit: nil, update: false) ⇒ String

Find the given git-based files from the git cache, loading from the remote repo if necessary.

Parameters:

  • remote (String)

    The URL of the git repo. Required.

  • path (String) (defaults to: nil)

    The path to the file or directory within the repo. Optional. Defaults to the entire repo.

  • commit (String) (defaults to: nil)

    The commit reference, which may be a SHA or any git ref such as a branch or tag. Optional. Defaults to HEAD.

  • update (Boolean) (defaults to: false)

    Force update of non-SHA commit references, even if it has previously been loaded.

Returns:

  • (String)

    The full path to the cached files.



70
71
72
73
74
75
76
77
78
79
# File 'lib/toys/utils/git_cache.rb', line 70

def find(remote, path: nil, commit: nil, update: false)
  path ||= ""
  commit ||= "HEAD"
  dir = ensure_dir(remote)
  lock_repo(dir) do
    ensure_repo(dir, remote)
    sha = ensure_commit(dir, commit, update)
    ensure_source(dir, sha, path.to_s)
  end
end