diff options
author | HampusM <hampus@hampusmat.com> | 2021-04-16 23:05:12 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-04-16 23:05:12 +0200 |
commit | 0b41f38661d4ce3f90ad4df7cc5a17efae8352aa (patch) | |
tree | defc5d71613985381dcd03eec081b6eb7b0629ca /api/git.js | |
parent | ef256e9e40b5fd0cc741c509e611808cc66bafad (diff) |
Added basic backend code, two api endpoints & sanitization
Diffstat (limited to 'api/git.js')
-rw-r--r-- | api/git.js | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/api/git.js b/api/git.js new file mode 100644 index 0000000..b8d89c3 --- /dev/null +++ b/api/git.js @@ -0,0 +1,62 @@ +const { exec } = require("child_process"); + +const log_format='{"hash": "%H", "author": "%an", "author_email": "%ae", "date": "%at", "subject": "%s"}' + +function getLog(path) +{ + return new Promise((resolve) => + { + exec(`git -C ${path} log --format=format:'${log_format}'`, (error, stdout, stderr) => + { + if(error) { + const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`); + + if(no_such_fileor_dir.test(error.message)) { + resolve({ "error": 404 }); + return; + } + resolve({ "error": error.message }); + return; + } + if(stderr) { + resolve({ "error": "Failed to communicate with git!" }); + return; + } + + const log = stdout.split('\n'); + log.forEach((entry, index) => log[index] = JSON.parse(entry)); + + resolve({ "data": log }); + }); + }); +} + +function getTimeSinceLatestCommit(path) +{ + return new Promise((resolve) => + { + exec(`git -C ${path} log -n 1 --date=unix'`, (error, stdout, stderr) => + { + if(error) { + const no_such_fileor_dir = new RegExp(`cannot change to '${path.replace('/', "\/")}': No such file or directory\\n$`); + + if(no_such_fileor_dir.test(error.message)) { + resolve({ "error": 404 }); + return; + } + resolve({ "error": error.message }); + return; + } + if(stderr) { + resolve({ "error": "Failed to communicate with git!" }); + return; + } + + const + + resolve({ "data": log }); + }); + }); +} + +module.exports.getLog = getLog;
\ No newline at end of file |