From 2f22d2c01fb77ede823d0e1c7105d25fc0a2d784 Mon Sep 17 00:00:00 2001 From: Adam Spiers Date: Tue, 6 Jan 2015 13:28:48 +0000 Subject: tweak JSONDependencyListener to produce data WebCola can use --- git-deps | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/git-deps b/git-deps index 567eaae..2977800 100755 --- a/git-deps +++ b/git-deps @@ -126,34 +126,54 @@ class CLIDependencyListener(DependencyListener): class JSONDependencyListener(DependencyListener): - """Dependency listener for use when building results in JSON.""" + """Dependency listener for use when compiling graph data in a JSON + format which can be consumed by WebCola / d3. Each new node has + to be added to a 'nodes' array, and its index in that array needs + to be tracked so that we can refer to the node by index when + specifying links between nodes. + """ def __init__(self, options): super(JSONDependencyListener, self).__init__(options) - self._nodes = {} + self._nodes = {} # maps node names to indices in the nodes array self._json = { - 'links' : [], + 'nodes': [], + 'links': [], } + def add_node(self, name): + """Adds the node to the nodes array if it doesn't already exist, and + returns the node's index in the array.""" + if name in self._nodes: + return self._nodes[name] + self._json['nodes'].append({'name': name}) + self._nodes[name] = len(self._json['nodes']) - 1 + return self._nodes[name] + + def add_link(self, source, target): + self._json['links'].append + def new_dependency(self, dependent, dependency, path, line_num): - dependent_sha = dependent.hex - dependency_sha = dependency.hex + source_index = self.add_node(dependent.hex) + target_index = self.add_node(dependency.hex) new_link = { - 'dependent' : dependent_sha, - 'dependency' : dependency_sha, + 'source': source_index, + 'target': target_index, + 'value': 1, } if self.options.log: pass # FIXME - self._nodes[dependent_sha] = 1 - self._nodes[dependency_sha] = 1 self._json['links'].append(new_link) + def json(self): + return json.dumps(self._json, sort_keys=True, indent=4) + def all_done(self): - self._json['nodes'] = self._nodes.keys() - print(json.dumps(self._json, sort_keys=True, indent=4)) + print(self.json()) + class DependencyDetector(object): """Class for automatically detecting dependencies between git commits. -- cgit