Skip to content

Commit

Permalink
get git results faster
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Jan 6, 2024
1 parent d1ec4cc commit 5307b27
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
39 changes: 5 additions & 34 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ const {
extractHandlebars,
insertHandlebars,
} = require('./lib/extractors.js');
const {
dateFromGitLog,
} = require('./lib/git.js');

const g_cacheid = Date.now();
const packageJSON = JSON.parse(fs.readFileSync('package.json', {encoding: 'utf8'}));
Expand Down Expand Up @@ -960,41 +963,9 @@ const Builder = function(outBaseDir, options) {
data);
}

function dateFromGitLog(result, filename, timeType) {
//console.log(filename, timeType, `'${result.stdout}`);
const dateStr = result.stdout.split('\n')[0].trim();
const seconds = parseInt(dateStr);
if (!isNaN(seconds)) {
return new Date(seconds * 1000);
}
const stat = fs.statSync(filename);
console.log('got date from filename:', stat[timeType]);
return new Date(stat[timeType]);
}

for (const article of g_articles) {
{
const result = await utils.executeP('git', [
'log',
'--format=%cd',
'--date=unix',
'--name-only',
'--diff-filter=A',
article.src_file_name,
]);
article.dateAdded = dateFromGitLog(result, article.src_file_name, 'ctimeMs');
}
{
const result = await utils.executeP('git', [
'log',
'--format=%cd',
'--date=unix',
'--name-only',
'--max-count=1',
article.src_file_name,
]);
article.dateModified = dateFromGitLog(result, article.src_file_name, 'mtimeMs');
}
article.dateAdded = await dateFromGitLog(fs, article.src_file_name, 'ctimeMs');
article.dateModified = await dateFromGitLog(fs, article.src_file_name, 'mtimeMs');
}

let articles = g_articles.filter(function(article) {
Expand Down
49 changes: 49 additions & 0 deletions lib/git.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const utils = require('./utils');

const s_gitFilenameToDateMaps = {
ctimeMs: {
map: new Map(),
},
mtimeMs: {
map: new Map(),
},
};

async function dateFromGitLog(fs, filename, timeType) {
const mapInfo = s_gitFilenameToDateMaps[timeType];
if (!mapInfo.read) {
mapInfo.read = true;
const result = await utils.executeP('git', [
'log',
'--format=%cd',
'--date=unix',
'--name-only',
...(timeType === 'ctimeMs' ? ['--diff-filter=A'] : ['--max-count=1']),
]);
const numberRE = /^\d+$/;
const lines = result.stdout.split('\n');
let currentDate;
for (const line of lines) {
if (numberRE.test(line)) {
const seconds = parseInt(line);
if (!isNaN(seconds)) {
currentDate = new Date(seconds * 1000);
}
} else if (line.length > 2) {
mapInfo.map.set(line.trim(), currentDate);
}
}
}
const date = mapInfo.map.get(filename);
if (date) {
return date;
}
const stat = fs.statSync(filename);
console.log('got date from filename:', stat[timeType]);
return new Date(stat[timeType]);
}

module.exports = {
dateFromGitLog,
};

0 comments on commit 5307b27

Please sign in to comment.