aboutsummaryrefslogtreecommitdiffstats
path: root/html/js/git-deps-data.js
blob: 236a35bcb6b1cb25757b7ee2e00efeb64d6ce6ff (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
86
87
88
89
90
91
92
93
94
95
96
97
var $ = require('jquery');

// The list of nodes and links to feed into WebCola.
// These will be dynamically built as we retrieve them via XHR.
var 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.
var 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.
var deps = {};

// Returns 1 iff a node was added, otherwise 0.
function add_node(commit) {
    if (commit.sha1 in node_index) {
        return 0;
    }
    nodes.push(commit);
    node_index[commit.sha1] = nodes.length - 1;
    return 1;
}

// Returns 1 iff a dependency was added, otherwise 0.
function add_dependency(parent_sha1, child_sha1) {
    if (! (parent_sha1 in deps)) {
        deps[parent_sha1] = {};
    }
    if (child_sha1 in deps[parent_sha1]) {
        // We've already got this link, presumably
        // from a previous XHR.
        return 0;
    }

    deps[parent_sha1][child_sha1] = true;

    add_link(parent_sha1, child_sha1);

    return 1;
}

function add_link(parent_sha1, child_sha1) {
    var pi = node_index[parent_sha1];
    var ci = node_index[child_sha1];

    var link = {
        source: pi,
        target: ci,
        value: 1   // no idea what WebCola needs this for
    };

    links.push(link);
}

// Returns true iff new data was added.
function add_data(data) {
    var new_nodes = 0, new_deps = 0;
    $.each(data.commits, function (i, commit) {
        new_nodes += add_node(commit);
    });
    $.each(data.dependencies, function (i, dep) {
        new_deps += add_dependency(dep.parent, dep.child);
    });

    if (new_nodes > 0 || new_deps > 0) {
        return [new_nodes, new_deps, data.root];
    }

    return false;
}

function node(sha1) {
    var i = node_index[sha1];
    if (! 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
};