Class: LabelWeaver::TempRepo
- Inherits:
-
Object
- Object
- LabelWeaver::TempRepo
- Defined in:
- lib/label_weaver/temp_repo.rb
Instance Attribute Summary collapse
-
#branch ⇒ Object
readonly
Returns the value of attribute branch.
-
#excludes ⇒ Object
readonly
Returns the value of attribute excludes.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#repository_path ⇒ Object
readonly
Returns the value of attribute repository_path.
-
#repository_url ⇒ Object
readonly
Returns the value of attribute repository_url.
Instance Method Summary collapse
- #changed_file?(repository_file, project_root_dir:) ⇒ Boolean
-
#clone_or_update ⇒ Object
Clones the repository if it doesn’t exist yet locally or fetches the latest updates.
- #digest_for(file) ⇒ Object
-
#initialize(repository_path, repository_url:, branch: "main", excludes: [], logger: Logger.new($stdout)) ⇒ TempRepo
constructor
A new instance of TempRepo.
-
#relative_path(filename) ⇒ Pathname
The filename relative to the git repository root.
-
#relevant_files ⇒ Array<Pathname>
All repository files that are not set to be excluded.
-
#relevant_files_with_project_files(project_root_dir:) ⇒ Array<Array<Pathname, Pathname>>
All relevant repository files with their corresponding path within the project.
-
#timestamp_for(file) ⇒ Time
The last time the file was part of a commit, closest we can get to an mtime.
-
#update_repository_timestamps ⇒ Object
Updates the atime and mtime of each file in the repository to their latest commit time TODO: Check if still needed.
Constructor Details
#initialize(repository_path, repository_url:, branch: "main", excludes: [], logger: Logger.new($stdout)) ⇒ TempRepo
Returns a new instance of TempRepo.
12 13 14 15 16 17 18 |
# File 'lib/label_weaver/temp_repo.rb', line 12 def initialize(repository_path, repository_url:, branch: "main", excludes: [], logger: Logger.new($stdout)) @repository_path = repository_path @repository_url = repository_url @branch = branch @excludes = excludes + %w[. ..] @logger = logger end |
Instance Attribute Details
#branch ⇒ Object (readonly)
Returns the value of attribute branch.
10 11 12 |
# File 'lib/label_weaver/temp_repo.rb', line 10 def branch @branch end |
#excludes ⇒ Object (readonly)
Returns the value of attribute excludes.
10 11 12 |
# File 'lib/label_weaver/temp_repo.rb', line 10 def excludes @excludes end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
10 11 12 |
# File 'lib/label_weaver/temp_repo.rb', line 10 def logger @logger end |
#repository_path ⇒ Object (readonly)
Returns the value of attribute repository_path.
10 11 12 |
# File 'lib/label_weaver/temp_repo.rb', line 10 def repository_path @repository_path end |
#repository_url ⇒ Object (readonly)
Returns the value of attribute repository_url.
10 11 12 |
# File 'lib/label_weaver/temp_repo.rb', line 10 def repository_url @repository_url end |
Instance Method Details
#changed_file?(repository_file, project_root_dir:) ⇒ Boolean
31 32 33 |
# File 'lib/label_weaver/temp_repo.rb', line 31 def changed_file?(repository_file, project_root_dir:) digest_for(repository_file) != digest_for(project_root_dir + relative_path(repository_file)) end |
#clone_or_update ⇒ Object
Clones the repository if it doesn’t exist yet locally or fetches the latest updates
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/label_weaver/temp_repo.rb', line 38 def clone_or_update if repository_path.exist? logger.info "Pulling latest changes from remote repository (#{repository_url})..." repo.pull("origin", branch) else logger.info "Cloning remote repository, this might take a while..." Git.clone( repository_url, repository_path.basename, path: repository_path.parent, branch: ) end end |
#digest_for(file) ⇒ Object
27 28 29 |
# File 'lib/label_weaver/temp_repo.rb', line 27 def digest_for(file) Digest::SHA1.file(file).hexdigest end |
#relative_path(filename) ⇒ Pathname
Returns the filename relative to the git repository root.
83 84 85 |
# File 'lib/label_weaver/temp_repo.rb', line 83 def relative_path(filename) filename.relative_path_from(repository_path) end |
#relevant_files ⇒ Array<Pathname>
Returns All repository files that are not set to be excluded.
58 59 60 61 62 63 64 65 |
# File 'lib/label_weaver/temp_repo.rb', line 58 def relevant_files files.filter do |file| # Reject any files that are set up to be excluded. Shell filename globbing rules apply next false if excludes.any? { relative_path(file).fnmatch?(_1, File::FNM_DOTMATCH) } true end end |
#relevant_files_with_project_files(project_root_dir:) ⇒ Array<Array<Pathname, Pathname>>
Returns all relevant repository files with their corresponding path within the project.
91 92 93 94 95 96 |
# File 'lib/label_weaver/temp_repo.rb', line 91 def relevant_files_with_project_files(project_root_dir:) relevant_files.map do |repository_file| relative_filename = relative_path(repository_file) [repository_file, project_root_dir + relative_filename] end end |
#timestamp_for(file) ⇒ Time
Returns the last time the file was part of a commit, closest we can get to an mtime.
23 24 25 |
# File 'lib/label_weaver/temp_repo.rb', line 23 def (file) repo.log(1).path(file).first.date end |
#update_repository_timestamps ⇒ Object
Updates the atime and mtime of each file in the repository to their latest commit time TODO: Check if still needed
71 72 73 74 75 76 77 78 |
# File 'lib/label_weaver/temp_repo.rb', line 71 def logger.info "Updating repository timestamps..." repository_path.glob("**/*", File::FNM_DOTMATCH).each do |file| # Set the file modification times to the last time they were updated in a commit time = repo.all.log.path(file).first.date file.utime(time, time) end end |