1 | "use strict";
|
2 |
|
3 | const log = require("npmlog");
|
4 | const childProcess = require("@lerna/child-process");
|
5 |
|
6 | module.exports.isBehindUpstream = isBehindUpstream;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function isBehindUpstream(gitRemote, branch, opts) {
|
14 | log.silly("isBehindUpstream");
|
15 |
|
16 | updateRemote(opts);
|
17 |
|
18 | const remoteBranch = `${gitRemote}/${branch}`;
|
19 | const [behind, ahead] = countLeftRight(`${remoteBranch}...${branch}`, opts);
|
20 |
|
21 | log.silly(
|
22 | "isBehindUpstream",
|
23 | `${branch} is behind ${remoteBranch} by ${behind} commit(s) and ahead by ${ahead}`
|
24 | );
|
25 |
|
26 | return Boolean(behind);
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function updateRemote(opts) {
|
33 |
|
34 | childProcess.execSync("git", ["remote", "update"], opts);
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function countLeftRight(symmetricDifference, opts) {
|
42 | const stdout = childProcess.execSync(
|
43 | "git",
|
44 | ["rev-list", "--left-right", "--count", symmetricDifference],
|
45 | opts
|
46 | );
|
47 |
|
48 | return stdout.split("\t").map((val) => parseInt(val, 10));
|
49 | }
|