forked from prizma/node-git-version
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (110 loc) · 4.03 KB
/
index.js
File metadata and controls
122 lines (110 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
/**
* 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;
module.exports = function (callback) {
var versionInfo = {};
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;
}
// Example output:
//
// commit 6f78514ee4c055453ac8effba2680b5fd5304f04 (HEAD -> master, tag: v1.1.0, origin/master)
// Merge: db7fb4a 0b5a3e4
// Author: BilliAlpha <???>
// Date: 1527347037
//
// 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})(?:\s\((.+)\))?/);
if (!firstLine || firstLine.length<2) {
if (callback) callback("Invalid log entry");
return;
}
// 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<decorate.length; d++) {
var e = decorate[d].trim();
// console.log("Decorate "+d+": "+e); // Debug
if (e.startsWith('tag: ')) { // Get tags
versionInfo.tags.push(e.substring(5));
} else if (e.startsWith('HEAD ->')) {
versionInfo.branch = e.substr(7);
} else if (!versionInfo.remoteBranch && e.indexOf('/')!==-1) {
versionInfo.remoteBranch = e;
}
}
}
for (var l=1; l<data.length; l++) {
var line = data[l];
if (line.startsWith('Merge:')) {
versionInfo.merge = true;
var mergeCommits = line.substring(-15).split(' ');
versionInfo.mergeCommits = mergeCommits;
} else if (line.startsWith('Author:')) {
versionInfo.author = line.split(':')[1].trim();
var authorParts = versionInfo.author.split('<');
versionInfo.authorName = authorParts[0].trim()
versionInfo.authorMail = authorParts[1].slice(0,-1);
} else if (line.startsWith('Date:')) {
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';
} else if (versionInfo.message && line.startsWith(' ')) {
versionInfo.message += line.substring(4)+'\n';
}
}
// Call callback
if (callback) callback(null, versionInfo);
// Compose version file info
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) {
console.warn('[Git-Version]: Can\'t create git-version.js file. Permission issue?');
}
});
});
}