module Gtn::ModificationTimes

Module for obtaining modification times of files. It walks the git history to record the last time a file was modified. This is faster than talking to the file system.

Public Class Methods

cached_command() click to toggle source
# File _plugins/gtn/mod.rb, line 48
def self.cached_command
  return command if discover_caches.nil?

  Jekyll.logger.info '[GTN/Time/Mod] Using cached modification times'

  previous_commit = discover_caches.split('-').last.split('.').first
  previous = File.read(discover_caches)

  `git log --name-only --pretty='GTN_GTN:%ct' #{previous_commit}..` + previous
end
clean_path(f) click to toggle source
# File _plugins/gtn/mod.rb, line 71
def self.clean_path(f)
  if f =~ %r{^\./}
    f[2..]
  else
    f
  end
end
command() click to toggle source
# File _plugins/gtn/mod.rb, line 59
def self.command
  `git log --name-only --pretty='GTN_GTN:%ct'`
end
commit_count_cache() click to toggle source
# File _plugins/gtn/mod.rb, line 67
def self.commit_count_cache
  @@COMMIT_COUNT_CACHE
end
discover_caches() click to toggle source
# File _plugins/gtn/mod.rb, line 29
def self.discover_caches
  # Really there should only be one, but maybe someone's been silly so
  # we'll just take the first one we find.
  Dir.glob('metadata/git-mod-*.txt').first
end
generate_cache() click to toggle source
# File _plugins/gtn/mod.rb, line 35
def self.generate_cache
  rev = `git rev-list -n 1 main`.strip

  if discover_caches.nil?
    File.write("metadata/git-mod-#{rev}.txt", command)
  else
    prev = discover_caches
    results = cached_command
    File.delete(prev)
    File.write("metadata/git-mod-#{rev}.txt", results)
  end
end
init_cache() click to toggle source
# File _plugins/gtn/mod.rb, line 11
def self.init_cache
  return unless @@TIME_CACHE.nil?

  @@TIME_CACHE = {}
  @@COMMIT_COUNT_CACHE = Hash.new(0)
  Jekyll.logger.info '[GTN/Time/Mod] Filling Time Cache'
  cached_command
    .split('GTN_GTN:')
    .map { |x| x.split("\n\n") }
    .select { |x| x.length > 1 }
    .each do |date, files|
    files.split(/\n/).each do |f|
      @@TIME_CACHE[f] = Time.at(date.to_i) if !@@TIME_CACHE.key? f
      @@COMMIT_COUNT_CACHE[f] += 1
    end
  end
end
obtain_modification_count(f_unk) click to toggle source
# File _plugins/gtn/mod.rb, line 79
def self.obtain_modification_count(f_unk)
  f = clean_path(f_unk)
  init_cache
  if @@COMMIT_COUNT_CACHE.key? f
    @@COMMIT_COUNT_CACHE[f]
  else
    0
  end
end
obtain_time(f_unk) click to toggle source
# File _plugins/gtn/mod.rb, line 89
def self.obtain_time(f_unk)
  f = clean_path(f_unk)
  init_cache
  if @@TIME_CACHE.key? f
    @@TIME_CACHE[f]
  else
    begin
      # Non git file.
      @@TIME_CACHE[f] = File.mtime(f)
      Jekyll.logger.warn "[GTN/Time/Mod] No git cached time available for #{f}, defaulting to checkout"
      @@TIME_CACHE[f]
    rescue StandardError
      Time.at(0)
    end
  end
end
time_cache() click to toggle source
# File _plugins/gtn/mod.rb, line 63
def self.time_cache
  @@TIME_CACHE
end