From 2963e7fd1c64af8482eaa82579ccdef5af1ef55a Mon Sep 17 00:00:00 2001
From: Daniel Gerhardt <code@dgerhardt.net>
Date: Thu, 2 Jul 2015 20:12:24 +0200
Subject: [PATCH] Add ref switching support for graphs

It is now possible to view contributor and commit statistics for
refs beyond the default branch.

Additionally, conditions in RefsController#switch have been refactored
in order that a `case` block is used now.
---
 CHANGELOG                                     |  1 +
 app/controllers/projects/graphs_controller.rb |  7 +++--
 app/controllers/projects/refs_controller.rb   | 26 +++++++++++--------
 app/views/projects/graphs/commits.html.haml   |  4 ++-
 app/views/projects/graphs/show.html.haml      |  6 +++--
 5 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index 2d41a65d2df..bafc7390f4a 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
 
 v 7.13.0 (unreleased)
   - Fix redirection to home page URL for unauthorized users (Daniel Gerhardt)
+  - Add branch switching support for graphs (Daniel Gerhardt)
   - Fix external issue tracker hook/test for HTTPS URLs (Daniel Gerhardt)
   - Remove link leading to a 404 error in Deploy Keys page (Stan Hu)
   - Add support for unlocking users in admin settings (Stan Hu)
diff --git a/app/controllers/projects/graphs_controller.rb b/app/controllers/projects/graphs_controller.rb
index a060ea6f998..0b6f7f5c91e 100644
--- a/app/controllers/projects/graphs_controller.rb
+++ b/app/controllers/projects/graphs_controller.rb
@@ -1,6 +1,9 @@
 class Projects::GraphsController < Projects::ApplicationController
+  include ExtractsPath
+
   # Authorize
   before_action :require_non_empty_project
+  before_action :assign_ref_vars
   before_action :authorize_download_code!
 
   def show
@@ -13,7 +16,7 @@ class Projects::GraphsController < Projects::ApplicationController
   end
 
   def commits
-    @commits = @project.repository.commits(nil, nil, 2000, 0, true)
+    @commits = @project.repository.commits(@ref, nil, 2000, 0, true)
     @commits_graph = Gitlab::Graphs::Commits.new(@commits)
     @commits_per_week_days = @commits_graph.commits_per_week_days
     @commits_per_time = @commits_graph.commits_per_time
@@ -23,7 +26,7 @@ class Projects::GraphsController < Projects::ApplicationController
   private
 
   def fetch_graph
-    @commits = @project.repository.commits(nil, nil, 6000, 0, true)
+    @commits = @project.repository.commits(@ref, nil, 6000, 0, true)
     @log = []
 
     @commits.each do |commit|
diff --git a/app/controllers/projects/refs_controller.rb b/app/controllers/projects/refs_controller.rb
index 01ca1537c0e..d83561cf32a 100644
--- a/app/controllers/projects/refs_controller.rb
+++ b/app/controllers/projects/refs_controller.rb
@@ -8,17 +8,21 @@ class Projects::RefsController < Projects::ApplicationController
   def switch
     respond_to do |format|
       format.html do
-        new_path = if params[:destination] == "tree"
-                     namespace_project_tree_path(@project.namespace, @project,
-                                                 (@id))
-                   elsif params[:destination] == "blob"
-                     namespace_project_blob_path(@project.namespace, @project,
-                                                 (@id))
-                   elsif params[:destination] == "graph"
-                     namespace_project_network_path(@project.namespace, @project, @id, @options)
-                   else
-                     namespace_project_commits_path(@project.namespace, @project, @id)
-                   end
+        new_path =
+          case params[:destination]
+          when "tree"
+            namespace_project_tree_path(@project.namespace, @project, @id)
+          when "blob"
+            namespace_project_blob_path(@project.namespace, @project, @id)
+          when "graph"
+            namespace_project_network_path(@project.namespace, @project, @id, @options)
+          when "graphs"
+            namespace_project_graph_path(@project.namespace, @project, @id)
+          when "graphs_commits"
+            commits_namespace_project_graph_path(@project.namespace, @project, @id)
+          else
+            namespace_project_commits_path(@project.namespace, @project, @id)
+          end
 
         redirect_to new_path
       end
diff --git a/app/views/projects/graphs/commits.html.haml b/app/views/projects/graphs/commits.html.haml
index 254a76e108b..141acbdcf72 100644
--- a/app/views/projects/graphs/commits.html.haml
+++ b/app/views/projects/graphs/commits.html.haml
@@ -1,9 +1,11 @@
 - page_title "Commit statistics"
+.tree-ref-holder
+  = render 'shared/ref_switcher', destination: 'graphs_commits'
 = render 'head'
 
 %p.lead
   Commit statistics for
-  %strong #{@repository.root_ref}
+  %strong #{@ref}
   #{@commits_graph.start_date.strftime('%b %d')} - #{@commits_graph.end_date.strftime('%b %d')}
 
 .row
diff --git a/app/views/projects/graphs/show.html.haml b/app/views/projects/graphs/show.html.haml
index 3a8dc89f84c..ecdd0eaf52f 100644
--- a/app/views/projects/graphs/show.html.haml
+++ b/app/views/projects/graphs/show.html.haml
@@ -1,5 +1,8 @@
 - page_title "Contributor statistics"
+.tree-ref-holder
+  = render 'shared/ref_switcher', destination: 'graphs'
 = render 'head'
+
 .loading-graph
   .center
     %h3.page-title
@@ -11,7 +14,7 @@
   .header.clearfix
     %h3#date_header.page-title
     %p.light
-      Commits to #{@project.default_branch}, excluding merge commits. Limited by 6,000 commits
+      Commits to #{@ref}, excluding merge commits. Limited by 6,000 commits
     %input#brush_change{:type => "hidden"}
   .graphs
     #contributors-master
@@ -35,4 +38,3 @@
       $(".stat-graph").fadeIn();
       $(".loading-graph").hide();
     dataType: "json"
-
-- 
GitLab