Skip to content

feat: filter commits by message #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 55 additions & 6 deletions src/getGitCommitDateFromPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ const path = require("path");
const spawn = require("cross-spawn");

/**
* Gets the Git commit date from path.
*
* The code is based on @vuepress/plugin-last-updated,
* https://github.com/vuejs/vuepress/blob/master/packages/%40vuepress/plugin-last-updated/
* Gets the Git commit date from path, filtering commits by regex.
*
* @param {string} filePath The file path
* @param {Object} options Filter options
* @param {RegExp} options.keep Whitelisting regex. Commit subjects matching this regex are kept.
* @param {RegExp} options.ignore Blacklisting regex. Commit subjects matching this regex are ignored.
*
* @return {Date} The git commit date if path is commited to Git.
*/
module.exports = function (filePath) {
module.exports = function (filePath, options) {
return options ? getGitCommitDateFiltered(filePath, options) : getGitCommitDateFast(filePath);
};

function getGitCommitDateFast(filePath) {
let output;

try {
Expand All @@ -34,4 +38,49 @@ module.exports = function (filePath) {
return new Date(ts);
}
}
};
}

function getGitCommitDateFiltered(filePath, options) {
const { keep, ignore } = options || {};

function matchesWhitelist(subject) {
return keep ? subject.match(keep) : true;
}
function matchesBlacklist(subject) {
return ignore ? !subject.match(ignore) : true;
}

let output;

try {
output = spawn.sync(
"git",
["log", "--follow", "--format=%at %s", path.basename(filePath)],
{ cwd: path.dirname(filePath) }
);
} catch {
throw new Error("Fail to run 'git log'");
}

if (output && output.stdout) {
const commits = output.stdout.toString("utf-8").split('\n');

// Filter commits which match filter options.
const filtered = commits.filter(s => {
const subject = s.substring(s.indexOf(' ') + 1);
return matchesWhitelist(subject) && matchesBlacklist(subject);
});

if (filtered && filtered[0]) {
// Grab latest commit timestamp.
const s = filtered[0];
const ts = parseInt(s.substring(0, s.indexOf(' ')), 10) * 1000;

// Paths not commited to Git returns empty timestamps, resulting in NaN.
// So, convert only valid timestamps.
if (!isNaN(ts)) {
return new Date(ts);
}
}
}
}
33 changes: 33 additions & 0 deletions tests/getGitCommitDateFromPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ test("Get commit date of a committed file", (t) => {
t.is(date.toISOString(), "2021-08-19T09:57:47.000Z");
});

test("Get commit date of a committed file (keep)", (t) => {
const filePath = path.join(__dirname, "./fixtures/sample.md");
const date = getGitCommitDateFromPath(filePath, { keep: /^First/ });
t.truthy(date);
t.is(date.toISOString(), "2021-08-19T09:24:06.000Z");
});

test("Get commit date of a committed file (ignore)", (t) => {
const filePath = path.join(__dirname, "./fixtures/sample.md");
const date = getGitCommitDateFromPath(filePath, { ignore: /dummy files/ });
t.truthy(date);
t.is(date.toISOString(), "2021-08-19T09:24:06.000Z");
});

test("Get commit date of a committed file (keep + ignore 1)", (t) => {
const filePath = path.join(__dirname, "./fixtures/sample.md");
const date = getGitCommitDateFromPath(filePath, { keep: /.*/, ignore: /dummy files/ });
t.truthy(date);
t.is(date.toISOString(), "2021-08-19T09:24:06.000Z");
});

test("Get commit date of a committed file (keep + ignore 2)", (t) => {
const filePath = path.join(__dirname, "./fixtures/sample.md");
const date = getGitCommitDateFromPath(filePath, { keep: /^First/, ignore: /^First/ });
t.is(date, undefined);
});

test("Get commit date of a committed file (no match)", (t) => {
const filePath = path.join(__dirname, "./fixtures/sample.md");
const date = getGitCommitDateFromPath(filePath, { keep: /never gonna give you up/ });
t.is(date, undefined);
});

test("Should not get commit date of a uncommitted file", async (t) => {
const filePath = path.join(outputBase, tempFileName);
await rimraf(outputBase);
Expand Down