-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit.js
77 lines (71 loc) · 1.93 KB
/
git.js
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
'use strict';
var exec = require('child_process').exec;
var url = require('url');
module.exports = {
name: 'git',
info : gitInfo,
status : gitStatus
};
function gitStatus(cwd, callback) {
exec('git status -s', {cwd: cwd},
function (error, stdout, stderr) {
var changes = (stdout || '').split('\n');
for( var i = changes.length-1; i >= 0; i-- ) {
if( changes[i] === '' ) {
changes.splice(i, 1);
}
}
callback(error, changes);
}
);
}
function gitInfo(cwd, callback) {
var c = 0;
var resp = {};
function onResp(key, text) {
if( typeof key === 'object' ) {
for( var i in key ) {
resp[i] = key[i];
}
} else {
resp[key] = text;
}
c++;
if( c === 5 ) {
callback(resp);
}
}
exec('git rev-parse --show-toplevel', {cwd: cwd},
function (error, stdout, stderr) {
onResp('networkDataPath', cwd.replace(stdout.replace(/\n/,''), ''));
}
);
exec('git describe --tags', {cwd: cwd},
function (error, stdout, stderr) {
onResp('tag', stdout.replace(/\n/,''));
}
);
exec('git branch | grep \'\\*\'', {cwd: cwd},
function (error, stdout, stderr) {
onResp('branch', stdout ? stdout.replace(/\*/,'').replace(/\s/g,'') : '');
}
);
exec('git log -1 | sed -n 1p', {cwd: cwd},
function (error, stdout, stderr) {
onResp('commit', stdout ? stdout.replace(/commit\s/,'').replace(/\n/g,'') : '');
}
);
exec('git config --get remote.origin.url', {cwd: cwd},
function (error, stdout, stderr) {
if( !stdout ) {
onResp('origin', '');
} else {
var urlInfo = url.parse(stdout);
onResp({
origin : urlInfo.hostname,
repository : urlInfo.path.replace(/^\//,'').replace(/.git$/,'')
});
}
}
);
}