UNPKG

1.21 kBJavaScriptView Raw
1"use strict";
2
3const log = require("npmlog");
4const childProcess = require("@lerna/child-process");
5
6module.exports.isBehindUpstream = isBehindUpstream;
7
8/**
9 * @param {string} gitRemote
10 * @param {string} branch
11 * @param {import("@lerna/child-process").ExecOpts} opts
12 */
13function 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 * @param {import("@lerna/command").ExecOpts} opts
31 */
32function updateRemote(opts) {
33 // git fetch, but for everything
34 childProcess.execSync("git", ["remote", "update"], opts);
35}
36
37/**
38 * @param {string} symmetricDifference
39 * @param {import("@lerna/command").ExecOpts} opts
40 */
41function 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}