为了知道生产中的项目实际上是什么,我们将向管理员显示Git日志 - 这可以作为临时更改日志。
我在Node.js Express服务器上有这个例程:
router.get('/changelog/json', ac.allow('ROLE_ADMIN'), function (req, res, next) {
const k = cp.spawn('bash');
k.stdin.write('git log --pretty=format:\'{%n "commit": "%H",%n "abbreviated_commit": "%h",%n "tree": "%T",%n "abbreviated_tree": "%t",%n "parent": "%P",%n "abbreviated_parent": "%p",%n "refs": "%D",%n "encoding": "%e",%n "subject": "%s",%n "sanitized_subject_line": "%f",%n "body": "%b",%n "commit_notes": "%N",%n "verification_flag": "%G?",%n "signer": "%GS",%n "signer_key": "%GK",%n "author": {%n "name": "%aN",%n "email": "%aE",%n "date": "%aD"%n },%n "commiter": {%n "name": "%cN",%n "email": "%cE",%n "date": "%cD"%n }%n},\'\n')
k.stdin.end();
k.stdout.pipe(res);
});
这类工作,但我们实际上并没有得到一个JSON数组,我们只是用逗号分隔JSON字符串。
我在这里收到了这个信息表: https://gist.github.com/varemenos/e95c2e098e657c7688fd https://git-scm.com/docs/pretty-formats
有谁知道如何从Git命令的stdout构造一个JSON数组?
我试过了:
router.get('/json', ac.allow('ROLE_ADMIN'), function (req, res, next) {
const p = createParser();
const k = cp.spawn('bash', [], {
cwd: global.cdtProjectRoot
});
const items = [];
k.stdin.write('git log --pretty=format:\'{%n "commit": "%H",%n "abbreviated_commit": "%h",%n "tree": "%T",%n "abbreviated_tree": "%t",%n "parent": "%P",%n "abbreviated_parent": "%p",%n "refs": "%D",%n "encoding": "%e",%n "subject": "%s",%n "sanitized_subject_line": "%f",%n "body": "%b",%n "commit_notes": "%N",%n "verification_flag": "%G?",%n "signer": "%GS",%n "signer_key": "%GK",%n "author": {%n "name": "%aN",%n "email": "%aE",%n "date": "%aD"%n },%n "commiter": {%n "name": "%cN",%n "email": "%cE",%n "date": "%cD"%n }%n},\'\n')
k.stdin.end();
k.stdout.pipe(p).on('data', function (d) {
// d would be a parsed JSON object
items.push(d);
})
.once('error', next)
.once('end', function () {
res.json({
success: items
})
})
});
我的解析器转换有效,因为我在另一个项目中使用它,所以它是来自stdout的JSON格式导致问题 - 'data'事件处理程序永远不会看到任何数据。
答案 0 :(得分:0)
这对我有用,我不得不改进git log格式选项:
router.get('/json', ac.allow('ROLE_ADMIN'), function (req, res, next) {
const p = createParser();
const k = cp.spawn('bash');
let items = [];
k.stdin.write(`git log -300 --pretty=format:'{"commit":"%H","sanitized_subject_line":"%f","commit_notes":"%N","author":"%aN","date":"%aD"}'`);
k.stdin.end('\n');
k.stdout.pipe(p).on('data', function (d) {
items.push(JSON.parse(d));
})
.once('error', next)
.once('end', function () {
res.json({
success: items
})
})
});
createParser看起来像这样:
const stream = require("stream");
exports.createParser = function () {
let lastLineData = '';
let strm = new stream.Transform({
objectMode: true,
transform: function (chunk, encoding, cb) {
let _this = this;
let data = String(chunk);
if (lastLineData) {
data = lastLineData + data;
}
let lines = data.split('\n');
lastLineData = lines.splice(lines.length - 1, 1)[0];
lines.forEach(function (l) {
l && _this.push(l);
});
cb();
},
flush: function (cb) {
if (lastLineData) {
this.push(lastLineData);
}
lastLineData = '';
cb();
}
});
return strm;
};
如果git日志字段中有某些字符(例如Author),这可能是barf,但我认为内置字段“sanitized_subject_line”会在从提交消息中删除坏字符时保存我们的皮肤。