Javascript 我在Node JS中检索Git标记信息的方法很慢,如何提高速度?
我正在创建一个节点JS脚本,需要从存储库中获取所有Git标记,以及标记创建的消息和日期,然后将它们保存为JSON文件中的条目。例如:Javascript 我在Node JS中检索Git标记信息的方法很慢,如何提高速度?,javascript,node.js,git,node-modules,Javascript,Node.js,Git,Node Modules,我正在创建一个节点JS脚本,需要从存储库中获取所有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
[{
"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"
}]
我已经知道了如何做到这一点,使用一些节点模块访问标记,然后执行一些Git命令,从每个标记中获取我需要的所有信息。这是通过使用
我的问题是,这是非常缓慢的。运行gitTag.all((tags)={})
(using)速度很快,因为它只是拉入标记名。但是,为了获取消息和日期,我对循环中的每个标记运行两个单独的命令:
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;
无论如何,这两个命令都有点慢,但是每次为每个标记名运行它们都需要很长时间
有没有更快的方法让我这么做?
请记住,我正在同步运行这个脚本,因为另一个节点脚本将读取这个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-for-all标记。在外壳中,这看起来像这样:
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
输出如下所示:
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
您必须首先将此输出拆分为单独的行,并将每行拆分为字段。您可以通过调整<代码>格式>代码>使之更容易(例如通过使用字段之间的特殊字符)。code>foreach正在阻塞,这取决于每个调用的单个结果 相反,我将使用本机API并从中创建承诺(如前所述) 您可以使用该函数来聚合结果。这是一个简化的示例
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.的答案更有效,因为它优化了查询本身。shell的功能取决于系统。它会将请求发送到系统shell,然后从shell获得响应。就是这样,为了快速得到结果,shell需要快速响应,然后只有您的
nodeJS
才会显示响应。您无需等待即可执行其他功能。但是为了加速这个shell
功能。您的系统应该运行良好。谢谢@BanujanBalendrakumar,我同意-这就是我问这个问题的部分原因。我希望有一种更快的方法来做这件事,它可能不依赖于系统,或者使用更少的执行。也许还有另一种更有效的方法来获取同样的信息。