Module: Dockly::History
- Defined in:
- lib/dockly/history.rb
Overview
This module contains logic to find matching content hash for a given commit.
Constant Summary collapse
- ASCII_FILE_SEP =
28.chr
- ASCII_RECORD_SEP =
30.chr
- TAG_PREFIX =
'dockly-'
Class Method Summary collapse
- .content_hash_for(paths) ⇒ Object
- .content_tag ⇒ Object
- .duplicate_build? ⇒ Boolean
- .duplicate_build_sha ⇒ Object
- .ls_files ⇒ Object
- .push_content_tag! ⇒ Object
- .repo ⇒ Object
- .tags ⇒ Object
- .write_content_tag! ⇒ Object
Class Method Details
.content_hash_for(paths) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/dockly/history.rb', line 51 def content_hash_for(paths) paths.sort.each_with_object(Digest::SHA384.new) do |path, hash| next unless File.exist?(path) mode = File::Stat.new(path).mode data = File.read(path) str = [path, mode, data].join(ASCII_RECORD_SEP.chr) + ASCII_FILE_SEP.chr hash.update(str) end.hexdigest end |
.content_tag ⇒ Object
41 42 43 |
# File 'lib/dockly/history.rb', line 41 def content_tag @content_tag ||= TAG_PREFIX + content_hash_for(ls_files) end |
.duplicate_build? ⇒ Boolean
24 25 26 |
# File 'lib/dockly/history.rb', line 24 def duplicate_build? !duplicate_build_sha.nil? end |
.duplicate_build_sha ⇒ Object
28 29 30 31 32 |
# File 'lib/dockly/history.rb', line 28 def duplicate_build_sha return @duplicate_build_sha if @duplicate_build_sha sha = [content_tag] @duplicate_build_sha = sha unless sha == repo.head.target_id end |
.ls_files ⇒ Object
45 46 47 48 49 |
# File 'lib/dockly/history.rb', line 45 def ls_files repo.head.target.tree.walk(:preorder) .map { |root, elem| [root, elem[:name]].compact.join } .select(&File.method(:file?)) end |
.push_content_tag! ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/dockly/history.rb', line 10 def push_content_tag! fail 'An SSH agent must be running to push the tag' if ENV['SSH_AUTH_SOCK'].nil? refs = ["refs/tags/#{content_tag}"] repo.remotes.each do |remote| username = remote.url.split('@').first creds = Rugged::Credentials::SshKeyFromAgent.new(username: username) remote.push(refs, credentials: creds) end end |
.repo ⇒ Object
61 62 63 |
# File 'lib/dockly/history.rb', line 61 def repo @repo ||= Rugged::Repository.discover('.') end |
.tags ⇒ Object
34 35 36 37 38 39 |
# File 'lib/dockly/history.rb', line 34 def ||= Hash.new do |hash, key| tag = repo.[key] hash[key] = tag.target_id if tag end end |
.write_content_tag! ⇒ Object
20 21 22 |
# File 'lib/dockly/history.rb', line 20 def write_content_tag! repo..create(content_tag, repo.head.target_id, true) end |