From cbe9f56afae2b1e55de96f59eed36859eea6eb82 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Wed, 17 Jul 2013 00:09:23 +0300
Subject: [PATCH] Ability to create new branch via UI

---
 .../projects/branches_controller.rb           |  6 +++--
 app/models/repository.rb                      | 10 ++++++++
 app/views/projects/branches/new.html.haml     | 24 +++++++++++++++++++
 .../projects/repositories/_filter.html.haml   |  7 ++++++
 config/routes.rb                              |  4 ++--
 5 files changed, 47 insertions(+), 4 deletions(-)
 create mode 100644 app/views/projects/branches/new.html.haml

diff --git a/app/controllers/projects/branches_controller.rb b/app/controllers/projects/branches_controller.rb
index b43017f5522..a9d9cfb61e1 100644
--- a/app/controllers/projects/branches_controller.rb
+++ b/app/controllers/projects/branches_controller.rb
@@ -10,7 +10,9 @@ class Projects::BranchesController < Projects::ApplicationController
   end
 
   def create
-    # TODO: implement
+    @project.repository.add_branch(params[:branch_name], params[:ref])
+
+    redirect_to project_branches_path(@project)
   end
 
   def destroy
@@ -21,7 +23,7 @@ class Projects::BranchesController < Projects::ApplicationController
     end
 
     respond_to do |format|
-      format.html { redirect_to project_branches_path }
+      format.html { redirect_to project_branches_path(@project) }
       format.js { render nothing: true }
     end
   end
diff --git a/app/models/repository.rb b/app/models/repository.rb
index 08574625012..b1f751a5cf3 100644
--- a/app/models/repository.rb
+++ b/app/models/repository.rb
@@ -35,11 +35,21 @@ class Repository
     commits
   end
 
+  def add_branch(branch_name, ref)
+    Rails.cache.delete(cache_key(:branch_names))
+
+    gitlab_shell.add_branch(path_with_namespace, branch_name, ref)
+  end
+
   def rm_branch(branch_name)
+    Rails.cache.delete(cache_key(:branch_names))
+
     gitlab_shell.rm_branch(path_with_namespace, branch_name)
   end
 
   def rm_tag(tag_name)
+    Rails.cache.delete(cache_key(:tag_names))
+
     gitlab_shell.rm_tag(path_with_namespace, tag_name)
   end
 
diff --git a/app/views/projects/branches/new.html.haml b/app/views/projects/branches/new.html.haml
new file mode 100644
index 00000000000..8df2c03f6c1
--- /dev/null
+++ b/app/views/projects/branches/new.html.haml
@@ -0,0 +1,24 @@
+%h3.page-title
+  %i.icon-code-fork
+  New branch
+= form_tag project_branches_path, method: :post do
+  .control-group
+    = label_tag :branch_name, 'Name for new branch', class: 'control-label'
+    .controls
+      = text_field_tag :branch_name, nil, placeholder: 'feature/dashboard'
+  .control-group
+    = label_tag :ref, 'Create from', class: 'control-label'
+    .controls
+      = text_field_tag :ref, nil, placeholder: 'master'
+      .light branch name or commit SHA
+  .form-actions
+    = submit_tag 'Create branch', class: 'btn btn-create'
+    = link_to 'Cancel', project_branches_path(@project), class: 'btn btn-cancel'
+
+:javascript
+  var availableTags = #{@project.repository.ref_names.to_json};
+
+  $("#ref").autocomplete({
+    source: availableTags,
+    minLength: 1
+  });
diff --git a/app/views/projects/repositories/_filter.html.haml b/app/views/projects/repositories/_filter.html.haml
index f42493ea4a0..138fd6d5118 100644
--- a/app/views/projects/repositories/_filter.html.haml
+++ b/app/views/projects/repositories/_filter.html.haml
@@ -7,3 +7,10 @@
       %i.icon-lock
   = nav_link(path: 'branches#index') do
     = link_to 'All branches', project_branches_path(@project)
+
+
+%hr
+  = link_to new_project_branch_path(@project), class: 'btn btn-create' do
+    %i.icon-add-sign
+    New branch
+
diff --git a/config/routes.rb b/config/routes.rb
index 0bae5d44395..2d9875eb496 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -223,8 +223,8 @@ Gitlab::Application.routes.draw do
         end
       end
 
-      resources :tags, only: [:index, :create, :destroy]
-      resources :branches, only: [:index, :create, :destroy]
+      resources :tags, only: [:index, :new, :create, :destroy]
+      resources :branches, only: [:index, :new, :create, :destroy]
       resources :protected_branches, only: [:index, :create, :destroy]
 
       resources :refs, only: [] do
-- 
GitLab