Skip to content

Commit

Permalink
Merge pull request #2468 from Snapstromegon/#2224-git-first-added-date
Browse files Browse the repository at this point in the history
#2224 git created option for date
  • Loading branch information
zachleat committed Jun 30, 2022
2 parents fc6fc96 + 3a8e426 commit 85a9759
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Template.js
Expand Up @@ -13,6 +13,7 @@ const { TemplatePath, isPlainObject } = require("@11ty/eleventy-utils");

const ConsoleLogger = require("./Util/ConsoleLogger");
const getDateFromGitLastUpdated = require("./Util/DateGitLastUpdated");
const getDateFromGitFirstAdded = require("./Util/DateGitFirstAdded");

const TemplateData = require("./TemplateData");
const TemplateContent = require("./TemplateContent");
Expand Down Expand Up @@ -949,6 +950,15 @@ class Template extends TemplateContent {
if (data.date.toLowerCase() === "last modified") {
return this._getDateInstance("ctimeMs");
}
if (data.date.toLowerCase() === "git created") {
let d = getDateFromGitFirstAdded(this.inputPath);
if (d) {
return d;
}

// return now if this file is not yet available in `git`
return new Date();
}
if (data.date.toLowerCase() === "created") {
return this._getDateInstance("birthtimeMs");
}
Expand Down
24 changes: 24 additions & 0 deletions src/Util/DateGitFirstAdded.js
@@ -0,0 +1,24 @@
const spawn = require("cross-spawn");

function getGitFirstAddedTimeStamp(filePath) {
return (
parseInt(
spawn
.sync(
"git",
// Formats https://www.git-scm.com/docs/git-log#_pretty_formats
// %at author date, UNIX timestamp
["log", "--diff-filter=A", "--follow", "-1", "--format=%at", filePath]
)
.stdout.toString("utf-8")
) * 1000
);
}

// return a Date
module.exports = function (inputPath) {
let timestamp = getGitFirstAddedTimeStamp(inputPath);
if (timestamp) {
return new Date(timestamp);
}
};
12 changes: 12 additions & 0 deletions test/EleventyTest.js
Expand Up @@ -2,6 +2,7 @@ const test = require("ava");
const Eleventy = require("../src/Eleventy");
const EleventyWatchTargets = require("../src/EleventyWatchTargets");
const TemplateConfig = require("../src/TemplateConfig");
const DateGitFirstAdded = require("../src/Util/DateGitFirstAdded.js");
const DateGitLastUpdated = require("../src/Util/DateGitLastUpdated");
const normalizeNewLines = require("./Util/normalizeNewLines");

Expand Down Expand Up @@ -548,3 +549,14 @@ ${JSON.stringify(arr)}`);
t.is(normalizeNewLines(content[0]), str);
t.is(normalizeNewLines(content[1]), str);
});

test("#2224: date 'git created' populates page.date", async (t) => {
let elev = new Eleventy("./test/stubs-2224/", "./test/stubs-2224/_site");

let results = await elev.toJSON();
let [result] = results;

// This doesn’t test the validity of the function, only that it populates page.date.
let comparisonDate = DateGitFirstAdded("./test/stubs-2224/index.njk");
t.is(result.content.trim(), "" + comparisonDate.getTime());
});
4 changes: 4 additions & 0 deletions test/stubs-2224/index.njk
@@ -0,0 +1,4 @@
---
date: git created
---
{{ page.date.getTime() }}

0 comments on commit 85a9759

Please sign in to comment.