Skip to content

Commit

Permalink
Fix git error for dynamically generated resources
Browse files Browse the repository at this point in the history
  • Loading branch information
ayushn21 committed Sep 19, 2023
1 parent 1d4c1d6 commit 51b4d8e
Showing 3 changed files with 41 additions and 22 deletions.
3 changes: 2 additions & 1 deletion lib/bridgetown-sitemap.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

require "bridgetown"
require "bridgetown/resource/base"
require "bridgetown-sitemap/builder"
require "bridgetown-sitemap/git_inspector"
require "bridgetown/resource/base"

Bridgetown.initializer :"bridgetown-sitemap" do |config|
config.builder BridgetownSitemap::Builder
34 changes: 34 additions & 0 deletions lib/bridgetown-sitemap/git_inspector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module BridgetownSitemap
class GitInspector
def initialize(resource)
@resource = resource
end

def latest_git_commit_date
return nil unless git_repo?
return nil unless repo_origin?

date = cache.getset(@resource.id) do
`git log -1 --pretty="format:%cI" "#{@resource.path}"`
end

Time.parse(date) if date.present?
end

private

def repo_origin?
@resource.model.origin.url.scheme == "repo"
end

def git_repo?
system "git status", out: File::NULL, err: File::NULL
end

def cache
@cache ||= Bridgetown::Cache.new("sitemap")
end
end
end
26 changes: 5 additions & 21 deletions lib/bridgetown/resource/base.rb
Original file line number Diff line number Diff line change
@@ -4,27 +4,11 @@ module Bridgetown
module Resource
class Base
def sitemap_last_modified_at
(data.last_modified_at || latest_git_commit_date || date)&.to_time
end

private

def latest_git_commit_date
return nil unless git_repo?

date = sitemap_cache.getset(id) do
`git log -1 --pretty="format:%cI" "#{path}"`
end

Time.parse(date) if date.present?
end

def git_repo?
system "git status", out: File::NULL, err: File::NULL
end

def sitemap_cache
@sitemap_cache = Bridgetown::Cache.new("sitemap")
(
data.last_modified_at ||
BridgetownSitemap::GitInspector.new(self).latest_git_commit_date ||
date
)&.to_time
end
end
end

0 comments on commit 51b4d8e

Please sign in to comment.