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
|
class Commits {
constructor(commits_json) {
// turn raw json into proper objects, retain (new-to-old)
// order
let i = 0
this.commits = commits_json.map((json) => {
return new Commit(json, i++)
})
// build the hash map
this.hash_to_commit = []
for (let commit of this.commits) {
this.hash_to_commit[commit.hash] = commit
}
// fill in parents
for (let commit_json of commits_json) {
let commit = this.hash_to_commit[commit_json.hash]
for (let parent_hash of commit_json.parents) {
let parent = this.hash_to_commit[parent_hash]
if (parent) {
commit.parents.push(parent)
} else {
console.log("commit",commit, "has no parent", parent_hash)
}
}
}
// fill in children
//
// walk first parent before walking second parent so that
// first child back link is to first parent
this._fill_children(this.commits[0])
/* save oldest and and newest based on commit date */
this.oldest = this.commits.reduce((oldest, commit) => {
return oldest.commit_date < commit.commit_date ? oldest : commit
}, this.commits[0])
this.newest = this.commits.reduce((newest, commit) => {
return newest.commit_date > commit.commit_date ? newest : commit
}, this.commits[0])
console.log("commits", i, this)
}
_fill_children(child) {
// assume child has at least one parent
let branches = []
branches.push([child, 0])
while (branches.length > 0) {
let [child, level] = branches.pop()
do {
if (child.parents.length > level+1) {
branches.push([child, level+1])
}
let parent = child.parents[level]
if (parent.children.includes(child)) {
break
}
parent.children.push(child)
child = parent
level = 0
} while (child.parents.length > 0)
}
return
}
}
|