Skip to content

Instantly share code, notes, and snippets.

@lukin0110
Created August 26, 2015 15:06
Show Gist options
  • Save lukin0110/4ebe4462cef79d26e043 to your computer and use it in GitHub Desktop.
Save lukin0110/4ebe4462cef79d26e043 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
'use strict';
var gulp = require('gulp');
var spawn = require('child_process').spawn;
var GIT_VERSION = "na";
gulp.task('version', function(){
//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;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment