Last active
April 1, 2019 01:00
-
-
Save machsix/45fd59bc7d5a16931b534dd8c38be93f to your computer and use it in GitHub Desktop.
npm-git-log.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {spawnSync} = require("child_process"); | |
const path = require("path"); | |
const gitLog = function(cwd, obj, magicKey) { | |
magicKey = "^^" || magicKey; | |
cwd = cwd || "."; | |
const args = ["log"]; | |
let logFormat = { | |
commit: "%H", | |
abbreviated_commit: "%h", | |
date: "%cD", | |
committer: "%cn", | |
author_date: "%aD", | |
author: "%an" | |
}; | |
if (obj) { | |
if (obj.hasOwnProperty("nCommit")) { | |
args.push(`-${obj.nCommit}`); | |
} | |
if (obj.hasOwnProperty("logFormat")) { | |
logFormat = obj.logFormat; | |
} | |
} | |
let strLogFormat = "{%n"; | |
for (const prop in logFormat) { | |
if (typeof logFormat[prop] === "string") { | |
strLogFormat += ` "${prop}": "${logFormat[prop]}",%n`; | |
} | |
} | |
strLogFormat = strLogFormat.slice(0, strLogFormat.length - 3) + `%n}${magicKey}`; | |
args.push(`--pretty=format:${strLogFormat}`); | |
args.push("HEAD"); | |
if (obj) { | |
if (obj.hasOwnProperty("fileList")) { | |
args.push("--"); | |
obj.fileList.forEach((x) => { | |
args.push(x); | |
}); | |
} | |
} | |
const out = spawnSync("git", args, { | |
cwd: path.resolve(process.cwd(), cwd), | |
encoding: "utf8" | |
}); | |
const commitInfoArray = out.stdout.split(magicKey).filter((val) => val.length > 0); | |
let commitInfo = "[\n"; | |
commitInfoArray.forEach((val, i) => { | |
if (i < commitInfoArray.length - 1) { | |
commitInfo += `${val},`; | |
} else { | |
commitInfo += `${val}\n]`; | |
} | |
}); | |
return JSON.parse(commitInfo); | |
}; | |
module.exports = { | |
gitLog: gitLog | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { execSync } = require('child_process'); | |
function gitLog(cwd, obj, magicKey) { | |
magicKey = '^^'||magicKey; | |
cwd = cwd||'.'; | |
let logFormat = { | |
commit: "%H", | |
abbreviated_commit: "%h", | |
date: "%cD", | |
committer: "%cn", | |
author_date: "%aD", | |
author: "%an" | |
}; | |
let nCommit = ""; | |
let fileList = ""; | |
if (obj) { | |
if (obj.hasOwnProperty('logFormat')) { | |
logFormat = obj.logFormat; | |
} | |
if (obj.hasOwnProperty('nCommit')) { | |
nCommit = `-n ${obj.nCommit}`; | |
} | |
if (obj.hasOwnProperty('fileList')) { | |
const fileList = " " + obj.fileList.join(" "); | |
} | |
} | |
let strLogFormat = '{%n'; | |
for (const prop in logFormat) { | |
strLogFormat += ` \\"${prop}\\": \\"${logFormat[prop]}\\",%n`; | |
} | |
strLogFormat = strLogFormat.slice(0, strLogFormat.length-3) + `%n}${magicKey}`; | |
const cmd = `git log ${nCommit} --pretty=format:"${strLogFormat}"${fileList}` | |
let stdout = execSync(cmd, {cwd: cwd, encoding: "utf8"}); | |
commitInfoArray = stdout.split(magicKey).filter((val) => val.length > 0); | |
let commitInfo = '[\n'; | |
commitInfoArray.forEach((val,i) => { | |
if (i < commitInfoArray.length-1) { | |
commitInfo += `${val},`; | |
} else { | |
commitInfo += `${val}\n]`; | |
} | |
}) | |
return JSON.parse(commitInfo); | |
} | |
const commitInfo = gitLog('../', {fileList: ['mydata.json']}); | |
console.log(commitInfo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment