scratch/content/html/en/blog/2010-09-02-Use-git-to-calculate-trusted-mtimes.md
Yann Esposito (Yogsototh) 6c6d974920 New blog entry
2010-09-02 16:51:37 +02:00

1.5 KiB

isHidden menupriority kind created_at title author_name author_uri tags
false 1 article 2010-09-02T15:54:10+02:00 Use git to calculate trusted mtimes Yann Esposito yannesposito.com
nanoc
web
git

You can remark at the bottom of each page I provide a last modification date. This label was first calculated using the mtime of the file on the file system. But many times I modify this date just to force some recompilation. Therefore the date wasn't a date of real modification.

I use git to version my website. And fortunately I can know the last date of real change of a file. This is how I do this with nanoc:

def gitmtime filepath=@item.path.sub('/Scratch/','content/html/').sub(/\/$/,'') ext=%{.#{@item[:extension]}} filepath<<=ext if not FileTest.exists?(filepath) filepath.sub!(ext,%{/index#{ext}}) end str=`git log -1 --format='%ci' -- #{filepath}` if str.nil? or str.empty? return Time.now else return DateTime.parse( str ) end end

Of course I know it is really slow and absolutely not optimized. But it works as expected. Now the date you see at the bottom is exactly the date I modified the content of the page.

Thanks to Eric Sunshine to provide me some hints at cleaning my code. Je tiens à remercier Eric Sunshine pour ses conseils sur ce problème.