Class: GitCache
- Inherits:
-
Object
- Object
- GitCache
- Defined in:
- lib/git_cache.rb,
lib/git_cache/error.rb,
lib/git_cache/version.rb,
lib/git_cache/repo_info.rb,
lib/git_cache/repo_lock.rb
Overview
This object provides cached access to remote git data. Given a remote repository, a path, and a commit, it makes the files available in the local filesystem. Access is cached, so repeated requests for the same commit and path in the same repo do not hit the remote repository again.
Defined Under Namespace
Classes: Error, RefInfo, RepoInfo, SourceInfo
Constant Summary collapse
- VERSION =
Version of the git_cache gem
"0.1.1"
Instance Attribute Summary collapse
-
#cache_dir ⇒ String
readonly
The cache directory.
Instance Method Summary collapse
-
#get(remote, path: nil, commit: nil, into: nil, update: false, timestamp: nil) ⇒ String
(also: #find)
Get the given git-based files from the git cache, loading from the remote repo if necessary.
-
#initialize(cache_dir: nil) ⇒ GitCache
constructor
Access a git cache.
-
#remotes ⇒ Array<String>
Returns an array of the known remote names.
-
#remove_refs(remote, refs: nil) ⇒ Array<RefInfo>?
Remove records of the given refs (i.e. branches, tags, or
HEAD) from the given repository's cache. -
#remove_repos(remotes) ⇒ Array<String>
Removes caches for the given repos, or all repos if specified.
-
#remove_sources(remote, commits: nil) ⇒ Array<SourceInfo>?
Removes shared sources for the given cache.
-
#repo_info(remote) ⇒ RepoInfo?
Returns a RepoInfo describing the cache for the given remote, or
nilif the given remote has never been cached.
Constructor Details
#initialize(cache_dir: nil) ⇒ GitCache
Access a git cache.
20 21 22 23 24 25 26 27 |
# File 'lib/git_cache.rb', line 20 def initialize(cache_dir: nil) require "digest" require "fileutils" require "json" require "exec_service" @cache_dir = ::File.(cache_dir || default_cache_dir) @exec = ::ExecService.new(out: :capture, err: :capture) end |
Instance Attribute Details
#cache_dir ⇒ String (readonly)
The cache directory.
34 35 36 |
# File 'lib/git_cache.rb', line 34 def cache_dir @cache_dir end |
Instance Method Details
#get(remote, path: nil, commit: nil, into: nil, update: false, timestamp: nil) ⇒ String Also known as: find
Get the given git-based files from the git cache, loading from the remote repo if necessary.
The resulting files are either copied into a directory you provide in
the :into parameter, or populated into a shared source directory if
you omit the :into parameter. In the latter case, it is important
that you do not modify the returned files or directories, nor add or
remove any files from the directories returned, to avoid confusing
callers that could be given the same directory. If you need to make any
modifications to the returned files, use :into to provide your own
private directory.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/git_cache.rb', line 72 def get(remote, path: nil, commit: nil, into: nil, update: false, timestamp: nil) path = ::GitCache.normalize_path(path) commit ||= "HEAD" ||= ::Time.now.to_i dir = ensure_repo_base_dir(remote) lock_repo(dir, remote, ) do |repo_lock| ensure_repo(dir, remote) sha = ensure_commit(dir, commit, repo_lock, update) if into copy_files(dir, sha, path, repo_lock, into) else ensure_source(dir, sha, path, repo_lock) end end end |
#remotes ⇒ Array<String>
Returns an array of the known remote names.
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/git_cache.rb', line 94 def remotes result = [] return result unless ::File.directory?(cache_dir) ::Dir.entries(cache_dir).each do |child| next if child.start_with?(".") dir = ::File.join(cache_dir, child) if ::File.file?(::File.join(dir, LOCK_FILE_NAME)) remote = lock_repo(dir, &:remote) result << remote if remote end end result.sort end |
#remove_refs(remote, refs: nil) ⇒ Array<RefInfo>?
Remove records of the given refs (i.e. branches, tags, or HEAD) from
the given repository's cache. The next time those refs are requested,
they will be pulled from the remote repo.
If you provide the refs: argument, only those refs are removed.
Otherwise, all refs are removed.
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/git_cache.rb', line 163 def remove_refs(remote, refs: nil) dir = repo_base_dir_for(remote) return nil unless ::File.directory?(dir) results = [] lock_repo(dir, remote) do |repo_lock| refs = repo_lock.refs if refs.nil? || refs == :all Array(refs).each do |ref| ref_data = repo_lock.delete_ref!(ref) results << RefInfo.new(ref, ref_data) if ref_data end end results.sort end |
#remove_repos(remotes) ⇒ Array<String>
Removes caches for the given repos, or all repos if specified.
Removes all cache information for the specified repositories, including local clones and shared source directories. The next time these repositories are requested, they will be reloaded from the remote repository from scratch.
Be careful not to remove repos that are currently in use by other GitCache clients.
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/git_cache.rb', line 138 def remove_repos(remotes) remotes = self.remotes if remotes.nil? || remotes == :all Array(remotes).map do |remote| dir = repo_base_dir_for(remote) if ::File.directory?(dir) ::FileUtils.chmod_R("u+w", dir, force: true) ::FileUtils.rm_rf(dir) remote end end.compact.sort end |
#remove_sources(remote, commits: nil) ⇒ Array<SourceInfo>?
Removes shared sources for the given cache. The next time a client requests them, the removed sources will be recopied from the repo.
If you provide the commits: argument, only sources associated with
those commits are removed. Otherwise, all sources are removed.
Be careful not to remove sources that are currently in use by other GitCache clients.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/git_cache.rb', line 193 def remove_sources(remote, commits: nil) dir = repo_base_dir_for(remote) return nil unless ::File.directory?(dir) results = [] lock_repo(dir, remote) do |repo_lock| commits = nil if commits == :all shas = Array(commits).map { |ref| repo_lock.lookup_ref(ref) }.compact.uniq if commits repo_lock.find_sources(shas: shas).each do |(sha, path)| data = repo_lock.delete_source!(sha, path) results << SourceInfo.new(dir, sha, path, data) end results.map(&:sha).uniq.each do |sha| unless repo_lock.source_exists?(sha) sha_dir = ::File.join(dir, sha) ::FileUtils.chmod_R("u+w", sha_dir, force: true) ::FileUtils.rm_rf(sha_dir) end end end results.sort end |
#repo_info(remote) ⇒ RepoInfo?
Returns a RepoInfo describing the cache for the given remote, or
nil if the given remote has never been cached.
115 116 117 118 119 120 121 |
# File 'lib/git_cache.rb', line 115 def repo_info(remote) dir = repo_base_dir_for(remote) return nil unless ::File.directory?(dir) lock_repo(dir, remote) do |repo_lock| RepoInfo.new(dir, repo_lock.data) end end |