You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
7.9 KiB

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>YBlog - Use git to calculate trusted mtimes</title>
<meta name="keywords" content="nanoc, web, git" />
<link rel="shortcut icon" type="image/x-icon" href="../../../../Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/css/y.css" />
<link rel="stylesheet" type="text/css" href="/css/legacy.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="/rss.xml" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="apple-touch-icon" href="../../../../Scratch/img/about/FlatAvatar@2x.png" />
<!--[if lt IE 9]>
<script src=""></script>
<!-- IndieAuth -->
<link href="" rel="me">
<link href="" rel="me">
<link href="" rel="me">
<link rel="pgpkey" href="../../../../pubkey.txt">
<body lang="en" class="article">
<div id="content">
<div id="header">
<div id="choix">
<span id="choixlang">
<a href="../../../../Scratch/fr/blog/2010-09-02-Use-git-to-calculate-trusted-mtimes/">French</a>
<span class="tomenu"><a href="#navigation">↓ Menu ↓</a></span>
<span class="flush"></span>
<div id="titre">
<h1>Use git to calculate trusted mtimes</h1>
<div class="flush"></div>
<div id="afterheader" class="article">
<div class="corps">
<p>You can remark at the bottom of each page I provide a last modification date. This label was first calculated using the <code>mtime</code> 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 <em>real</em> modification.</p>
<p>I use <a href="">git</a> to version my website. And fortunately I can know the last date of <em>real</em> change of a file. This is how I do this with <a href="">nanoc</a>:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode ruby"><code class="sourceCode ruby"><a class="sourceLine" id="cb1-1" title="1"><span class="kw">def</span> gitmtime</a>
<a class="sourceLine" id="cb1-2" title="2"> filepath=<span class="ot">@item</span>.path.sub(<span class="st">'/Scratch/'</span>,<span class="st">'content/html/'</span>).sub(<span class="ot">/\/$/</span>,<span class="st">''</span>)</a>
<a class="sourceLine" id="cb1-3" title="3"> ext=<span class="ot">%{</span><span class="st">.</span><span class="ot">#{@item</span>[<span class="st">:extension</span>]<span class="ot">}}</span></a>
<a class="sourceLine" id="cb1-4" title="4"> filepath&lt;&lt;=ext</a>
<a class="sourceLine" id="cb1-5" title="5"> <span class="kw">if</span> <span class="kw">not</span> <span class="dt">FileTest</span>.exists?(filepath)</a>
<a class="sourceLine" id="cb1-6" title="6"> filepath.sub!(ext,<span class="ot">%{#{@item</span>.raw_filename<span class="ot">}#{</span>ext<span class="ot">}}</span>)</a>
<a class="sourceLine" id="cb1-7" title="7"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb1-8" title="8"> str=<span class="st">`git log -1 --format='%ci' -- </span><span class="ot">#{</span>filepath<span class="ot">}</span><span class="st">`</span></a>
<a class="sourceLine" id="cb1-9" title="9"> <span class="kw">if</span> str.nil? <span class="kw">or</span> str.empty?</a>
<a class="sourceLine" id="cb1-10" title="10"> <span class="kw">return</span> <span class="dt">Time</span>.now</a>
<a class="sourceLine" id="cb1-11" title="11"> <span class="kw">else</span></a>
<a class="sourceLine" id="cb1-12" title="12"> <span class="kw">return</span> <span class="dt">DateTime</span>.parse( str )</a>
<a class="sourceLine" id="cb1-13" title="13"> <span class="kw">end</span></a>
<a class="sourceLine" id="cb1-14" title="14"><span class="kw">end</span></a></code></pre></div>
<p>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 <em>content</em> of the page.</p>
<p><em>Edit</em>: Thanks to Eric Sunshine and Kris to provide me some hints at cleaning my code.</p>
<div id="afterarticle">
<div id="social">
<a href="/rss.xml" target="_blank" rel="noopener noreferrer nofollow" class="social">RSS</a>
<a href="" target="_blank" rel="noopener noreferrer nofollow" class="social">Tweet</a>
<a href="" target="_blank" rel="noopener noreferrer nofollow" class="social">FB</a>
<br />
<a class="message" href="../../../../Scratch/en/blog/Social-link-the-right-way/">These social sharing links preserve your privacy</a>
<div id="navigation">
<a href="../../../../">Home</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/blog">Blog</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/softwares">Softwares</a>
<span class="sep">¦</span>
<a href="../../../../Scratch/en/about">About</a>
<div id="totop"><a href="#header">↑ Top ↑</a></div>
<div id="bottom">
Published on 2010-09-02
<a href="">Follow @yogsototh</a>
<a rel="license" href="">Yann Esposito©</a>
Done with
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>Vim</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">spacemacs</a>
<span class="pala">&amp;</span>
<a href="" target="_blank" rel="noopener noreferrer nofollow"><strike>nanoc</strike></a>
<a href="" target="_blank" rel="noopener noreferrer nofollow">Hakyll</a>
<hr />
<div style="max-width: 100%">
<a href="">
<img src="../../../../Scratch/img/ada-logo.png" class="simple" style="height: 16px;
border-radius: 50%;
display:inline-block;" />
<code style="display:inline-block;
text-align: left;
vertical-align: top;
max-width: 85%;">