Merge pull request #1540 from hedgedoc/fix/get_git_commit
This commit is contained in:
@@ -25,16 +25,29 @@ exports.toIntegerConfig = function toIntegerConfig (configValue) {
|
||||
}
|
||||
|
||||
exports.getGitCommit = function getGitCommit (repodir) {
|
||||
if (!fs.existsSync(repodir + '/.git/HEAD')) {
|
||||
return undefined
|
||||
try {
|
||||
// prefer using git to get the current ref, as poking in .git is very fragile
|
||||
return require('child_process').execSync('git rev-parse HEAD')
|
||||
} catch (e) {
|
||||
// there was an error running git, try to parse refs ourselves
|
||||
if (!fs.existsSync(repodir + '/.git/HEAD')) {
|
||||
// there is no HEAD information
|
||||
return undefined
|
||||
}
|
||||
let reference = fs.readFileSync(repodir + '/.git/HEAD', 'utf8')
|
||||
if (reference.startsWith('ref: ')) {
|
||||
// HEAD references another ref, try to get the commit SHA from .git/ref/heads
|
||||
reference = reference.substr(5).replace('\n', '')
|
||||
const refPath = path.resolve(repodir + '/.git', reference)
|
||||
if (!fs.existsSync(refPath)) {
|
||||
// ref does not exist in .git/ref/heads
|
||||
return undefined
|
||||
}
|
||||
reference = fs.readFileSync(refPath, 'utf8')
|
||||
}
|
||||
reference = reference.replace('\n', '')
|
||||
return reference
|
||||
}
|
||||
let reference = fs.readFileSync(repodir + '/.git/HEAD', 'utf8')
|
||||
if (reference.startsWith('ref: ')) {
|
||||
reference = reference.substr(5).replace('\n', '')
|
||||
reference = fs.readFileSync(path.resolve(repodir + '/.git', reference), 'utf8')
|
||||
}
|
||||
reference = reference.replace('\n', '')
|
||||
return reference
|
||||
}
|
||||
|
||||
exports.getGitHubURL = function getGitHubURL (repo, reference) {
|
||||
|
||||
Reference in New Issue
Block a user