Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checksummer #431

Merged
merged 9 commits into from May 2, 2014
Copy path View file
@@ -7,6 +7,7 @@ module Nanoc

# Load helper classes
autoload 'Context', 'nanoc/base/context'
autoload 'Checksummer', 'nanoc/base/checksummer'
autoload 'DirectedGraph', 'nanoc/base/directed_graph'
autoload 'Errors', 'nanoc/base/errors'
autoload 'Memoization', 'nanoc/base/memoization'
Copy path View file
@@ -0,0 +1,82 @@
# encoding: utf-8

module Nanoc

# Creates checksums for given objects.
#
# A checksum is a string, such as “mL+TaqNsEeiPkWloPgCtAofT1yg=”, that is used
# to determine whether a piece of data has changed.
class Checksummer

class << self

# @param obj The object to create a checksum for
#
# @return [String] The digest
def calc(obj)
digest = Digest::SHA1.new
update(obj, digest)
digest.base64digest
end

private

def update(obj, digest)
digest.update(obj.class.to_s)

case obj
when String
digest.update(obj)
when Array
obj.each do |el|
digest.update('elem')
update(el, digest)
end
when Hash
obj.each do |key, value|
digest.update('key')
update(key, digest)
digest.update('value')
update(value, digest)
end
when Pathname
filename = obj.to_s
if File.exist?(filename)
stat = File.stat(filename)
digest.update(stat.size.to_s + '-' + stat.mtime.utc.to_s)
else
digest.update('???')
end
when Nanoc::RulesCollection
update(obj.data, digest)
when Nanoc::CodeSnippet
update(obj.data, digest)
when Nanoc::Item, Nanoc::Layout
digest.update('content')
if obj.respond_to?(:binary?) && obj.binary?
update(Pathname.new(obj.raw_filename), digest)
else
update(obj.raw_content, digest)
end

digest.update('attributes')
attributes = obj.attributes.dup
attributes.delete(:file)
update(attributes, digest)
else
data = begin
Marshal.dump(obj)
rescue
obj.inspect
end

digest.update(data)
end

end

end

end

end
@@ -7,6 +7,11 @@ module Nanoc
# @api private
class RulesCollection

# @return [String] the contents of the Rules file
#
# @api private
attr_accessor :data

extend Nanoc::Memoization

# @return [Array<Nanoc::Rule>] The list of item compilation rules that
@@ -161,7 +166,7 @@ def reference
# @return [String] The checksum for this object. If its contents change,
# the checksum will change as well.
def checksum
@data.checksum
Nanoc::Checksummer.calc(self)
end

def inspect
@@ -64,17 +64,7 @@ def freeze_recursively
#
# @api private
def checksum
digest = Digest::SHA1.new
digest.update('array')
each do |e|
digest.update('elem')
if e.respond_to?(:checksum)
digest.update(e.checksum)
else
digest.update(Marshal.dump(e).checksum)
end
end
digest.hexdigest
Nanoc::Checksummer.calc(self)
end

end
Copy path View file
@@ -66,20 +66,7 @@ def freeze_recursively
#
# @api private
def checksum
digest = Digest::SHA1.new
digest.update('hash')
each_pair do |k,v|
digest.update('pair')
[k,v].each do |e|
digest.update('pair-elem')
if e.respond_to?(:checksum)
digest.update(e.checksum)
else
digest.update(Marshal.dump(e).checksum)
end
end
end
digest.hexdigest
Nanoc::Checksummer.calc(self)
end

end
@@ -9,8 +9,7 @@ module Nanoc::PathnameExtensions
#
# @api private
def checksum
stat = File.stat(to_s)
stat.size.to_s + '-' + stat.mtime.to_s
Nanoc::Checksummer.calc(self)
end

end
@@ -16,9 +16,7 @@ def cleaned_identifier
#
# @api private
def checksum
digest = Digest::SHA1.new
digest.update(self)
digest.hexdigest
Nanoc::Checksummer.calc(self)
end

end
@@ -50,7 +50,7 @@ def inspect
# @return [String] The checksum for this object. If its contents change,
# the checksum will change as well.
def checksum
@data.checksum
Nanoc::Checksummer.calc(self)
end

end
@@ -247,22 +247,7 @@ def inspect
# @return [String] The checksum for this object. If its contents change,
# the checksum will change as well.
def checksum
content_checksum =
if binary?
if File.exist?(raw_filename)
Pathname.new(raw_filename).checksum
else
''.checksum
end
else
@raw_content.checksum
end

attributes = @attributes.dup
attributes.delete(:file)
attributes_checksum = attributes.checksum

content_checksum + ',' + attributes_checksum
Nanoc::Checksummer.calc(self)
end
memoize :checksum

@@ -88,9 +88,7 @@ def inspect
# @return [String] The checksum for this object. If its contents change,
# the checksum will change as well.
def checksum
attributes = @attributes.dup
attributes.delete(:file)
@raw_content.checksum + ',' + attributes.checksum
Nanoc::Checksummer.calc(self)
end
memoize :checksum

Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.