-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit2json.js
More file actions
72 lines (61 loc) · 2 KB
/
git2json.js
File metadata and controls
72 lines (61 loc) · 2 KB
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
/* Get the git history as json */
let historyText = require('fs')
.readFileSync('./history.txt', 'utf-8')
.replace(/\r/g, `\n`) // get rid of carriage returns
.replace(/\n\n+/g, ' ') // compress multi line items
.replace(/(\w\n)|(\.\n)/g, ' ') // edge case
// because regex are hard, lets just do this last bit teh easy way
// go through and look for lines that have embedded quotes
let historyList = historyText.split(`\n`).map(l => {
const matches = l.match(/\s+"(\w+)": "(.*".*)",/)
if (matches && matches.length) {
const result = `"${matches[1]}": "${matches[2].replace(/"/g, '')}",`
return result
} else {
return l
}
})
// fix the end character
historyList[historyList.length - 1] = '}'
const finalHistoryJsonText = `[${historyList.join(`\n`)}]`
require('fs').writeFileSync(`finaljson.json`, finalHistoryJsonText)
let history = JSON.parse(finalHistoryJsonText)
/* Get the file data */
const commitHash = history.reduce((hash, commit) => {
hash[commit.commit] = commit
return hash
}, {})
const fileDataLines = require('fs')
.readFileSync('asciifiles.txt', 'utf-8')
.split('\n')
let currentCommit = null
for (let i = 0; i < fileDataLines.length; i++) {
let line = fileDataLines[i]
let commitMatches = line.match(/^commit\s(.*)/)
if (commitMatches) {
currentCommit = commitHash[commitMatches[1]]
currentCommit.files = []
} else {
let fileMatches = line.match(/^(A|M|D)\s+(.*)/)
if (fileMatches) {
currentCommit
currentCommit.files.push({
change: fileMatches[1],
filename: fileMatches[2]
})
}
}
}
/* Format for Elasticsearch Bulk API */
const indexName = 'git-history'
const importLines = []
history.forEach(h => {
importLines.push({ index: { _index: indexName, _id: h.commit } })
h.author.date = new Date(h.author.date).toISOString()
h.commiter.date = new Date(h.commiter.date).toISOString()
importLines.push(h)
})
require('fs').writeFileSync(
`importFile.txt`,
importLines.map(i => JSON.stringify(i)).join('\n') + '\n'
)