mermaid/src/diagrams/gitGraph/gitGraphAst.js

210 lines
5.5 KiB
JavaScript
Raw Normal View History

2017-04-11 22:14:25 +08:00
var Logger = require('../../logger')
var log = Logger.Log
var _ = require('lodash')
2016-03-28 13:17:31 +05:30
2017-04-11 22:14:25 +08:00
var commits = {}
var head = null
var branches = { 'master': head }
var curBranch = 'master'
var direction = 'LR'
var seq = 0
2017-04-11 22:14:25 +08:00
function getRandomInt (min, max) {
return Math.floor(Math.random() * (max - min)) + min
2016-03-29 20:55:22 +05:30
}
2016-04-28 11:27:24 +05:30
2017-04-11 22:14:25 +08:00
function getId () {
var pool = '0123456789abcdef'
var id = ''
for (var i = 0; i < 7; i++) {
id += pool[getRandomInt(0, 16)]
}
return id
}
2017-04-11 22:14:25 +08:00
function isfastforwardable (currentCommit, otherCommit) {
log.debug('Entering isfastforwardable:', currentCommit.id, otherCommit.id)
2017-04-13 20:16:38 +08:00
while (currentCommit.seq <= otherCommit.seq && currentCommit !== otherCommit) {
2017-04-16 23:48:36 +08:00
// only if other branch has more commits
2017-04-11 22:14:25 +08:00
if (otherCommit.parent == null) break
if (Array.isArray(otherCommit.parent)) {
log.debug('In merge commit:', otherCommit.parent)
return isfastforwardable(currentCommit, commits[otherCommit.parent[0]]) ||
2017-04-16 23:48:36 +08:00
isfastforwardable(currentCommit, commits[otherCommit.parent[1]])
2016-03-27 17:53:01 +05:30
} else {
2017-04-11 22:14:25 +08:00
otherCommit = commits[otherCommit.parent]
2016-03-27 16:13:07 +05:30
}
2017-04-11 22:14:25 +08:00
}
log.debug(currentCommit.id, otherCommit.id)
2017-04-13 20:16:38 +08:00
return currentCommit.id === otherCommit.id
2017-04-11 22:14:25 +08:00
}
function isReachableFrom (currentCommit, otherCommit) {
var currentSeq = currentCommit.seq
var otherSeq = otherCommit.seq
if (currentSeq > otherSeq) return isfastforwardable(otherCommit, currentCommit)
return false
}
exports.setDirection = function (dir) {
direction = dir
}
var options = {}
exports.setOptions = function (rawOptString) {
log.debug('options str', rawOptString)
rawOptString = rawOptString && rawOptString.trim()
rawOptString = rawOptString || '{}'
try {
options = JSON.parse(rawOptString)
} catch (e) {
log.error('error while parsing gitGraph options', e.message)
}
}
exports.getOptions = function () {
return options
}
exports.commit = function (msg) {
2017-04-16 23:48:36 +08:00
var commit = {
id: getId(),
2017-04-11 22:14:25 +08:00
message: msg,
seq: seq++,
2017-04-16 23:48:36 +08:00
parent: head == null ? null : head.id
}
2017-04-11 22:14:25 +08:00
head = commit
commits[commit.id] = commit
branches[curBranch] = commit.id
log.debug('in pushCommit ' + commit.id)
}
exports.branch = function (name) {
branches[name] = head != null ? head.id : null
log.debug('in createBranch')
}
exports.merge = function (otherBranch) {
var currentCommit = commits[branches[curBranch]]
var otherCommit = commits[branches[otherBranch]]
if (isReachableFrom(currentCommit, otherCommit)) {
log.debug('Already merged')
return
}
if (isfastforwardable(currentCommit, otherCommit)) {
branches[curBranch] = branches[otherBranch]
head = commits[branches[curBranch]]
} else {
2017-04-16 23:48:36 +08:00
// create merge commit
2017-04-11 22:14:25 +08:00
var commit = {
id: getId(),
message: 'merged branch ' + otherBranch + ' into ' + curBranch,
seq: seq++,
parent: [head == null ? null : head.id, branches[otherBranch]]
2016-05-07 10:52:24 +05:30
}
2017-04-11 22:14:25 +08:00
head = commit
commits[commit.id] = commit
branches[curBranch] = commit.id
}
log.debug(branches)
log.debug('in mergeBranch')
}
exports.checkout = function (branch) {
log.debug('in checkout')
curBranch = branch
var id = branches[curBranch]
head = commits[id]
}
exports.reset = function (commitRef) {
log.debug('in reset', commitRef)
var ref = commitRef.split(':')[0]
var parentCount = parseInt(commitRef.split(':')[1])
2017-04-13 20:16:38 +08:00
var commit = ref === 'HEAD' ? head : commits[branches[ref]]
2017-04-11 22:14:25 +08:00
log.debug(commit, parentCount)
while (parentCount > 0) {
commit = commits[commit.parent]
parentCount--
if (!commit) {
var err = 'Critical error - unique parent commit not found during reset'
log.error(err)
throw err
2016-03-28 13:18:11 +05:30
}
2017-04-11 22:14:25 +08:00
}
head = commit
branches[curBranch] = commit.id
}
function upsert (arr, key, newval) {
var match = _.find(arr, key)
if (match) {
var index = _.indexOf(arr, _.find(arr, key))
arr.splice(index, 1, newval)
} else {
arr.push(newval)
}
}
function prettyPrintCommitHistory (commitArr) {
var commit = _.maxBy(commitArr, 'seq')
var line = ''
_.each(commitArr, function (c) {
2017-04-13 20:16:38 +08:00
if (c === commit) {
2017-04-11 22:14:25 +08:00
line += '\t*'
2016-03-28 13:18:11 +05:30
} else {
2017-04-11 22:14:25 +08:00
line += '\t|'
2016-03-28 13:18:11 +05:30
}
2017-04-11 22:14:25 +08:00
})
var label = [line, commit.id, commit.seq]
_.each(branches, function (v, k) {
2017-04-13 20:16:38 +08:00
if (v === commit.id) label.push(k)
2017-04-11 22:14:25 +08:00
})
log.debug(label.join(' '))
if (Array.isArray(commit.parent)) {
var newCommit = commits[commit.parent[0]]
upsert(commitArr, commit, newCommit)
commitArr.push(commits[commit.parent[1]])
} else if (commit.parent == null) {
return
} else {
var nextCommit = commits[commit.parent]
upsert(commitArr, commit, nextCommit)
}
commitArr = _.uniqBy(commitArr, 'id')
prettyPrintCommitHistory(commitArr)
}
exports.prettyPrint = function () {
log.debug(commits)
var node = exports.getCommitsArray()[0]
prettyPrintCommitHistory([node])
2016-03-27 23:02:31 +05:30
}
2016-03-27 11:22:30 +05:30
exports.clear = function () {
2017-04-11 22:14:25 +08:00
commits = {}
head = null
branches = { 'master': head }
curBranch = 'master'
seq = 0
}
exports.getBranchesAsObjArray = function () {
var branchArr = _.map(branches, function (v, k) {
2017-04-16 23:48:36 +08:00
return { 'name': k, 'commit': commits[v] }
2017-04-11 22:14:25 +08:00
})
return branchArr
}
exports.getBranches = function () { return branches }
exports.getCommits = function () { return commits }
exports.getCommitsArray = function () {
var commitArr = Object.keys(commits).map(function (key) {
return commits[key]
})
_.each(commitArr, function (o) { log.debug(o.id) })
return _.orderBy(commitArr, ['seq'], ['desc'])
}
exports.getCurrentBranch = function () { return curBranch }
exports.getDirection = function () { return direction }
exports.getHead = function () { return head }