-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
51 lines (38 loc) · 1.35 KB
/
index.ts
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
import currentBranch from 'current-git-branch';
import execa from 'execa';
import commitCount from 'git-commit-count';
import path from 'path';
import isAbsolute from 'is-absolute';
export interface Options {
remote?: string;
branch?: string;
}
const gitNeedsPull = (cwd = process.cwd(), { remote, branch }: Options = {}): boolean => {
const thisPath = isAbsolute(cwd) ? cwd : path.join(cwd, cwd);
const r = remote || 'origin';
if (commitCount(thisPath) <= 0) {
return false;
}
let b = branch || currentBranch(thisPath);
b = b && b.slice(0, 14) !== '(HEAD detached' ? b : 'master';
try {
const updateExec = 'git fetch';
const localCommitExec = 'git rev-parse HEAD';
const remoteCommitExec = `git rev-parse ${r}/${b}`;
const lastCommonCommitExec = `git merge-base HEAD ${r}/${b}`;
execa.commandSync(updateExec, { cwd: thisPath });
const localCommit = execa.commandSync(localCommitExec, { cwd: thisPath }).stdout;
const remoteCommit = execa.commandSync(remoteCommitExec, { cwd: thisPath }).stdout;
if (localCommit === remoteCommit) {
return false;
}
const lastCommonCommit = execa.commandSync(lastCommonCommitExec, { cwd: thisPath }).stdout;
if (lastCommonCommit === localCommit) {
return true;
}
return false;
} catch (e) {
return false;
}
};
export default gitNeedsPull;