2017-12-19 12 views
0

실제 프로덕션 환경에서 무엇이 커밋되었는지 알기 위해 관리자에게 Git 로그를 표시합니다. 이는 임시 변경 로그 역할을합니다. 우리가 쉼표로 분리 된 JSON 문자열을 얻을git log in JSON - 브라우저에 changelog 보내기

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 배열을하지 않습니다

은 내가 Node.js를 Express 서버에이 루틴이있다.

나는이 정보는 여기에서 형성되었다 : https://gist.github.com/varemenos/e95c2e098e657c7688fd https://git-scm.com/docs/pretty-formats

사람이 내가 망할 놈의 명령의 표준 출력에서 ​​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 
    }) 
    }) 

}); 

내가 다른 프로젝트에서 사용하기 때문에 문제를 일으키는 표준 출력에서 ​​오는 JSON의 형식에 대해 뭔가 그래서 내 파서, 작품을 변환 -의 데이터를 '이벤트 핸들러는 데이터를 전혀 보지 못합니다.

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는 다음과 같습니다 :

이 나를 위해 일
+1

포맷 문자열을 통해 로그에서 추출하는 많은 필드 중 *에는 JSON에 "특별한"문자가 포함될 수 있습니다. 예를 들어, 커밋 대상에 큰 따옴표가 들어 있다고 가정합니다. 지금 무슨 일이 일어날까요? JSON을 생성 할 의도가 전혀없는 도구에서 문자열 표현을 추출하는 대신 Javascript로 표현하려는 객체를 작성한 다음 문자열로 표현하는 것이 훨씬 더 좋습니다. –

+0

네, 맞습니다. 그래서 내가 원하는 필드를 얻고 공백으로 나눕니다. 나는 각 필드의 순서가 무엇인지 알 필요가 있다고 생각하고 객체에 넣을 수 있습니다. 간단한 예제를 보여 주면 나는 upvote 할 것이다. –

+0

예 수동으로 파싱하는 것이 어렵다는 것을 깨달았습니다. JSON을 사용하여 간단히 --pretty = 형식을 단순화했습니다. –

답변

0

, 나는 자식 로그 형식 옵션을 개선했다

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; 
}; 

망할 놈의 로그에 특정 문자가있는 경우이 발프 수 필드와 같은 저자,하지만 난 내장 된 필드 "sanitized_subject_line"때 우리의 피부를 저장 비트에서 나쁜 메시지를 제거하는 것입니다 커밋 메시지.