-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjekyll-git-mtime.rb
51 lines (43 loc) · 2.02 KB
/
jekyll-git-mtime.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Jekyll
module GitMtime
class GitError < StandardError; end
# run before most other hooks
LOW_PRIORITY = Jekyll::Hooks::PRIORITY_MAP[:low]
# Various constants
MTIME_PAGE_VAR = 'mtime'
def self.get_mtime(path=nil)
additional_status_options = ''
additional_log_options = ''
if (path != nil)
additional_status_options = "--ignored -- #{path}"
additional_log_options = "-- #{path}"
end
git_status_str = `git status --porcelain -uall #{additional_status_options}`
if ($? != 0)
raise GitError, "git status failed"
end
if (!git_status_str.empty?)
# git status reported that the file or workspace has been modified/ignored/staged/...
return nil
else
git_log_str = `git log -1 --format=format:"%ct" #{additional_log_options}`
if ($? != 0 || git_log_str.empty?)
raise GitError, "git log failed"
end
return Time.at git_log_str.to_i
end
end
Jekyll::Hooks.register(:pages, :post_init, priority: LOW_PRIORITY) do |page|
if (page.data[MTIME_PAGE_VAR] == 'build' || page.data[MTIME_PAGE_VAR] == 'site.time')
# mtime has been explicitly set to site.time (time of current build)
page.data[MTIME_PAGE_VAR] = page.site.time
elsif (page.data[MTIME_PAGE_VAR] == 'site' || page.data[MTIME_PAGE_VAR] == 'branch')
# set mtime to the time of the latest of any commits (not necessarily related to the current page“
page.data[MTIME_PAGE_VAR] = get_mtime || page.site.time
elsif (page.data[MTIME_PAGE_VAR] == nil)
# set mtime to the time of the last commit related to the current page
page.data[MTIME_PAGE_VAR] = get_mtime(page.path) || File.mtime(page.path)
end
end
end
end