diff --git a/app/assets/javascripts/angularjs/controllers/project_branches_controller.js b/app/assets/javascripts/angularjs/controllers/project_branches_controller.js index d181dbb33..8435a51f3 100644 --- a/app/assets/javascripts/angularjs/controllers/project_branches_controller.js +++ b/app/assets/javascripts/angularjs/controllers/project_branches_controller.js @@ -43,6 +43,22 @@ RosaABF.controller('ProjectBranchesController', ['$scope', '$http', 'ApiProject' $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 1db0de864..851a95a98 100644 --- a/app/assets/javascripts/angularjs/services/project.js +++ b/app/assets/javascripts/angularjs/services/project.js @@ -23,6 +23,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 8a6ed88be..c675a35c2 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 unless request.xhr? @@ -41,8 +41,13 @@ class Projects::Git::TreesController < Projects::Git::BaseController end def restore_branch - @project.restore_branch @treeish, params[:sha] if @treeish.present? && params[:sha].present? - render :nothing => true + status = @project.create_branch(@treeish, params[:sha], current_user) ? 200 : 422 + render :nothing => true, :status => status + end + + def create + status = @project.create_branch(params[:new_ref], params[:from_ref], current_user) ? 200 : 422 + render :nothing => true, :status => status end def destroy diff --git a/app/helpers/diff_helper.rb b/app/helpers/diff_helper.rb index 2c522e519..6974ca1ad 100644 --- a/app/helpers/diff_helper.rb +++ b/app/helpers/diff_helper.rb @@ -37,7 +37,7 @@ module DiffHelper end prepare(args.merge({:filepath => filepath, :comments => comments, :in_discussion => in_discussion})) - res = "" + res = "
" res += "" res += renderer diff_display.data #diff_display.render(Git::Diff::InlineCallback.new comments, path) res += tr_line_comments(comments) if in_discussion diff --git a/app/views/projects/base/_repo_block.html.haml b/app/views/projects/base/_repo_block.html.haml index 5bea6e279..e641613dc 100644 --- a/app/views/projects/base/_repo_block.html.haml +++ b/app/views/projects/base/_repo_block.html.haml @@ -42,7 +42,7 @@ %li{:class => ('selected' if act == :index && contr == :commits )} = link_to t('project_menu.commits'), commits_path(project, treeish) %li{:class => ('selected' if act == :branches && contr == :trees )} - = link_to t('project_menu.branches', :count => '{{singleton.project.branches_count}}'), branches_path(project, branch) + = link_to t('project_menu.branches', :count => '{{singleton.project.branches_count}}'), branch_path(project, branch) %li.tags{:class => ('selected' if act == :tags && contr == :trees )} = link_to t('project_menu.tags', :count => project.repo.tags.count), tags_path(project) .both diff --git a/app/views/projects/git/trees/branches.html.haml b/app/views/projects/git/trees/branches.html.haml index b4a0a621d..304ce7515 100644 --- a/app/views/projects/git/trees/branches.html.haml +++ b/app/views/projects/git/trees/branches.html.haml @@ -21,13 +21,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{'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 78807a1c6..f296ebe97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -335,10 +335,11 @@ Rosa::Application.routes.draw do # Tags get '/tags' => "git/trees#tags", :as => :tags # Branches - get '/branches/:treeish' => "git/trees#branches", :as => :branches get '/branches' => "git/trees#branches", :as => :branches - delete '/branches/:treeish' => "git/trees#destroy", :as => :branches - put '/branches/:treeish' => "git/trees#restore_branch", :as => :branches + get '/branches/:treeish' => "git/trees#branches", :as => :branch + delete '/branches/:treeish' => "git/trees#destroy", :as => :branch + put '/branches/:treeish' => "git/trees#restore_branch", :as => :branch + 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..032d8bb57 100644 --- a/lib/modules/models/git.rb +++ b/lib/modules/models/git.rb @@ -33,16 +33,22 @@ module Modules repo.tags.map(&:name) + repo.branches.map(&:name) end - # TODO: return something else instead of empty string on success and error - def restore_branch(branch, sha) - repo.git.native(:branch, {}, branch, sha) + def create_branch(new_ref, from_ref, user) + return false if new_ref.blank? || from_ref.blank? || !(from_commit = repo.commit(from_ref)) + status, out, err = repo.git.native(:branch, {:process_info => true}, new_ref, from_commit.id) + if status == 0 + Resque.enqueue(GitHook, owner.uname, name, from_commit.id, GitHook::ZERO, "refs/heads/#{new_ref}", 'commit', "user-#{user.id}", nil) + return true + end + return false + end def delete_branch(branch, user) return false if default_branch == branch.name message = repo.git.native(:branch, {}, '-D', branch.name) if message.present? - Resque.enqueue(GitHook,owner.uname, name, GitHook::ZERO, branch.commit.id, "refs/heads/#{branch.name}", 'commit', "user-#{user.id}", message) + Resque.enqueue(GitHook, owner.uname, name, GitHook::ZERO, branch.commit.id, "refs/heads/#{branch.name}", 'commit', "user-#{user.id}", message) end return message.present? end diff --git a/spec/controllers/projects/git/git_trees_controller_spec.rb b/spec/controllers/projects/git/git_trees_controller_spec.rb index f2eb72079..aca37adfd 100644 --- a/spec/controllers/projects/git/git_trees_controller_spec.rb +++ b/spec/controllers/projects/git/git_trees_controller_spec.rb @@ -47,6 +47,11 @@ describe Projects::Git::TreesController do response.should_not be_success end + it 'should not be able to perform create action' do + post :create, @params.merge(:treeish => '', :from_ref => 'master', :new_ref => 'master-1') + response.should_not be_success + end + end context 'for other user' do @@ -80,6 +85,11 @@ describe Projects::Git::TreesController do response.should_not be_success end + it 'should not be able to perform create action' do + post :create, @params.merge(:treeish => '', :from_ref => 'master', :new_ref => 'master-1') + response.should_not be_success + end + [:tags, :branches].each do |action| it "should be able to perform #{action} action" do get action, @params.merge(:treeish => 'master') @@ -106,7 +116,12 @@ describe Projects::Git::TreesController do end it 'should be able to perform restore_branch action' do - put :restore_branch, @params.merge(:treeish => 'conflicts') + put :restore_branch, @params.merge(:treeish => 'master-1', :sha => 'master') + response.should be_success + end + + it 'should be able to perform create action' do + post :create, @params.merge(:treeish => '', :from_ref => 'master', :new_ref => 'master-1') response.should be_success end end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index e61151ab9..502f2ef89 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -117,21 +117,21 @@ describe Project do end end - context '#restore_branch' do + context '#create_branch' do before do project.delete_branch(branch, user) end - xit 'ensures that returns true on success' do - project.restore_branch(branch.name, branch.commit.id).should be_true + it 'ensures that returns true on success' do + project.create_branch(branch.name, branch.commit.id, user).should be_true end - it 'ensures that branch has been restored' do - lambda { project.restore_branch(branch.name, branch.commit.id) }.should change{ project.repo.branches.count }.by(1) + it 'ensures that branch has been created' do + lambda { project.create_branch(branch.name, branch.commit.id, user) }.should change{ project.repo.branches.count }.by(1) end - xit 'ensures that returns false on restore wrong branch' do - project.restore_branch(branch.name, GitHook::ZERO).should be_false + it 'ensures that returns false on create wrong branch' do + project.create_branch(branch.name, GitHook::ZERO, user).should be_false end end