Commit 8c50707dd4c9c0ca21f373ebd9fea30eb1b606b4

Authored by Dmitriy Zaporozhets
2 parents 1fdeb9e0 ad33c398

Merge pull request #2823 from hiroponz/switchable-branch-on-network-graph

Switchable the main branch on network graph
app/controllers/graph_controller.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class GraphController < ProjectResourceController
  2 + include ExtractsPath
  3 +
  4 + # Authorize
  5 + before_filter :authorize_read_project!
  6 + before_filter :authorize_code_access!
  7 + before_filter :require_non_empty_project
  8 +
  9 + def show
  10 + respond_to do |format|
  11 + format.html
  12 + format.json do
  13 + graph = Gitlab::Graph::JsonBuilder.new(project, @ref)
  14 + render :json => graph.to_json
  15 + end
  16 + end
  17 + end
  18 +end
... ...
app/controllers/projects_controller.rb
... ... @@ -90,16 +90,6 @@ class ProjectsController &lt; ProjectResourceController
90 90 end
91 91 end
92 92  
93   - def graph
94   - respond_to do |format|
95   - format.html
96   - format.json do
97   - graph = Gitlab::Graph::JsonBuilder.new(project)
98   - render :json => graph.to_json
99   - end
100   - end
101   - end
102   -
103 93 def destroy
104 94 return access_denied! unless can?(current_user, :remove_project, project)
105 95  
... ...
app/controllers/refs_controller.rb
... ... @@ -13,6 +13,8 @@ class RefsController &lt; ProjectResourceController
13 13 format.html do
14 14 new_path = if params[:destination] == "tree"
15 15 project_tree_path(@project, (@ref + "/" + params[:path]))
  16 + elsif params[:destination] == "graph"
  17 + project_graph_path(@project, @ref)
16 18 else
17 19 project_commits_path(@project, @ref)
18 20 end
... ...
app/views/graph/show.html.haml 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 +%h3.page_title Project Network Graph
  2 +%br
  3 += render partial: 'shared/ref_switcher', locals: {destination: 'graph', path: @path}
  4 +%br
  5 +.graph_holder
  6 + %h4
  7 + %small You can move around the graph by using the arrow keys.
  8 + #holder.graph
  9 + .loading.loading-gray
  10 +
  11 +:javascript
  12 + var branch_graph;
  13 + $(function(){
  14 + branch_graph = new BranchGraph($("#holder"), {
  15 + url: '#{project_graph_path(@project, @ref, format: :json)}',
  16 + commit_url: '#{project_commit_path(@project, 'ae45ca32').gsub("ae45ca32", "%s")}',
  17 + ref: '#{@ref}'
  18 + });
  19 + });
... ...
app/views/layouts/project_resource.html.haml
... ... @@ -20,8 +20,8 @@
20 20 = link_to 'Files', project_tree_path(@project, @ref || @repository.root_ref)
21 21 = nav_link(controller: %w(commit commits compare repositories protected_branches)) do
22 22 = link_to "Commits", project_commits_path(@project, @ref || @repository.root_ref)
23   - = nav_link(path: 'projects#graph') do
24   - = link_to "Network", graph_project_path(@project)
  23 + = nav_link(controller: %w(graph)) do
  24 + = link_to "Network", project_graph_path(@project, @ref || @repository.root_ref)
25 25  
26 26 - if @project.issues_enabled
27 27 = nav_link(controller: %w(issues milestones labels)) do
... ...
app/views/projects/graph.html.haml
... ... @@ -1,17 +0,0 @@
1   -%h3.page_title Project Network Graph
2   -%br
3   -
4   -.graph_holder
5   - %h4
6   - %small You can move around the graph by using the arrow keys.
7   - #holder.graph
8   - .loading.loading-gray
9   -
10   -:javascript
11   - var branch_graph;
12   - $(function(){
13   - branch_graph = new BranchGraph($("#holder"), {
14   - url: '#{url_for controller: 'projects', action: 'graph', format: :json}',
15   - commit_url: '#{project_commit_path(@project, 'ae45ca32').gsub("ae45ca32", "%s")}'
16   - });
17   - });
config/routes.rb
... ... @@ -163,7 +163,6 @@ Gitlab::Application.routes.draw do
163 163 resources :projects, constraints: { id: /[a-zA-Z.0-9_\-\/]+/ }, except: [:new, :create, :index], path: "/" do
164 164 member do
165 165 get "wall"
166   - get "graph"
167 166 get "files"
168 167 end
169 168  
... ... @@ -173,6 +172,7 @@ Gitlab::Application.routes.draw do
173 172 resources :compare, only: [:index, :create]
174 173 resources :blame, only: [:show], constraints: {id: /.+/}
175 174 resources :blob, only: [:show], constraints: {id: /.+/}
  175 + resources :graph, only: [:show], constraints: {id: /.+/}
176 176 match "/compare/:from...:to" => "compare#show", as: "compare",
177 177 :via => [:get, :post], constraints: {from: /.+/, to: /.+/}
178 178  
... ...
features/steps/project/project_network_graph.rb
... ... @@ -14,6 +14,6 @@ class ProjectNetworkGraph &lt; Spinach::FeatureSteps
14 14 Gitlab::Graph::JsonBuilder.stub(max_count: 10)
15 15  
16 16 project = Project.find_by_name("Shop")
17   - visit graph_project_path(project)
  17 + visit project_graph_path(project, "master")
18 18 end
19 19 end
... ...
features/steps/shared/paths.rb
... ... @@ -141,7 +141,7 @@ module SharedPaths
141 141 # Stub Graph::JsonBuilder max_size to speed up test (10 commits vs. 650)
142 142 Gitlab::Graph::JsonBuilder.stub(max_count: 10)
143 143  
144   - visit graph_project_path(@project)
  144 + visit project_graph_path(@project, root_ref)
145 145 end
146 146  
147 147 Given "I visit my project's issues page" do
... ...
lib/extracts_path.rb
... ... @@ -54,9 +54,10 @@ module ExtractsPath
54 54 input.gsub!(/^#{Gitlab.config.gitlab.relative_url_root}/, "")
55 55 # Remove project, actions and all other staff from path
56 56 input.gsub!(/^\/#{Regexp.escape(@project.path_with_namespace)}/, "")
57   - input.gsub!(/^\/(tree|commits|blame|blob|refs)\//, "") # remove actions
  57 + input.gsub!(/^\/(tree|commits|blame|blob|refs|graph)\//, "") # remove actions
58 58 input.gsub!(/\?.*$/, "") # remove stamps suffix
59 59 input.gsub!(/.atom$/, "") # remove rss feed
  60 + input.gsub!(/.json$/, "") # remove json suffix
60 61 input.gsub!(/\/edit$/, "") # remove edit route part
61 62  
62 63 if input.match(/^([[:alnum:]]{40})(.+)/)
... ...
lib/gitlab/graph/json_builder.rb
... ... @@ -9,8 +9,9 @@ module Gitlab
9 9 @max_count ||= 650
10 10 end
11 11  
12   - def initialize project
  12 + def initialize project, ref
13 13 @project = project
  14 + @ref = ref
14 15 @repo = project.repo
15 16 @ref_cache = {}
16 17  
... ... @@ -66,9 +67,9 @@ module Gitlab
66 67 heads.select!{|h| h.is_a? Grit::Head or h.is_a? Grit::Remote}
67 68 # sort heads so the master is top and current branches are closer
68 69 heads.sort! do |a,b|
69   - if a.name == "master"
  70 + if a.name == @ref
70 71 -1
71   - elsif b.name == "master"
  72 + elsif b.name == @ref
72 73 1
73 74 else
74 75 b.commit.committed_date <=> a.commit.committed_date
... ...
vendor/assets/javascripts/branch-graph.js
... ... @@ -73,7 +73,8 @@
73 73 , cumonth = ""
74 74 , offsetX = 20
75 75 , offsetY = 60
76   - , barWidth = Math.max(graphWidth, this.dayCount * 20 + 320);
  76 + , barWidth = Math.max(graphWidth, this.dayCount * 20 + 320)
  77 + , scrollLeft = cw;
77 78  
78 79 this.raphael = r;
79 80  
... ... @@ -145,12 +146,18 @@
145 146  
146 147 if (this.commits[i].refs) {
147 148 this.appendLabel(x, y, this.commits[i].refs);
  149 +
  150 + // The main branch is displayed in the center.
  151 + re = new RegExp('(^| )' + this.options.ref + '( |$)');
  152 + if (this.commits[i].refs.match(re)) {
  153 + scrollLeft = x - graphWidth / 2;
  154 + }
148 155 }
149 156  
150 157 this.appendAnchor(top, this.commits[i], x, y);
151 158 }
152 159 top.toFront();
153   - this.element.scrollLeft(cw);
  160 + this.element.scrollLeft(scrollLeft);
154 161 this.bindEvents();
155 162 };
156 163  
... ...