Skip to content

Commit 6b66a76

Browse files
committed
Graph: build json
1 parent ef08872 commit 6b66a76

File tree

9 files changed

+308
-1
lines changed

9 files changed

+308
-1
lines changed

.rbenv-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.9.2-p290

app/assets/javascripts/application.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//= require jquery.ui.selectmenu
1111
//= require jquery.tagify
1212
//= require jquery.cookie
13+
//= require raphael
14+
//= require branch-graph
1315
//= require_tree .
1416

1517
$(function(){
@@ -35,4 +37,4 @@ function showMenu() {
3537

3638
function resetMenu() {
3739
$(this).removeClass("hover");
38-
}
40+
}

app/controllers/projects_controller.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require File.join(Rails.root, 'lib', 'graph_commit')
2+
13
class ProjectsController < ApplicationController
24
before_filter :project, :except => [:index, :new, :create]
35
layout :determine_layout
@@ -125,6 +127,36 @@ def tree
125127
return render_404
126128
end
127129

130+
def graph
131+
@repo = project.repo
132+
commits = Grit::Commit.find_all(@repo, nil, {:max_count => 650})
133+
ref_cache = {}
134+
commits.collect! do |commit|
135+
add_refs(commit, ref_cache)
136+
GraphCommit.new(commit)
137+
end
138+
139+
days = GraphCommit.index_commits(commits)
140+
@days_json = days.compact.collect{|d| [d.day, d.strftime("%b")] }.to_json
141+
@commits_json = commits.collect do |c|
142+
h = {}
143+
h[:parents] = c.parents.collect do |p|
144+
[p.id,0,0]
145+
end
146+
h[:author] = c.author.name.force_encoding("UTF-8")
147+
h[:time] = c.time
148+
h[:space] = c.space
149+
h[:refs] = c.refs.collect{|r|r.name}.join(" ") unless c.refs.nil?
150+
h[:id] = c.sha
151+
h[:date] = c.date
152+
h[:message] = c.message.force_encoding("UTF-8")
153+
h[:email] = c.author.email
154+
h
155+
end.to_json
156+
157+
render :text => @commits_json
158+
end
159+
128160
def blob
129161
@repo = project.repo
130162
@commit = project.commit(params[:commit_id])
@@ -149,6 +181,14 @@ def destroy
149181

150182
protected
151183

184+
def add_refs(commit, ref_cache)
185+
if ref_cache.empty?
186+
@repo.refs.each {|ref| ref_cache[ref.commit.id] ||= [];ref_cache[ref.commit.id] << ref}
187+
end
188+
commit.refs = ref_cache[commit.id] if ref_cache.include? commit.id
189+
commit.refs ||= []
190+
end
191+
152192
def project
153193
@project ||= Project.find_by_code(params[:id])
154194
end

app/views/layouts/project.html.haml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
= link_to "History", project_path(@project), :class => current_page?(:controller => "projects", :action => "show", :id => @project) ? "current" : nil
2323
= link_to "Tree", tree_project_path(@project), :class => current_page?(:controller => "projects", :action => "tree", :id => @project) ? "current" : nil
2424
= link_to "Commits", project_commits_path(@project), :class => current_page?(:controller => "commits", :action => "index", :project_id => @project) ? "current" : nil
25+
= link_to "Network graph", graph_project_path(@project), :class => current_page?(:controller => "projects", :action => "graph", :project_id => @project) ? "current" : nil
2526
= link_to team_project_path(@project), :class => (current_page?(:controller => "projects", :action => "team", :id => @project) || controller.controller_name == "team_members") ? "current" : nil do
2627
Team
2728
- if @project.users_projects.count > 0

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
get "blob"
3535
get "team"
3636
get "wall"
37+
get "graph"
3738

3839
# tree viewer
3940
get "tree/:commit_id" => "projects#tree"
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
var commits = chunk1.commits,
2+
comms = {},
3+
pixelsX = [],
4+
pixelsY = [],
5+
mmax = Math.max,
6+
mtime = 0,
7+
mspace = 0,
8+
parents = {};
9+
for (var i = 0, ii = commits.length; i < ii; i++) {
10+
for (var j = 0, jj = commits[i].parents.length; j < jj; j++) {
11+
parents[commits[i].parents[j][0]] = true;
12+
}
13+
mtime = Math.max(mtime, commits[i].time);
14+
mspace = Math.max(mspace, commits[i].space);
15+
}
16+
mtime = mtime + 4;
17+
mspace = mspace + 10;
18+
for (i = 0; i < ii; i++) {
19+
if (commits[i].id in parents) {
20+
commits[i].isParent = true;
21+
}
22+
comms[commits[i].id] = commits[i];
23+
}
24+
var colors = ["#000"];
25+
for (var k = 0; k < mspace; k++) {
26+
colors.push(Raphael.getColor());
27+
}
28+
function branchGraph(holder) {
29+
var ch = mspace * 20 + 20, cw = mtime * 20 + 20,
30+
r = Raphael("holder", cw, ch),
31+
top = r.set();
32+
var cuday = 0, cumonth = "";
33+
r.rect(0,0,days.length*20+20,40).attr({fill: "#999"});
34+
35+
for (mm = 0; mm < days.length; mm++) {
36+
if(days[mm] != null){
37+
if(cuday != days[mm][0]){
38+
r.text(10+mm*20,30,days[mm][0]).attr({font: "12px Fontin-Sans, Arial", fill: "#444"});
39+
cuday = days[mm][0]
40+
}
41+
if(cumonth != days[mm][1]){
42+
r.text(10+mm*20,10,days[mm][1]).attr({font: "12px Fontin-Sans, Arial", fill: "#444"});
43+
cumonth = days[mm][1]
44+
}
45+
46+
}
47+
}
48+
for (i = 0; i < ii; i++) {
49+
var x = 10 + 20 * commits[i].time,
50+
y = 70 + 20 * commits[i].space;
51+
r.circle(x, y, 3).attr({fill: colors[commits[i].space], stroke: "none"});
52+
if (commits[i].refs != null && commits[i].refs != "") {
53+
var longrefs = commits[i].refs
54+
var shortrefs = commits[i].refs;
55+
if (shortrefs.length > 15){
56+
shortrefs = shortrefs.substr(0,13) + "...";
57+
}
58+
var t = r.text(x+5,y+5,shortrefs).attr({font: "12px Fontin-Sans, Arial", fill: "#666",
59+
title: longrefs, cursor: "pointer", rotation: "90"});
60+
61+
var textbox = t.getBBox();
62+
t.translate(textbox.height/-4,textbox.width/2);
63+
}
64+
for (var j = 0, jj = commits[i].parents.length; j < jj; j++) {
65+
var c = comms[commits[i].parents[j][0]];
66+
if (c) {
67+
var cx = 10 + 20 * c.time,
68+
cy = 70 + 20 * c.space;
69+
if (c.space == commits[i].space) {
70+
r.path("M" + (x - 5) + "," + (y + .0001) + "L" + (15 + 20 * c.time) + "," + (y + .0001))
71+
.attr({stroke: colors[c.space], "stroke-width": 2});
72+
73+
} else if (c.space < commits[i].space) {
74+
r.path(["M", x - 5, y + .0001, "l-5-2,0,4,5,-2C",x-5,y,x -17, y+2, x -20, y-10,"L", cx,y-10,cx , cy])
75+
.attr({stroke: colors[commits[i].space], "stroke-width": 2});
76+
} else {
77+
r.path(["M", x-5, y, "l-5-2,0,4,5,-2C",x-5,y,x -17, y-2, x -20, y+10,"L", cx,y+10,cx , cy])
78+
.attr({stroke: colors[commits[i].space], "stroke-width": 2});
79+
}
80+
}
81+
}
82+
(function (c, x, y) {
83+
top.push(r.circle(x, y, 10).attr({fill: "#000", opacity: 0, cursor: "pointer"})
84+
.hover(function () {
85+
var s = r.text(100, 100,c.author + "\n \n" +c.id + "\n \n" + c.message).attr({fill: "#fff"});
86+
this.popup = r.popupit(x, y + 5, s, 0);
87+
top.push(this.popup.insertBefore(this));
88+
}, function () {
89+
this.popup && this.popup.remove() && delete this.popup;
90+
}));
91+
}(commits[i], x, y));
92+
}
93+
top.toFront();
94+
var hw = holder.offsetWidth,
95+
hh = holder.offsetHeight,
96+
v = r.rect(hw - 8, 0, 4, Math.pow(hh, 2) / ch, 2).attr({fill: "#000", opacity: 0}),
97+
h = r.rect(0, hh - 8, Math.pow(hw, 2) / cw, 4, 2).attr({fill: "#000", opacity: 0}),
98+
bars = r.set(v, h),
99+
drag,
100+
dragger = function (e) {
101+
if (drag) {
102+
e = e || window.event;
103+
holder.scrollLeft = drag.sl - (e.clientX - drag.x);
104+
holder.scrollTop = drag.st - (e.clientY - drag.y);
105+
}
106+
};
107+
holder.onmousedown = function (e) {
108+
e = e || window.event;
109+
drag = {x: e.clientX, y: e.clientY, st: holder.scrollTop, sl: holder.scrollLeft};
110+
document.onmousemove = dragger;
111+
bars.animate({opacity: .5}, 300);
112+
};
113+
document.onmouseup = function () {
114+
drag = false;
115+
document.onmousemove = null;
116+
bars.animate({opacity: 0}, 300);
117+
};
118+
holder.scrollLeft = cw;
119+
};
120+
Raphael.fn.popupit = function (x, y, set, dir, size) {
121+
dir = dir == null ? 2 : dir;
122+
size = size || 5;
123+
x = Math.round(x);
124+
y = Math.round(y);
125+
var bb = set.getBBox(),
126+
w = Math.round(bb.width / 2),
127+
h = Math.round(bb.height / 2),
128+
dx = [0, w + size * 2, 0, -w - size * 2],
129+
dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
130+
p = ["M", x - dx[dir], y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
131+
"l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
132+
"l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
133+
"l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
134+
"l", -mmax(w - size, 0), 0, "z"].join(","),
135+
xy = [{x: x, y: y + size * 2 + h}, {x: x - size * 2 - w, y: y}, {x: x, y: y - size * 2 - h}, {x: x + size * 2 + w, y: y}][dir];
136+
set.translate(xy.x - w - bb.x, xy.y - h - bb.y);
137+
return this.set(this.path(p).attr({fill: "#234", stroke: "none"}).insertBefore(set.node ? set : set[0]), set);
138+
};
139+
Raphael.fn.popup = function (x, y, text, dir, size) {
140+
dir = dir == null ? 2 : dir > 3 ? 3 : dir;
141+
size = size || 5;
142+
text = text || "$9.99";
143+
var res = this.set(),
144+
d = 3;
145+
res.push(this.path().attr({fill: "#000", stroke: "#000"}));
146+
res.push(this.text(x, y, text).attr(this.g.txtattr).attr({fill: "#fff", "font-family": "Helvetica, Arial"}));
147+
res.update = function (X, Y, withAnimation) {
148+
X = X || x;
149+
Y = Y || y;
150+
var bb = this[1].getBBox(),
151+
w = bb.width / 2,
152+
h = bb.height / 2,
153+
dx = [0, w + size * 2, 0, -w - size * 2],
154+
dy = [-h * 2 - size * 3, -h - size, 0, -h - size],
155+
p = ["M", X - dx[dir], Y - dy[dir], "l", -size, (dir == 2) * -size, -mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, -size, -size,
156+
"l", 0, -mmax(h - size, 0), (dir == 3) * -size, -size, (dir == 3) * size, -size, 0, -mmax(h - size, 0), "a", size, size, 0, 0, 1, size, -size,
157+
"l", mmax(w - size, 0), 0, size, !dir * -size, size, !dir * size, mmax(w - size, 0), 0, "a", size, size, 0, 0, 1, size, size,
158+
"l", 0, mmax(h - size, 0), (dir == 1) * size, size, (dir == 1) * -size, size, 0, mmax(h - size, 0), "a", size, size, 0, 0, 1, -size, size,
159+
"l", -mmax(w - size, 0), 0, "z"].join(","),
160+
xy = [{x: X, y: Y + size * 2 + h}, {x: X - size * 2 - w, y: Y}, {x: X, y: Y - size * 2 - h}, {x: X + size * 2 + w, y: Y}][dir];
161+
xy.path = p;
162+
if (withAnimation) {
163+
this.animate(xy, 500, ">");
164+
} else {
165+
this.attr(xy);
166+
}
167+
return this;
168+
};
169+
return res.update(x, y);
170+
};

lib/commit_ext.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
module CommitExt
2+
3+
attr_accessor :refs
4+
25
def safe_message
36
message.encode("UTF-8",
47
:invalid => :replace,

lib/graph_commit.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require "grit"
2+
3+
class GraphCommit
4+
attr_accessor :time, :space
5+
def initialize(commit)
6+
@_commit = commit
7+
@time = -1
8+
@space = 0
9+
end
10+
11+
def method_missing(m, *args, &block)
12+
@_commit.send(m, *args, &block)
13+
end
14+
15+
# Method is adding time and space on the
16+
# list of commits. As well as returns date list
17+
# corelated with time set on commits.
18+
#
19+
# @param [Array<GraphCommit>] comits to index
20+
#
21+
# @return [Array<TimeDate>] list of commit dates corelated with time on commits
22+
def self.index_commits(commits)
23+
days, heads = [], []
24+
map = {}
25+
26+
commits.reverse.each_with_index do |c,i|
27+
c.time = i
28+
days[i]=c.committed_date
29+
map[c.id] = c
30+
heads += c.refs unless c.refs.nil?
31+
end
32+
33+
heads.select!{|h| h.is_a? Grit::Head or h.is_a? Grit::Remote}
34+
# sort heads so the master is top and current branches are closer
35+
heads.sort! do |a,b|
36+
if a.name == "master"
37+
-1
38+
elsif b.name == "master"
39+
1
40+
else
41+
b.commit.committed_date <=> a.commit.committed_date
42+
end
43+
end
44+
45+
j = 0
46+
heads.each do |h|
47+
if map.include? h.commit.id then
48+
j = mark_chain(j+=1, map[h.commit.id], map)
49+
end
50+
end
51+
days
52+
end
53+
54+
# Add space mark on commit and its parents
55+
#
56+
# @param [Fixnum] space (row on the graph) to be set
57+
# @param [GraphCommit] the commit object.
58+
# @param [Hash<String,GraphCommit>] map of commits
59+
#
60+
# @return [Fixnum] max space used.
61+
def self.mark_chain(mark, commit, map)
62+
commit.space = mark if commit.space == 0
63+
m1 = mark - 1
64+
marks = commit.parents.collect do |p|
65+
if map.include? p.id and map[p.id].space == 0 then
66+
mark_chain(m1+=1, map[p.id],map)
67+
else
68+
m1 + 1
69+
end
70+
end
71+
marks << mark
72+
marks.compact.max
73+
end
74+
75+
def self.add_refs(commit, ref_cache)
76+
if ref_cache.empty?
77+
@repo.refs.each {|ref| ref_cache[ref.commit.id] ||= [];ref_cache[ref.commit.id] << ref}
78+
end
79+
commit.refs = ref_cache[commit.id] if ref_cache.include? commit.id
80+
commit.refs ||= []
81+
end
82+
end

vendor/assets/javascripts/raphael.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)