This repository has been archived by the owner on Nov 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (120 loc) · 4.45 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
const exec = require('child_process').exec
/* eslint-disable no-template-curly-in-string */
const colorize = color => (firstStr, secondStr = '') =>
`%{$fg[black]$bg[${color}]%}${firstStr}%{\${reset_color}%}%{$fg[black]$bg[white]%}${secondStr}%{\${reset_color}%}`
/* eslint-enable no-template-curly-in-string */
const colorizeInfo = colorize('blue')
const colorizeWarn = colorize('red')
const customConf = process.env.AVETISK_GIT_PROMPT
const defaultConf = {
order: ['U', 'C', 'D', 'R', '?', 'A', 'M', 'branch'],
errors: {
/* eslint-disable no-template-curly-in-string */
notGitRepo: '%{$fg[black]$bg[white]%} ø git %{${reset_color}%}',
/* eslint-enable no-template-curly-in-string */
},
labels: {
'?': ({ staged }) => colorizeWarn(' \\? ', ` ${staged} `),
A: ({ staged, unstaged }) =>
colorizeInfo(
' A ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
C: ({ staged, unstaged }) =>
colorizeWarn(
' C ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
D: ({ staged, unstaged }) =>
colorizeInfo(
' D ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
M: ({ staged, unstaged }) =>
colorizeInfo(
' M ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
R: ({ staged, unstaged }) =>
colorizeInfo(
' R ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
U: ({ staged, unstaged }) =>
colorizeWarn(
' U ', ` ${staged ? `+${staged}` : ''}${staged && unstaged ? '/' : ''}${unstaged ? `-${unstaged}` : ''} `
),
separator: ' ',
branch: ({ branch, local, remote }) =>
colorize('green')(
` ${branch} `,
local || remote
? ` ${local ? `+${local}` : ''}${local && remote ? '/' : ''}${remote ? `-${remote}` : ''} `
: ''
),
},
}
const config = Object.assign(
{},
defaultConf,
// eslint-disable-next-line import/no-dynamic-require
customConf ? require(customConf) : {}
)
const print = status => process.stdout.write(
config
.order
.filter(label => Object.values(status[label]).some(value => value))
.map(label => config.labels[label](status[label]))
.join(config.labels.separator)
.trim()
)
new Promise((resolve, reject) =>
exec('git rev-parse --is-inside-work-tree', (err, stdout) => (err || stdout.trim() !== 'true'
? reject(new Error('not_git_repo'))
: resolve())
)
)
.then(() => new Promise(resolve => exec('git rev-parse --abbrev-ref HEAD', (err, stdout) =>
resolve(err ? '\\?\\?\\?' : stdout.trim())
)))
.then(branch => Promise.all([
new Promise((resolve, reject) =>
exec(`git log $(git remote)/${branch}..HEAD --pretty=oneline | wc -l`, (err, stdout) =>
(err ? reject() : resolve(Number.parseInt(stdout, 10)))
)
),
new Promise((resolve, reject) =>
exec(`git log HEAD..$(git remote)/${branch} --pretty=oneline | wc -l`, (err, stdout) =>
(err ? reject() : resolve(Number.parseInt(stdout, 10)))
)
),
new Promise((resolve, reject) =>
exec('git status --porcelain', (err, stdout) => (
err
? reject()
: resolve(
stdout
.split('\n')
.reduce((status, [first, last]) => {
const result = Object.assign({}, status)
if (result[first]) result[first].staged += 1
if (result[last]) result[last].unstaged += 1
return result
}, {
U: { staged: 0, unstaged: 0 },
C: { staged: 0, unstaged: 0 },
R: { staged: 0, unstaged: 0 },
D: { staged: 0, unstaged: 0 },
A: { staged: 0, unstaged: 0 },
M: { staged: 0, unstaged: 0 },
'?': { staged: 0, unstaged: 0 },
branch: { branch },
})
))
)
),
]))
.then(([local, remote, status]) =>
Object.assign({}, status, { branch: { local, remote, branch: status.branch.branch } }))
.then(print)
.catch(err => process.stdout.write(
`${err}` === 'Error: not_git_repo'
? config.errors.notGitRepo
: 'ERR: no git status'
))
process.stdout.on('error', ({ code }) => code === 'EPIPE' && process.exit(0))