aboutsummaryrefslogtreecommitdiffstats
path: root/html/js/git-deps-data.coffee
blob: a8b0b516e1f0a76b9cadf623465b8b519fa4fb08 (plain) (blame)
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
$ = require("jquery")

# The list of nodes and links to feed into WebCola.
# These will be dynamically built as we retrieve them via XHR.
nodes = []
links = []

# WebCola requires links to refer to nodes by index within the
# nodes array, so as nodes are dynamically added, we need to
# be able to retrieve their index efficiently in order to add
# links to/from them.  This also allows us to avoid adding the
# same node twice.
node_index = {}

# Constraints will be added to try to keep siblings at the same y
# position.  For this we need to track siblings, which we do by
# mapping each parent to an array of its siblings in this hash.
# It also enables us to deduplicate links across multiple XHRs.
deps = {}

# Returns 1 iff a node was added, otherwise 0.
add_node = (commit) ->
    return 0 if commit.sha1 of node_index
    nodes.push commit
    node_index[commit.sha1] = nodes.length - 1
    return 1

# Returns 1 iff a dependency was added, otherwise 0.
add_dependency = (parent_sha1, child_sha1) ->
    deps[parent_sha1] = {}  unless parent_sha1 of deps
    
    # We've already got this link, presumably
    # from a previous XHR.
    return 0 if child_sha1 of deps[parent_sha1]
    deps[parent_sha1][child_sha1] = true
    add_link parent_sha1, child_sha1
    return 1

add_link = (parent_sha1, child_sha1) ->
    pi = node_index[parent_sha1]
    ci = node_index[child_sha1]
    link =
        source: pi
        target: ci
        value: 1 # no idea what WebCola needs this for

    links.push link
    return

# Returns true iff new data was added.
add_data = (data) ->
    new_nodes = 0
    new_deps = 0
    $.each data.commits, (i, commit) ->
        new_nodes += add_node(commit)

    $.each data.dependencies, (i, dep) ->
        new_deps += add_dependency(dep.parent, dep.child)

    if new_nodes > 0 or new_deps > 0
        return [
            new_nodes
            new_deps
            data.root
        ]

    return false

node = (sha1) ->
    i = node_index[sha1]
    unless i
        console.error "No index for SHA1 '" + sha1 + "'"
        return null
    return nodes[i]

module.exports =
    # Variables
    nodes: nodes
    links: links
    node_index: node_index
    deps: deps
    
    # Functions
    add: add_data
    node: node