fork重名,提交

This commit is contained in:
huang 2015-12-04 15:49:05 +08:00
parent 1c08c79e8e
commit c1747ab217
5 changed files with 56 additions and 37 deletions

View File

@ -64,32 +64,53 @@ class RepositoriesController < ApplicationController
end
def forked
# 被forked的标识如果不满足单个用户唯一性则不执行fork
if is_sigle_identifier?(User.current, @repository.identifier)
# REDO: 那些人有权限forked项目
g = Gitlab.client
gproject = g.post ("/projects/fork/#{@project.gpid}?user_id=#{User.current.gid}")
if gproject
copy_project(@project, gproject)
end
# 如果当前用户已经fork过该项目不会新fork项目则跳至已fork的项
unless has_forked?(@project, User.current)
project = project_from_current_project(@project.id, User.current.id)
redirect_to project_path(project)
else
flash[:notice] = l(:project_gitlab_fork_double_message)
redirect_to settings_project_url(@project, :tab => 'repositories')
# 单个用户只能拥有一个名字一样的版本库否则不能fork
# if is_sigle_identifier?(User.current, @repository.identifier)
# REDO: 那些人有权限forked项目
g = Gitlab.client
gproject = g.fork(@project.gpid, User.current.gid)
if gproject
copy_project(@project, gproject)
forked_count = @project.forked_count.to_i + 1
@project.update_attributes(:forked_count => forked_count)
end
# else
# flash[:notice] = l(:project_gitlab_fork_double_message)
# redirect_to settings_project_url(@project, :tab => 'repositories')
# end
end
end
# 判断用户是否已经fork过该项目
def has_forked?(project, user)
projects = Project.where("user_id =?", user)
projects.map(&:forked_from_project_id).detect{|s| s == @project.id}.nil? ? true : false
end
# 获取当前用户fork过的项目
def project_from_current_project(project, user)
project = Project.where("user_id =? and forked_from_project_id =?",user, project).first
end
# copy a project for fork
def copy_project(project, gproject)
def copy_project(tproject, gproject)
project = Project.new
project.name = @project.name
project.is_public = @project.is_public
project.status = @project.status
project.description = @project.description
project.hidden_repo = @project.hidden_repo
project.name = tproject.name
project.is_public = tproject.is_public
project.status = tproject.status
project.description = tproject.description
project.hidden_repo = tproject.hidden_repo
project.user_id = User.current.id
project.project_type = 0
project.project_new_type = @project.project_new_type
project.project_new_type = tproject.project_new_type
project.gpid = gproject.id
project.forked_from_project_id = tproject.id
if project.save
r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
m = Member.new(:user => User.current, :roles => [r])
@ -124,16 +145,16 @@ class RepositoriesController < ApplicationController
def copy_repository(project, gproject)
# 避免
if is_sigle_identifier?(project.user_id, gproject.name)
# if is_sigle_identifier?(project.user_id, gproject.name)
repository = Repository.factory('Git')
repository.project_id = project.id
repository.type = 'Repository::Gitlab'
repository.url = gproject.name
repository.identifier = gproject.name
repository = repository.save
else
flash[:notice] = l(:project_gitlab_create_double_message)
end
# else
# flash[:notice] = l(:project_gitlab_create_double_message)
# end
end
def newrepo

View File

@ -25,8 +25,10 @@
<textarea id="copy_rep_content" class="cloneUrl mt5 fl" type="input" placeholder="http://xianbo_trustie2@repository.trustie.net/xianbo/trustie2.git"><%=@repository.type.to_s=="Repository::Gitlab" ? @repos_url.to_s.lstrip : @repository.url %></textarea>
<a href="javascript:void(0);" class="clone_btn mt5" onclick="jsCopy()"><span class="vl_copy" title="点击复制版本库地址"></span></a>
<div class="fl mt5 ml15"><a href="javascript:void(0);" class="vl_btn fb" onclick="zip()"><span class="vl_zip"></span>ZIP</a> </div>
<div class="fr mt5"><a href="javascript:void(0);" class="vl_btn fb" onclick="zip()"><span class="vl_fork"></span>Fork</a> <span href="javascript:void(0);" class="vl_btn_2 fb">0</span> </div>
<!--<div class="fr mt5"><a href="javascript:void(0);" class="vl_btn fb" onclick="zip()"><span class="vl_fork"></span><%= link_to "Fork", :controller => 'repositories', :action => 'forked' %></a> <span href="javascript:void(0);" class="vl_btn_2 fb">0</span> </div>-->
<!--<div class="fr mt5"><a href="javascript:void(0);" class="vl_btn fb" onclick="zip()"><span class="vl_fork"></span>Fork</a> <span href="javascript:void(0);" class="vl_btn_2 fb">0</span> </div>-->
<div class="fr mt5"><span class="vl_fork"></span><%= link_to "Fork", :controller => 'repositories', :action => 'forked'%>
<span href="javascript:void(0);" class="vl_btn_2 fb"><%= @project.forked_count.to_i %></span> </div>
<div class="cl"></div>
<div class="recordBanner mt10">
<% if @changesets && !@changesets.empty? %>

View File

@ -0,0 +1,5 @@
class AddForkedCountToProjects < ActiveRecord::Migration
def change
add_column :projects, :forked_count, :integer
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20151203072815) do
ActiveRecord::Schema.define(:version => 20151204062220) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -738,17 +738,6 @@ ActiveRecord::Schema.define(:version => 20151203072815) do
add_index "homework_commons", ["course_id", "id"], :name => "index_homework_commons_on_course_id_and_id"
create_table "homework_detail_groups", :force => true do |t|
t.integer "homework_common_id"
t.integer "min_num"
t.integer "max_num"
t.integer "base_on_project"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "homework_detail_groups", ["homework_common_id"], :name => "index_homework_detail_groups_on_homework_common_id"
create_table "homework_detail_manuals", :force => true do |t|
t.float "ta_proportion"
t.integer "comment_status"
@ -1330,6 +1319,7 @@ ActiveRecord::Schema.define(:version => 20151203072815) do
t.integer "project_new_type"
t.integer "gpid"
t.integer "forked_from_project_id"
t.integer "forked_count"
end
add_index "projects", ["lft"], :name => "index_projects_on_lft"

View File

@ -241,8 +241,9 @@ class Gitlab::Client
# Forks a project into the user namespace of the authenticated user.
# @param [Integer] - The ID of the project to be forked
def fork(id)
post("/projects/fork/#{id}")
def fork(gpid, gid)
post ("/projects/fork/#{gpid}?user_id=#{gid}")
# post("/projects/fork/#{id}")
end
# Mark this project as forked from the other