diff --git a/.gitignore b/.gitignore index 1b8f5f6..499cb4a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/* ._* -version.js +test.js +git-version.js diff --git a/README.md b/README.md index 40cd32d..7f83c9c 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,60 @@ -## node-git-version +# node-git-version -This module creates version.js file in current directory containing object with following information: - - *tag* - Git tag attached to HEAD (if any) - *hash* - SHA-1 hash of HEAD - *timestamp* - current timestamp +[![npm](https://img.shields.io/npm/v/node-git-version.svg)](https://www.npmjs.com/package/node-git-version) + +This module creates git-version.js file in the current directory containing an object with following informations: + + - **commit** - SHA-1 hash of HEAD + - **shortCommit** - First 7 characters of the commit hash + - **tags** - Git tags attached to HEAD + - **branch** - Current branch tracked by HEAD + - **remoteBranch** - Branch on remote repository + - **author** - Raw string of the commit author + - **authorName** - Author name extracted from commit author + - **authorMail** - Author mail extracted from commit author + - **merge** - True if this commit merges commits + - **mergeCommits** - Array of short commit hashes merged + - **date** - An object with : + - **timestamp** - The unix timestamp of the commit (in seconds) + - **gmt** - The GMT formated date string + - **iso** - The ISO 8601 formated date string + - **summary** - The first line of the commit message + - **message** - The entire commit message This information is presented as Node.js module and can be used by Node.js app for getting version information - + ## Usage ``` -$ npm install -g node-git-version -$ // Go to your repo -$ node-git-version -$ cat version.js - -module.exports = { - tag: '1.0.0' - hash: 'e037765' - timestamp: 1425721222 -}; +$ npm install node-git-version +``` + +Sample test file : +```js +var gitVersion = require('node-git-version'); + +gitVersion(function (err, data) { + if (err) return console.error(err); + console.log(JSON.stringify(data, null, 2)); +}, null); +``` + +Output : +```js +{ + "commit": "de8f6b90ab190e6f2e95d1663cfa61186d269d67", + "shortCommit": "de8f6b9", + "tags": [], + "branch": " master", + "author": "BilliAlpha ", + "authorName": "BilliAlpha", + "authorMail": "billi.pamege.300@gmail.com", + "date": { + "timestamp": 1527347037, + "iso": "2018-05-26T15:03:57.000Z", + "gmt": "Sat, 26 May 2018 15:03:57 GMT" + }, + "summary": "Fix various errors", + "message": "Fix various errors\n\nNow it works !\n" +} ``` diff --git a/index.js b/index.js index 4bd362c..d00f1af 100644 --- a/index.js +++ b/index.js @@ -1,70 +1,122 @@ #!/usr/bin/env node /** - * This module creates version.js file in current directory - * containing object with following information: - * tag - Git tag attached to HEAD (if any) - * hash - SHA-1 hash of HEAD - * timestamp - current timestamp + * This module creates git-version.js file in the current directory + * containing an object with following informations: + * commit - SHA-1 hash of HEAD + * shortCommit - First 7 characters of the commit hash + * tags - Git tags attached to HEAD + * branch - Current branch tracked by HEAD + * remoteBranch - Branch on remote repository + * author - Raw string of the commit author + * authorName - Author name extracted from commit author + * authorMail - Author mail extracted from commit author + * merge - True if this commit merges commits + * mergeCommits - Array of short commit hashes merged + * date - An object with : + * timestamp - The unix timestamp of the commit (in seconds) + * gmt - The GMT formated date string + * iso - The ISO 8601 formated date string + * summary - The first line of the commit message + * message - The entire commit message * * This information is presented as Node.js module and can be used * by Node.js app for getting version information * * Created: Maxim Stepanov * Date: March 2015 + * Modified: BilliAlpha + * Date: May 2018 */ - var fs = require('fs'); - var exec = require('child_process').exec; - var child = exec('git reflog --decorate -1', function (error, stdout, stderr) - { - if (error) - { - // Shit - console.log('[FAILED]: Failed to run Git command'); - process.exit(1); - } +var fs = require('fs'); +var exec = require('child_process').exec; - // Example output: a32d6d8 (HEAD, tag: TAG-V.02, tag: TAG-V.01, master) HEAD@{0}: commit (initial): Asd - // Run regular expression to extract sha and tag - var sha = stdout.match(/[a-z0-9]+\s\(HEAD/g); - if (sha && sha.length > 0) - { - sha = sha[0].slice(0, -6); - } +module.exports = function (callback) { + var versionInfo = {}; - var tag = stdout.match(/tag\:\s[a-zA-Z0-9\-\.]+\,/g); - if (tag && tag.length > 0) - { - tag = tag[0].slice(5, -1); - } + var child = exec('git log --decorate -1 --date=unix', function (error, stdout, stderr) { + if (error) { // Shit + console.log('[Git-Version]: Failed to run Git command'); + if (callback) callback(error); + return; + } - // Compose version file info - var versionInfo = 'module.exports = {'; - - if (tag) - { - versionInfo += '\n\ttag: \'' + tag + '\','; - } - else - { - versionInfo += '\n\ttag: null,'; - } + // Example output: + // + // commit 6f78514ee4c055453ac8effba2680b5fd5304f04 (HEAD -> master, tag: v1.1.0, origin/master) + // Merge: db7fb4a 0b5a3e4 + // Author: BilliAlpha + // Date: 1527347037 + // + // Merge branch 'master' + // - versionInfo += '\n\thash: \'' + sha + '\','; - versionInfo += '\n\ttimestamp: ' + Math.floor(new Date().getTime()/1000); - versionInfo += '\n};\n'; + var data = stdout.split('\n'); - // Create version.js file - fs.writeFile('version.js', versionInfo, function(err) - { - if(err) - { - console.log('[FAILED]: can\'t create version.js file. Permission issue?'); + // Run regular expression to extract firstLine parts + var firstLine = data[0].match(/commit ([a-z0-9]{40})(?:\s\((.+)\))?/); + if (!firstLine || firstLine.length<2) { + if (callback) callback("Invalid log entry"); + return; } - else - { - console.log('[OK]'); + + // Get commit id + versionInfo.commit = firstLine[1].substr(0,40); + versionInfo.shortCommit = versionInfo.commit.substr(0,7); + + // Parse decorate infos + versionInfo.tags = []; + if (firstLine.length > 2) { + var decorate = firstLine[2].split(','); + for (var d=0; d')) { + versionInfo.branch = e.substr(7); + } else if (!versionInfo.remoteBranch && e.indexOf('/')!==-1) { + versionInfo.remoteBranch = e; + } + } } + + for (var l=1; l (https://prizma.mobi)", "license": "MIT", "repository" : {