-
Notifications
You must be signed in to change notification settings - Fork 17
/
webpack.gitinfo.js
68 lines (58 loc) · 2.15 KB
/
webpack.gitinfo.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
// grab some info from git on the state of the repo
// https://isomorphic-git.org/docs/en/fs
const git = require('isomorphic-git')
const fs = require('fs')
async function tagsToCommits ({ dir }) {
const map = {}
const tags = await git.listTags({ fs, dir })
for (const tag of tags) {
const ref = await git.resolveRef({ fs, dir, ref: tag })
map[tag] = ref
}
return map
}
async function tagsPointAt ({ dir, oid }) {
const tagMap = await tagsToCommits({ dir })
const pointAt =
Object.entries(tagMap)
.filter(([tag, commit]) => commit === oid)
.map(([tag]) => tag)
return pointAt
}
async function fetchGitInfo ({ ref, dir = '.' } = {}) {
try {
const [head] = await git.log({ fs, dir, depth: 1, ref })
const tagsPointAtHead = await tagsPointAt({ dir, oid: head.oid })
// https://isomorphic-git.org/docs/en/statusMatrix
const fileStatus = (await git.statusMatrix({ fs, dir })).reduce((fs, file) => {
fs.push({
file: file[0],
untracked: file[1] === 0 && file[2] === 2 && file[3] === 0,
unmodified: file[1] === 1 && file[2] === 1 && file[3] === 1,
staged: file[3] === 2 || file[3] === 3 || (file[1] === 1 && file[2] === 0 && file[3] === 0),
modified: file[1] === 1 && file[2] === 2,
deleted: file[1] === 1 && file[2] === 0,
added: file[1] === 0 && file[2] === 2 && file[3] !== 0,
})
return fs
}, [])
const gitInfo = {
headOid: head.oid.substr(0, 8),
hasChanges: fileStatus.length !== fileStatus.filter(status => status.unmodified).length,
isHeadOidTagged: tagsPointAtHead.length > 0,
headTags: tagsPointAtHead,
untracked: fileStatus.filter(status => status.untracked).length,
staged: fileStatus.filter(status => status.staged).length,
added: fileStatus.filter(status => status.added).length,
modified: fileStatus.filter(status => status.modified).length,
deleted: fileStatus.filter(status => status.deleted).length,
}
return gitInfo
} catch (e) {
if (e.name === 'ResolveRefError') {
console.info('building from non-git repo')
}
return false
}
}
module.exports = fetchGitInfo