Sha256: ae4689c69de17135c625a5d5b73bfdf4d8a775fcf9fffa8db8bdf262bd6d38e0

Contents?: true

Size: 632 Bytes

Versions: 3

Compression:

Stored size: 632 Bytes

Contents

# encoding: UTF-8

require "digest/md5"

module Bunch
  class ContentHash
    def initialize(file_or_tree)
      @input = file_or_tree
    end

    def result
      @path = []
      @hash = 0
      @input.accept(self)
      @hash
    end

    def enter_tree(tree)
      @path << tree.name if tree.name
    end

    def leave_tree(tree)
      @path.pop if tree.name
    end

    def visit_file(file)
      file_path = [*@path, file.path].join("/")
      @hash ^= truncated_md5("#{file_path}:#{file.content}")
    end

    private

    def truncated_md5(string)
      Digest::MD5.digest(string)[0..4].unpack("L")[0]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bunch-1.0.0pre3 lib/bunch/content_hash.rb
bunch-1.0.0pre2 lib/bunch/content_hash.rb
bunch-1.0.0pre1 lib/bunch/content_hash.rb