module Licensed::Sources::ContentVersioning

Constants

CONTENTS
GIT

Public Instance Methods

contents_hash(paths) click to toggle source

Find the version for a list of paths using their file contents

paths - list of paths to find version

Returns a hash of the path contents as an identifier for the group

# File lib/licensed/sources/helpers/content_versioning.rb, line 59
def contents_hash(paths)
  return if paths.nil?

  paths = paths.compact.select { |path| File.file?(path) }
  return if paths.empty?
  # rubocop:disable GitHub/InsecureHashAlgorithm
  paths.sort
       .reduce(Digest::XXHash64.new, :file)
       .digest
       .to_s(16) # convert to hex
  # rubocop:enable GitHub/InsecureHashAlgorithm
end
contents_version(*paths) click to toggle source

Find the version for a list of paths using the version strategy specified for the source from the configuration

paths - list of paths to find version

Returns a version identifier for the given files

# File lib/licensed/sources/helpers/content_versioning.rb, line 17
def contents_version(*paths)
  case version_strategy
  when CONTENTS
    contents_hash(paths)
  when GIT
    git_version(paths)
  end
end
git_version(paths) click to toggle source

Find the version for a list of paths using Git commit information

paths - list of paths to find version

Returns the most recent git SHA from the given paths

# File lib/licensed/sources/helpers/content_versioning.rb, line 46
def git_version(paths)
  return if paths.nil?

  paths.map { |path| Licensed::Git.version(path) }
       .reject { |sha| sha.to_s.empty? }
       .max_by { |sha| Licensed::Git.commit_date(sha) }
end
version_strategy() click to toggle source

Returns the version strategy configured for the source

# File lib/licensed/sources/helpers/content_versioning.rb, line 27
def version_strategy
  # default to git for backwards compatible behavior
  @version_strategy ||= begin
    case config.fetch("version_strategy", nil)
    when CONTENTS
      CONTENTS
    when GIT
      GIT
    else
      Licensed::Git.available? ? GIT : CONTENTS
    end
  end
end