-
-
Save idflood/2ac2255320b0ed389b3ab627533ee674 to your computer and use it in GitHub Desktop.
Gulp git version, fetches the latest git commit hash. Can be used as version for your application. Inject/replace it in your html
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
'use strict'; | |
var gulp = require('gulp'); | |
var spawn = require('child_process').spawn; | |
var GIT_VERSION = "na"; | |
gulp.task('version', function(cb){ | |
//git --git-dir=.git log --pretty='%ct %h' -1 | |
//git --git-dir=.git log --pretty='%h' -1 | |
var child = spawn("git", ["--git-dir=.git", "log", "--pretty=%h", "-1"], {cwd: process.cwd()}), | |
stdout = '', | |
stderr = ''; | |
child.stdout.setEncoding('utf8'); | |
child.stdout.on('data', function (data) { | |
stdout += data; | |
}); | |
child.stderr.setEncoding('utf8'); | |
child.stderr.on('data', function (data) { | |
stderr += data; | |
}); | |
child.on('close', function(code) { | |
var normalized = stdout.replace(/(?:\r\n|\r|\n)/g, "") | |
console.log("Version: " + normalized); | |
GIT_VERSION = normalized; | |
cb(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added the callback (
cb
) and called it on close. Otherwise the task ordering may be wrong, and a task requiring this could have theGIT_VERSION
not defined.