From 6f67b580c489c5a95abd6e57e5a4e0b3ccc47a05 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Mon, 22 Jul 2013 15:27:14 +0400 Subject: [PATCH] #222: added form for creating new branch --- .../controllers/project_refs_controller.js | 17 +++++++++++++++++ .../angularjs/models/project_ref.js | 1 + .../javascripts/angularjs/services/project.js | 5 +++++ app/assets/stylesheets/design/custom.scss | 5 ++++- .../projects/git/trees_controller.rb | 9 +++++++-- .../projects/git/trees/branches.html.haml | 18 ++++++++++++++---- config/locales/models/project.en.yml | 1 + config/locales/models/project.ru.yml | 1 + config/routes.rb | 1 + lib/modules/models/git.rb | 8 ++++++++ 10 files changed, 59 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/angularjs/controllers/project_refs_controller.js b/app/assets/javascripts/angularjs/controllers/project_refs_controller.js index a7b7e1ffa..512341851 100644 --- a/app/assets/javascripts/angularjs/controllers/project_refs_controller.js +++ b/app/assets/javascripts/angularjs/controllers/project_refs_controller.js @@ -7,6 +7,7 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu $scope.project_id = null; $scope.current_ref = null; $scope.project_resource = null; + $scope.new_branch = false; $scope.init = function(project_id, ref, locale) { $scope.project_id = project_id; @@ -49,6 +50,22 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu $scope.singleton.project.branches_count = $scope.branches.length; } + $scope.create = function(branch) { + branch.ui_container = false; + $scope.project_resource.$create_branch( + { + owner: $scope.project.owner.uname, + project: $scope.project.name, + from_ref: branch.ref, + new_ref: branch.new_ref + }, function() { // on success + $scope.getRefs(); + }, function () { // on error + $scope.getRefs(); + } + ); + } + $scope.destroy = function(branch) { $scope.project_resource.$delete_branch( {owner: $scope.project.owner.uname, project: $scope.project.name, ref: branch.ref}, diff --git a/app/assets/javascripts/angularjs/models/project_ref.js b/app/assets/javascripts/angularjs/models/project_ref.js index b13b40dc9..8e8258374 100644 --- a/app/assets/javascripts/angularjs/models/project_ref.js +++ b/app/assets/javascripts/angularjs/models/project_ref.js @@ -11,6 +11,7 @@ var ProjectRef = function(atts) { //with some logic... self.isTag = self.object.type == 'tag'; + self.ui_container = false; self.path = function(project) { return '/' + project.fullname + '/tree/' + self.ref; diff --git a/app/assets/javascripts/angularjs/services/project.js b/app/assets/javascripts/angularjs/services/project.js index 66a55dff2..3b34e4617 100644 --- a/app/assets/javascripts/angularjs/services/project.js +++ b/app/assets/javascripts/angularjs/services/project.js @@ -18,6 +18,11 @@ RosaABF.factory("ApiProject", ['$resource', function($resource) { url: '/:owner/:project/branches/:ref', // ?sha= method: 'PUT', isArray : false + }, + create_branch: { + url: '/:owner/:project/branches', // ?new_ref=&from_ref= + method: 'POST', + isArray : false } } ); diff --git a/app/assets/stylesheets/design/custom.scss b/app/assets/stylesheets/design/custom.scss index 76a7224b2..7d5e43824 100644 --- a/app/assets/stylesheets/design/custom.scss +++ b/app/assets/stylesheets/design/custom.scss @@ -75,7 +75,10 @@ header menu ul li a { td.actions ul { float: right; } - .text { font-size: 12px; } + .text { + font-size: 12px; + padding-top: 2px; + } td { border-bottom: 1px solid #a9c6dd; padding: 0 20px; diff --git a/app/controllers/projects/git/trees_controller.rb b/app/controllers/projects/git/trees_controller.rb index 9cb34a9ba..27e2a7d0d 100644 --- a/app/controllers/projects/git/trees_controller.rb +++ b/app/controllers/projects/git/trees_controller.rb @@ -4,8 +4,8 @@ class Projects::Git::TreesController < Projects::Git::BaseController skip_before_filter :set_branch_and_tree, :set_treeish_and_path, :only => :archive before_filter lambda { raise Grit::NoSuchPathError if params[:treeish] != @branch.try(:name) }, :only => [:branch, :destroy] - skip_authorize_resource :project, :only => [:destroy, :restore_branch] - before_filter lambda { authorize!(:write, @project) }, :only => [:destroy, :restore_branch] + skip_authorize_resource :project, :only => [:destroy, :restore_branch, :create] + before_filter lambda { authorize!(:write, @project) }, :only => [:destroy, :restore_branch, :create] def show render('empty') and return if @project.is_empty? @@ -39,6 +39,11 @@ class Projects::Git::TreesController < Projects::Git::BaseController render :nothing => true end + def create + status = @project.create_branch(params[:new_ref], params[:from_ref]) ? 200 : 422 + render :nothing => true, :status => status + end + def destroy status = @branch && @project.delete_branch(@branch, current_user) ? 200 : 422 render :nothing => true, :status => status diff --git a/app/views/projects/git/trees/branches.html.haml b/app/views/projects/git/trees/branches.html.haml index 912207c01..c05770993 100644 --- a/app/views/projects/git/trees/branches.html.haml +++ b/app/views/projects/git/trees/branches.html.haml @@ -22,13 +22,23 @@ %a{'ng-href' => '{{branch.path(project)}}' } {{branch.ref}} %td.actions %ul.actions - %li.text{'ng-show' => 'branch.ref == current_ref'} - = t('layout.projects.base_branch') - if can?(:write, @project) - %li{'ng-hide' => 'branch.ref == current_ref'} + %li{'ng-hide' => 'branch.ref == current_ref || branch.ui_container'} %a{:href => '', 'ng-click' => 'destroy(branch)'} = t('layout.projects.delete_branch') - %li{'ng-hide' => 'branch.ref == current_ref'} + %li{'ng-hide' => 'branch.ui_container'} + %a{:href => '', 'ng-click' => 'branch.ui_container = true'} + = t('layout.projects.new_branch') + %li{'ng-show' => 'branch.ui_container'} + %form{'ng-submit' => 'create(branch)'} + %input{:name => 'new_ref', 'ng-model' => 'branch.new_ref'} + %input{:type => 'submit', :value =>t('layout.create')} + %a{:href => '', 'ng-click' => 'branch.ui_container = false'} + = t('layout.cancel') + %li{'ng-hide' => 'branch.ref == current_ref || branch.ui_container'} %a{'ng-href' => '{{branch.diff_path(project, current_ref)}}' } = t('layout.projects.compare') + %li.text{'ng-show' => 'branch.ref == current_ref && !branch.ui_container'} + = t('layout.projects.base_branch') + diff --git a/config/locales/models/project.en.yml b/config/locales/models/project.en.yml index 1492f8fb9..bf018c003 100644 --- a/config/locales/models/project.en.yml +++ b/config/locales/models/project.en.yml @@ -28,6 +28,7 @@ en: new_build_list: New build confirm_delete: Are you sure you want to delete this project? new: New project + new_branch: New branch location: Location git_repo_location: Path to git repo current_project_header: Current project diff --git a/config/locales/models/project.ru.yml b/config/locales/models/project.ru.yml index a647bb2b7..7f9cfc5fc 100644 --- a/config/locales/models/project.ru.yml +++ b/config/locales/models/project.ru.yml @@ -28,6 +28,7 @@ ru: new_build_list: Новая сборка confirm_delete: Вы уверены, что хотите удалить этот проект? new: Новый проект + new_branch: Новая ветка location: Расположение git_repo_location: Путь к git-репозиторию current_project_header: Текущий проект diff --git a/config/routes.rb b/config/routes.rb index be0045a48..942acad29 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -337,6 +337,7 @@ Rosa::Application.routes.draw do get '/branches/:treeish' => "git/trees#branches", :as => :branches delete '/branches/:treeish' => "git/trees#destroy", :as => :branches put '/branches/:treeish' => "git/trees#restore_branch", :as => :branches + post '/branches' => "git/trees#create", :as => :branches # Commits get '/commits/:treeish(/*path)' => "git/commits#index", :as => :commits, :format => false get '/commit/:id(.:format)' => "git/commits#show", :as => :commit diff --git a/lib/modules/models/git.rb b/lib/modules/models/git.rb index 2de1d4b58..496d12c7d 100644 --- a/lib/modules/models/git.rb +++ b/lib/modules/models/git.rb @@ -36,6 +36,14 @@ module Modules # TODO: return something else instead of empty string on success and error def restore_branch(branch, sha) repo.git.native(:branch, {}, branch, sha) + return true + end + + def create_branch(new_ref, from_ref) + return false if new_ref.blank? || from_ref.blank? || + repo.branches.none?{|b| b.name == from_ref} || + repo.branches.one?{|b| b.name == new_ref} + restore_branch new_ref, from_ref end def delete_branch(branch, user)