-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
216 lines (171 loc) · 5.67 KB
/
index.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/usr/bin/env node
var Glob = require('glob');
var Path = require('path');
var Promise = require('bluebird');
var ChildProcessPromise = require('child-process-promise').exec;
var Colors = require('colors/safe');
console.log('-> Locating git repositories in', process.cwd(), '...');
var filePaths = Glob.sync(Path.resolve(process.cwd()) + '/**/.git').map((s) => s.replace('/.git', ''));
console.log('Found', filePaths.length, 'git repositories');
Promise.mapSeries(filePaths, function (filePath) {
filePath = Path.resolve(__dirname, filePath);
console.log('##################################################################################################');
console.log('-> Verifying repository', filePath);
var remoteBranchesPromise = Promise.try(function() {
console.log('-> Fetching all remote branches ...');
return Exec(
'git fetch --all --prune', {
cwd: filePath
}
).then(function (stdout, stderr) {
return Exec('git branch -r', { cwd: filePath });
}).then(function (stdout, stderr) {
var branches = stdout
.split('\n')
.map((s) => s.trim())
.filter((s) => s !== 'origin/HEAD -> origin/master')
.filter(Boolean)
.map((s) => s.split('/')[1]);
return Promise.resolve(branches);
});
});
var currentBranchPromise = remoteBranchesPromise.then(function(branches) {
console.log('-> Checking current branch ...');
var branchTracksPromise = branches.map(function(branch) {
return Exec('git branch --track '+ branch + ' origin/' + branch, {
cwd: filePath
}).catch(function() {
// Ignores branch already exists errors
});
});
return Promise.all(branchTracksPromise);
}).then(function (stdout, stderr) {
return Exec(
'git show -s --pretty=%d HEAD', {
cwd: filePath
}
);
}).then(function (stdout, stderr) {
var candidateBranches = stdout
.split(',')
.map((s) => s.trim())
.map((s) => s.replace(/\)|\(/g, ''))
.map((s) => s.replace('HEAD -> ', ''))
.filter((s) => s !== 'HEAD')
.map((s) => {
return s.split('/').length > 1 ? s.split('/')[1] : s;
});
if (candidateBranches.length === 0) {
// Could not detect with first command
// Attempt a second command
return Exec(
'git branch -a --contains HEAD', {
cwd: filePath
}).then(function (stdout2, stderr2) {
candidateBranches = stdout2
.split(/\r\n|\r|\n/g)
.map((s) => s.trim())
.map(function(s) {
if (s.indexOf('/') !== -1) {
var split = s.split('/');
return split[split.length - 1];
}
return s;
}).sort(function (a, b) {
if (a.indexOf('*') !== -1) {
return -1;
}
if (b.indexOf('*') !== -1) {
return 1;
}
if (a === 'master') {
return -1;
}
if (b === 'master') {
return 1;
}
return a > b ? 1 : -1;
})
.map((s) => s.replace('* ', ''))
.filter(Boolean)
.filter((c) => c.indexOf('HEAD detached at') === -1);
if (candidateBranches.length > 0) {
return Promise.resolve(candidateBranches[0]);
}
return Promise.resolve(null);
});
}
return Promise.resolve(candidateBranches[0]);
}).then(function (currentBranch) {
if (currentBranch === null) {
throw Error('Could not determine current branch');
}
console.log('-> Your current ' + Colors.underline('LOCAL') + ' branch is:');
console.log(Colors.bgCyan('* ' + currentBranch));
return Promise.resolve(currentBranch);
});
return Promise.all(
[remoteBranchesPromise, currentBranchPromise]
).spread(function (branches, currentBranch) {
console.log('-> Found ' + Colors.underline('REMOTE') + ' branches:');
branches.forEach(function (branch) {
if (branch !== currentBranch) {
console.log(Colors.inverse('- ' + branch));
} else {
console.log(Colors.bgCyan('* ' + branch));
}
});
return Promise.mapSeries(branches, function (branch) {
console.log('-> Checking branch ' + branch + ':');
var checkBehindRemotePromise = Exec(
'git rev-list HEAD...origin/' + branch + ' --count', {
cwd: filePath
}
).then(function (stdout, stderr) {
var behind = parseInt(stdout);
if (behind > 0) {
if (branch === currentBranch) {
console.warn(Colors.red('Your local branch', branch, 'is behind remote for', behind, 'commits'));
} else {
console.log('Your local branch', branch, 'is behind remote for', behind, 'commits');
}
} else {
console.log('Your local branch', branch, 'is up to date with remote');
}
});
var checkBehindMasterPromise = Promise.try(function () {
if (branch === 'master') {
return Promise.resolve();
}
return Exec(
'git rev-list --left-right --count origin/master...origin/' + branch, {
cwd: filePath
}
).then(function (stdout, stderr) {
var numbers = stdout.split(/[ \n\t]/g).filter(Boolean);
var behindMaster = parseInt(numbers[0]);
var ahead = parseInt(numbers[1]);
if (behindMaster > 0) {
if (branch === currentBranch) {
console.warn(Colors.red('Remote branch', branch, 'is behind remote master for', behindMaster, 'commits'));
} else {
console.log(Colors.yellow('Remote branch', branch, 'is behind remote master for', behindMaster, 'commits'));
}
} else {
console.log('Remote branch', branch, 'is ahead of master for', ahead, 'commits');
}
});
});
return Promise.all([checkBehindRemotePromise, checkBehindMasterPromise]);
});
}).catch(function (err) {
console.error(Colors.red('ERROR: ' + err.message));
});
});
function Exec() {
return ChildProcessPromise.apply(this, arguments).then(function (result) {
var stderr = result.stderr;
var stdout = result.stdout;
return Promise.resolve(stdout, stderr);
});
}