我在NodeJS中检索Git标签信息的方法很慢,如何加快速度



我正在创建一个NodeJS脚本,该脚本需要从存储库中获取所有Git标记,以及创建标记的消息和日期,然后将它们保存为JSON文件中的条目。例如:

[{
"tag": "v1.1.0",
"message": "Add navigation",
"date": "Tue Oct 4 10:19:12 2018 +0100"
}, {
"tag": "v1.1.1",
"message": "Fix issue with spacing in the navigation",
"date": "Tue Oct 9 12:13:16 2018 +0100"
}]

我已经想好了如何做到这一点,使用一些Node模块来访问标签,然后执行一些Git命令来从每个标签中获得我需要的所有信息。这是使用shelljs完成的。

我的问题是,这是非常缓慢的。运行gitTag.all((tags) = {})(使用git标记)速度很快,因为它只需要拉入标记名称。然而,为了获得消息和日期,我为循环中的每个标签运行两个单独的命令:

let msg = shell.exec(`git for-each-ref refs/tags/${tag} --format='%(subject)'`, {silent:true}).stdout;
let date = shell.exec(`git for-each-ref refs/tags/${tag} --format='%(authordate)'`, {silent:true}).stdout;

无论如何,这两个命令都有点慢,但每次为每个标记名运行它们都需要很长时间。

我有更快的方法吗

请记住,我正在同步运行这个,因为另一个Node脚本将读取这个JSON文件,这可能会导致竞争。然而,任何异步的想法都是受欢迎的,如果它们也能解决这个问题的话。

请参阅下面的完整代码

const gitTag = require('git-tag')({
localOnly: true,
dir: '.git'
});
const fs = require('fs-extra');
const shell = require('shelljs');
let tagsAllData = [];
let formatString = (string) => {
return string.replace(/^'/, '').replace(/'n+$/, '');
}
// Fetch all git tags
gitTag.all((tags) => {
tags.forEach(tag => {
// Collect the tag message and date values
let msg = shell.exec(`git for-each-ref refs/tags/${tag} --format='%(subject)'`, {silent:true}).stdout;
let date = shell.exec(`git for-each-ref refs/tags/${tag} --format='%(authordate)'`, {silent:true}).stdout;
// Create array of tag objects
tagsAllData.push({
'tag': tag,
'message': formatString(msg),
'date': formatString(date)
});
});
// Write the tag data as a JSON file
let tagsJSON = JSON.parse(JSON.stringify(tagsAllData));
fs.writeJSONSync('src/data/tags.json', tagsJSON);
});

您的实际代码为每个标记调用shell两次。为了加快速度,你应该调用shell,因此git最多一次-对于所有标记。在一个外壳中,它看起来像这样:

git for-each-ref --sort=v:refname --format "tag: %(refname:strip=2) message: %(subject) date: %(creatordate:iso)" refs/tags

输出如下:

tag: v2.20.0-rc0 message: Git 2.20-rc0 date: 2018-11-18 18:25:38 +0900
tag: v2.20.0-rc1 message: Git 2.20-rc1 date: 2018-11-21 23:25:15 +0900
tag: v2.20.0-rc2 message: Git 2.20-rc2 date: 2018-12-01 21:45:08 +0900
tag: v2.20.1 message: Git 2.20.1 date: 2018-12-15 12:31:46 +0900

您必须首先将此输出拆分为单独的行,并将每一行拆分为字段。您可以通过调整format来简化解析(例如,在字段之间使用特殊字符)。

在另一个答案的基础上,我还将考虑使整个方法更加异步。foreach正在阻塞,并且将取决于每个呼叫的单独结果。

相反,我将使用原生API,并从中创建一个承诺(如这里所解释的)。

您可以使用Promise.all来聚合结果。这是一个做的简化示例

const dateQueries = [];
const msgQueries = [];
tags.forEach(tag => {
const dateQuery = exec(`git for-each-ref refs/tags/${tag} --format='%(subject)'`);
const msgQuery = exec(`git for-each-ref refs/tags/${tag} --format='%(authordate)'`);
dateQueries.push(dateQuery);
msgQueries.push(msgQuery);
});
const msgResults = await Promise.all(msgQueries);
const dateResults = await Promise.all(dateQueries);

然而,A.H.的答案更具性能,因为它优化了查询本身。

最新更新