From 62ef2e02dd9321fbfcdae72565f2cf77150172a3 Mon Sep 17 00:00:00 2001 From: BilliAlpha Date: Sat, 26 May 2018 12:39:57 +0200 Subject: [PATCH 1/6] Parse more infos --- index.js | 125 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 78 insertions(+), 47 deletions(-) diff --git a/index.js b/index.js index 4bd362c..2dc69e2 100644 --- a/index.js +++ b/index.js @@ -12,59 +12,90 @@ * * 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); - } - - var tag = stdout.match(/tag\:\s[a-zA-Z0-9\-\.]+\,/g); - if (tag && tag.length > 0) - { - tag = tag[0].slice(5, -1); - } - - // Compose version file info - var versionInfo = 'module.exports = {'; +module.exports = function (opts) { + var versionInfo = {}; - if (tag) - { - versionInfo += '\n\ttag: \'' + tag + '\','; - } - else - { - versionInfo += '\n\ttag: null,'; - } + var child = exec('git log --decorate -1 --date=unix', function (error, stdout, stderr) { + if (error) { + // Shit + console.log('[FAILED]: Failed to run Git command'); + process.exit(error.code); + } - versionInfo += '\n\thash: \'' + sha + '\','; - versionInfo += '\n\ttimestamp: ' + Math.floor(new Date().getTime()/1000); - versionInfo += '\n};\n'; + // Example output: + // + // commit 6f78514ee4c055453ac8effba2680b5fd5304f04 (HEAD -> master, tag: v1.1.0, origin/master) + // Merge: db7fb4a 0b5a3e4 + // Author: BilliAlpha + // Date: Fri May 25 21:50:04 2018 +0200 + // + // Merge branch 'master' + // + + var data = stdout.split('\n'); + + // Run regular expression to extract firstLine parts + var firstLine = data[0].match(/commit ([a-z0-9]{40})(?: \((.+)\))?/g); + + // Get commit id + if (firstLine && firstLine.length > 0) { + versionInfo.commit = firstLine[0]; + versionInfo.shortCommit = versionInfo.commit.slice(0,7); + } - // Create version.js file - fs.writeFile('version.js', versionInfo, function(err) - { - if(err) - { - console.log('[FAILED]: can\'t create version.js file. Permission issue?'); + // Parse decorate infos + versionInfo.tags = []; + if (firstLine && firstLine.length > 1) { + var decorate = firstLine[1].split(','); + for (var d=0; d'); + if (arrow!==-1) { + versionInfo.branch = e.substring(arrow+1); + } + } else if (d===decorate.length-1) { + versionInfo.remoteBranch = e; + } + } } - else - { - console.log('[OK]'); + + for (var l=1; l Date: Sat, 26 May 2018 12:41:38 +0200 Subject: [PATCH 2/6] Update package.json --- package.json | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 711719b..07f9508 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,10 @@ { "name": "node-git-version", "description": "Node.js application for creating version file of git repository", - "version": "0.1.1", + "version": "1.0.0", "main": "index.js", "bin": "index.js", - "dependencies": - { - }, + "dependencies": {}, "keywords": [ "git", "nodejs", @@ -14,11 +12,11 @@ "version", "prizma" ], - "author": "maxxie (https://prizma.mobi)", + "author": "BilliAlpha ", "license": "MIT", "repository" : { "type" : "git", - "url" : "https://github.com/prizma/node-git-version.git" + "url" : "https://github.com/BilliAlpha/node-git-version.git" } } From 416b42aa731a4eb63c33c0352ac0c33790d11035 Mon Sep 17 00:00:00 2001 From: BilliAlpha Date: Sat, 26 May 2018 12:52:26 +0200 Subject: [PATCH 3/6] Add callback to function And change package name --- index.js | 27 ++++++++++++++------------- package.json | 2 +- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 2dc69e2..725f1b8 100644 --- a/index.js +++ b/index.js @@ -19,14 +19,14 @@ var fs = require('fs'); var exec = require('child_process').exec; -module.exports = function (opts) { +module.exports = function (callback, opts) { var versionInfo = {}; - + var child = exec('git log --decorate -1 --date=unix', function (error, stdout, stderr) { - if (error) { - // Shit - console.log('[FAILED]: Failed to run Git command'); - process.exit(error.code); + if (error) { // Shit + console.log('[Git-Version]: Failed to run Git command'); + if (callback) callback(error); + return; } // Example output: @@ -38,12 +38,12 @@ module.exports = function (opts) { // // Merge branch 'master' // - + var data = stdout.split('\n'); - + // Run regular expression to extract firstLine parts var firstLine = data[0].match(/commit ([a-z0-9]{40})(?: \((.+)\))?/g); - + // Get commit id if (firstLine && firstLine.length > 0) { versionInfo.commit = firstLine[0]; @@ -83,18 +83,19 @@ module.exports = function (opts) { } else if (line.startsWith('Date:')) { versionInfo.date = line.split(':')[1].trim(); } else if (line.startsWith(' ')) { - versionInfo.message = line; + versionInfo.message = line; } } + // Call callback + if (callback) callback(null, versionInfo); + // Compose version file info var fileContents = 'module.exports = '+JSON.stringify(versionInfo, null, 2)+';\n'; // Create git-version.js file fs.writeFile('git-version.js', fileContents, function(err) { if(err) { - console.log('[FAILED]: can\'t create git-version.js file. Permission issue?'); - } else { - console.log('[OK]'); + console.warn('[Git-Version]: Can\'t create git-version.js file. Permission issue?'); } }); }); diff --git a/package.json b/package.json index 07f9508..96881c6 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "node-git-version", + "name": "node-git-version-infos", "description": "Node.js application for creating version file of git repository", "version": "1.0.0", "main": "index.js", From de8f6b90ab190e6f2e95d1663cfa61186d269d67 Mon Sep 17 00:00:00 2001 From: BilliAlpha Date: Sat, 26 May 2018 17:03:57 +0200 Subject: [PATCH 4/6] Fix various errors Now it works ! --- .gitignore | 3 ++- index.js | 37 ++++++++++++++++++++----------------- package-lock.json | 5 +++++ package.json | 3 +-- 4 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 package-lock.json 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/index.js b/index.js index 725f1b8..8f0a5d2 100644 --- a/index.js +++ b/index.js @@ -42,28 +42,28 @@ module.exports = function (callback, opts) { var data = stdout.split('\n'); // Run regular expression to extract firstLine parts - var firstLine = data[0].match(/commit ([a-z0-9]{40})(?: \((.+)\))?/g); + var firstLine = data[0].match(/commit ([a-z0-9]{40})(?:\s\((.+)\))?/); + if (!firstLine || firstLine.length<2) { + if (callback) callback("Invalid log entry"); + return; + } // Get commit id - if (firstLine && firstLine.length > 0) { - versionInfo.commit = firstLine[0]; - versionInfo.shortCommit = versionInfo.commit.slice(0,7); - } + versionInfo.commit = firstLine[1].substr(0,40); + versionInfo.shortCommit = versionInfo.commit.substr(0,7); // Parse decorate infos versionInfo.tags = []; - if (firstLine && firstLine.length > 1) { - var decorate = firstLine[1].split(','); + if (firstLine.length > 2) { + var decorate = firstLine[2].split(','); for (var d=0; d'); - if (arrow!==-1) { - versionInfo.branch = e.substring(arrow+1); - } - } else if (d===decorate.length-1) { + } else if (e.startsWith('HEAD ->')) { + versionInfo.branch = e.substr(7); + } else if (!versionInfo.remoteBranch && e.indexOf('/')!==-1) { versionInfo.remoteBranch = e; } } @@ -79,11 +79,14 @@ module.exports = function (callback, opts) { versionInfo.author = line.split(':')[1].trim(); var authorParts = versionInfo.author.split('<'); versionInfo.authorName = authorParts[0].trim() - versionInfo.authorMail = authorParts[1].substring(0,-1); + versionInfo.authorMail = authorParts[1].slice(0,-1); } else if (line.startsWith('Date:')) { versionInfo.date = line.split(':')[1].trim(); - } else if (line.startsWith(' ')) { - versionInfo.message = line; + } else if (!versionInfo.message && line.startsWith(' ')) { + versionInfo.summary = line.substring(4); + versionInfo.message = versionInfo.summary+'\n'; + } else if (versionInfo.message && line.startsWith(' ')) { + versionInfo.message += line.substring(4)+'\n'; } } @@ -91,7 +94,7 @@ module.exports = function (callback, opts) { if (callback) callback(null, versionInfo); // Compose version file info - var fileContents = 'module.exports = '+JSON.stringify(versionInfo, null, 2)+';\n'; + var fileContents = 'module.exports = '+JSON.stringify(versionInfo, null, '\t')+';\n'; // Create git-version.js file fs.writeFile('git-version.js', fileContents, function(err) { if(err) { diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a0ef8f9 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,5 @@ +{ + "name": "node-git-version-infos", + "version": "1.0.0", + "lockfileVersion": 1 +} diff --git a/package.json b/package.json index 96881c6..d9f9147 100644 --- a/package.json +++ b/package.json @@ -9,8 +9,7 @@ "git", "nodejs", "node.js", - "version", - "prizma" + "version" ], "author": "BilliAlpha ", "license": "MIT", From 0224f57d05b787670da9d5a8ae857c9adccf9141 Mon Sep 17 00:00:00 2001 From: BilliAlpha Date: Sat, 26 May 2018 17:22:32 +0200 Subject: [PATCH 5/6] Return multiple date formats --- index.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8f0a5d2..243a3e8 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ var fs = require('fs'); var exec = require('child_process').exec; -module.exports = function (callback, opts) { +module.exports = function (callback) { var versionInfo = {}; var child = exec('git log --decorate -1 --date=unix', function (error, stdout, stderr) { @@ -81,7 +81,11 @@ module.exports = function (callback, opts) { versionInfo.authorName = authorParts[0].trim() versionInfo.authorMail = authorParts[1].slice(0,-1); } else if (line.startsWith('Date:')) { - versionInfo.date = line.split(':')[1].trim(); + versionInfo.date = {}; + var date = new Date(line.split(':')[1].trim()*1000) + versionInfo.date.timestamp = date.getTime()/1000; + versionInfo.date.iso = date.toISOString(); + versionInfo.date.gmt = date.toGMTString(); } else if (!versionInfo.message && line.startsWith(' ')) { versionInfo.summary = line.substring(4); versionInfo.message = versionInfo.summary+'\n'; From 57fd419ab8e9255c2609204eae3670b05f12e6ec Mon Sep 17 00:00:00 2001 From: BilliAlpha Date: Sat, 26 May 2018 17:51:49 +0200 Subject: [PATCH 6/6] Update doc --- README.md | 70 +++++++++++++++++++++++++++++++++++++++------------- index.js | 25 ++++++++++++++----- package.json | 25 ++++++++++++++----- 3 files changed, 91 insertions(+), 29 deletions(-) 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 243a3e8..d00f1af 100644 --- a/index.js +++ b/index.js @@ -1,11 +1,24 @@ #!/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 @@ -34,7 +47,7 @@ module.exports = function (callback) { // commit 6f78514ee4c055453ac8effba2680b5fd5304f04 (HEAD -> master, tag: v1.1.0, origin/master) // Merge: db7fb4a 0b5a3e4 // Author: BilliAlpha - // Date: Fri May 25 21:50:04 2018 +0200 + // Date: 1527347037 // // Merge branch 'master' // diff --git a/package.json b/package.json index d9f9147..20f7556 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "node-git-version-infos", + "name": "node-git-version", "description": "Node.js application for creating version file of git repository", "version": "1.0.0", "main": "index.js", @@ -7,15 +7,28 @@ "dependencies": {}, "keywords": [ "git", - "nodejs", - "node.js", - "version" + "version", + "branch", + "commit", + "author", + "date", + "tags" + ], + "contributors": [ + { + "name": "maxxie", + "email": "mast@imast.ru", + "web": "https://prizma.mobi" + }, { + "name": "BilliAlpha", + "email": "billi.pamege.300@gmail.com", + "web": "http://billi-dot.pe.hu" + } ], - "author": "BilliAlpha ", "license": "MIT", "repository" : { "type" : "git", - "url" : "https://github.com/BilliAlpha/node-git-version.git" + "url" : "https://github.com/prizma/node-git-version.git" } }