-
Notifications
You must be signed in to change notification settings - Fork 6
/
gitremote.js
47 lines (41 loc) · 1.08 KB
/
gitremote.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
/* global exports, process */
var child_process = require('child_process');
exports.getRemoteUrl = function getRemoteUrl(options) {
if (!options) {
options = {};
}
var cwd = options.cwd || process.cwd();
var uri = child_process.execSync(
'git config remote.' + (options.origin || 'origin') + '.url',
{cwd: cwd}
).toString().trim();
if (!uri) {
throw new Error('Can not get remote url from dir: ' + cwd);
}
return uri;
};
exports.getCurrentBranch = function getCurrentBranch(cwd) {
var cmds = [
'git symbolic-ref --short HEAD',
'git rev-parse --abbrev-ref HEAD',
];
for (var i = 0, l = cmds.length; i < l; i++) {
try {
var out = child_process.execSync(cmds[i], {cwd: cwd})
.toString().trim();
if (out.indexOf('fatal: ') === 0) {
return null;
}
return out;
} catch (ex) { /* */ }
}
return null;
};
exports.getGitRootPath = function(cwd) {
try {
return child_process.execSync('git rev-parse --show-toplevel', {cwd: cwd})
.toString().trim();
} catch (ex) {
return null;
}
};