-
Notifications
You must be signed in to change notification settings - Fork 0
/
methods.js
93 lines (74 loc) · 2.21 KB
/
methods.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
'use strict';
const async = require('async');
module.exports = function (exec, options) {
const pwd = cb => exec('pwd', cb);
const status = cb => exec('git status --porcelain', cb);
const currentBranch = cb => exec('git rev-parse --abbrev-ref HEAD', cb);
const currentUpstream = cb => exec('git rev-parse --abbrev-ref --symbolic-full-name @{u}', cb);
const fetchOrigin = (res, cb) => {
if (options.fetch) {
exec(`git fetch origin ${res.upBranch}`, cb);
} else {
cb(null, null);
}
};
const lastCommitHash = cb => exec(`git log --pretty=format:"%h" --decorate -n 1`, cb);
const lastCommitDate = cb => exec(`git log --pretty=format:"%ad" --decorate --date=relative -n 1`, cb);
const lastCommitAuthor = cb => exec(`git log --pretty=format:"%cn" --decorate -n 1`, cb);
const lastCommitMessage = cb => exec(`git log --pretty=format:"%s" --decorate -n 1`, cb);
const getUpBranch = (res, cb) => {
const upBranch = res.currentUpstream ? res.currentUpstream : 'origin/' + res.currentBranch;
cb(null, upBranch);
};
const branchBehind = cb => {
async.auto({
currentBranch,
currentUpstream,
upBranch: ['currentBranch', 'currentUpstream', getUpBranch],
fetchOrigin: ['upBranch', fetchOrigin]
}, (err, res) => {
if (err) {
console.log(err);
}
exec(`git log ${res.currentBranch}..${res.upBranch} --pretty=oneline | wc -l`, cb);
});
};
const branchAhead = cb => {
async.auto({
currentBranch,
currentUpstream,
upBranch: ['currentBranch', 'currentUpstream', getUpBranch],
fetchOrigin: ['upBranch', fetchOrigin]
}, (err, res) => {
if (err) {
console.log(err);
}
exec(`git log ${res.upBranch}..${res.currentBranch} --pretty=oneline | wc -l`, cb);
});
};
const customBranchBehind = (branch) => cb => {
async.auto({
currentBranch,
upBranch: async.constant(branch),
fetchOrigin: ['upBranch', fetchOrigin]
}, (err, res) => {
if (err) {
console.log(err);
}
exec(`git log ${res.currentBranch}..origin/${branch} --pretty=oneline | wc -l`, cb);
});
};
return {
pwd,
status,
currentBranch,
currentUpstream,
lastCommitHash,
lastCommitDate,
lastCommitAuthor,
lastCommitMessage,
customBranchBehind,
branchAhead,
branchBehind
};
};