BigW Consortium Gitlab

Commit b82f415f by Lin Jen-Shin

Move all branch creation to raw_ensure_branch,

and keep it only called in update_branch_with_hooks.
parent d4d138ee
No related merge requests found
...@@ -998,17 +998,6 @@ class Project < ActiveRecord::Base ...@@ -998,17 +998,6 @@ class Project < ActiveRecord::Base
Gitlab::UploadsTransfer.new.rename_project(path_was, path, namespace.path) Gitlab::UploadsTransfer.new.rename_project(path_was, path, namespace.path)
end end
def fetch_ref(source_project, branch_name, ref)
repository.fetch_ref(
source_project.repository.path_to_repo,
"refs/heads/#{ref}",
"refs/heads/#{branch_name}"
)
repository.after_create_branch
repository.find_branch(branch_name)
end
# Expires various caches before a project is renamed. # Expires various caches before a project is renamed.
def expire_caches_before_rename(old_path) def expire_caches_before_rename(old_path)
repo = Repository.new(old_path, self) repo = Repository.new(old_path, self)
......
...@@ -784,13 +784,16 @@ class Repository ...@@ -784,13 +784,16 @@ class Repository
@tags ||= raw_repository.tags @tags ||= raw_repository.tags
end end
# rubocop:disable Metrics/ParameterLists
def commit_dir( def commit_dir(
user, path, message, branch, user, path, message, branch,
author_email: nil, author_name: nil, source_branch: nil) author_email: nil, author_name: nil,
source_branch: nil, source_project: project)
update_branch_with_hooks( update_branch_with_hooks(
user, user,
branch, branch,
source_branch: source_branch) do |ref| source_branch: source_branch,
source_project: source_project) do |ref|
options = { options = {
commit: { commit: {
branch: ref, branch: ref,
...@@ -804,15 +807,18 @@ class Repository ...@@ -804,15 +807,18 @@ class Repository
raw_repository.mkdir(path, options) raw_repository.mkdir(path, options)
end end
end end
# rubocop:enable Metrics/ParameterLists
# rubocop:disable Metrics/ParameterLists # rubocop:disable Metrics/ParameterLists
def commit_file( def commit_file(
user, path, content, message, branch, update, user, path, content, message, branch, update,
author_email: nil, author_name: nil, source_branch: nil) author_email: nil, author_name: nil,
source_branch: nil, source_project: project)
update_branch_with_hooks( update_branch_with_hooks(
user, user,
branch, branch,
source_branch: source_branch) do |ref| source_branch: source_branch,
source_project: source_project) do |ref|
options = { options = {
commit: { commit: {
branch: ref, branch: ref,
...@@ -837,11 +843,13 @@ class Repository ...@@ -837,11 +843,13 @@ class Repository
def update_file( def update_file(
user, path, content, user, path, content,
branch:, previous_path:, message:, branch:, previous_path:, message:,
author_email: nil, author_name: nil, source_branch: nil) author_email: nil, author_name: nil,
source_branch: nil, source_project: project)
update_branch_with_hooks( update_branch_with_hooks(
user, user,
branch, branch,
source_branch: source_branch) do |ref| source_branch: source_branch,
source_project: source_project) do |ref|
options = { options = {
commit: { commit: {
branch: ref, branch: ref,
...@@ -867,13 +875,16 @@ class Repository ...@@ -867,13 +875,16 @@ class Repository
end end
# rubocop:enable Metrics/ParameterLists # rubocop:enable Metrics/ParameterLists
# rubocop:disable Metrics/ParameterLists
def remove_file( def remove_file(
user, path, message, branch, user, path, message, branch,
author_email: nil, author_name: nil, source_branch: nil) author_email: nil, author_name: nil,
source_branch: nil, source_project: project)
update_branch_with_hooks( update_branch_with_hooks(
user, user,
branch, branch,
source_branch: source_branch) do |ref| source_branch: source_branch,
source_project: source_project) do |ref|
options = { options = {
commit: { commit: {
branch: ref, branch: ref,
...@@ -890,14 +901,18 @@ class Repository ...@@ -890,14 +901,18 @@ class Repository
Gitlab::Git::Blob.remove(raw_repository, options) Gitlab::Git::Blob.remove(raw_repository, options)
end end
end end
# rubocop:enable Metrics/ParameterLists
# rubocop:disable Metrics/ParameterLists
def multi_action( def multi_action(
user:, branch:, message:, actions:, user:, branch:, message:, actions:,
author_email: nil, author_name: nil, source_branch: nil) author_email: nil, author_name: nil,
source_branch: nil, source_project: project)
update_branch_with_hooks( update_branch_with_hooks(
user, user,
branch, branch,
source_branch: source_branch) do |ref| source_branch: source_branch,
source_project: source_project) do |ref|
index = rugged.index index = rugged.index
branch_commit = find_branch(ref) branch_commit = find_branch(ref)
...@@ -942,6 +957,7 @@ class Repository ...@@ -942,6 +957,7 @@ class Repository
Rugged::Commit.create(rugged, options) Rugged::Commit.create(rugged, options)
end end
end end
# rubocop:enable Metrics/ParameterLists
def get_committer_and_author(user, email: nil, name: nil) def get_committer_and_author(user, email: nil, name: nil)
committer = user_to_committer(user) committer = user_to_committer(user)
...@@ -991,8 +1007,6 @@ class Repository ...@@ -991,8 +1007,6 @@ class Repository
end end
def revert(user, commit, base_branch, revert_tree_id = nil) def revert(user, commit, base_branch, revert_tree_id = nil)
source_sha = raw_ensure_branch(base_branch, source_commit: commit).
first.dereferenced_target.sha
revert_tree_id ||= check_revert_content(commit, base_branch) revert_tree_id ||= check_revert_content(commit, base_branch)
return false unless revert_tree_id return false unless revert_tree_id
...@@ -1000,8 +1014,11 @@ class Repository ...@@ -1000,8 +1014,11 @@ class Repository
update_branch_with_hooks( update_branch_with_hooks(
user, user,
base_branch, base_branch,
source_branch: revert_tree_id) do source_commit: commit) do
source_sha = find_branch(base_branch).dereferenced_target.sha
committer = user_to_committer(user) committer = user_to_committer(user)
Rugged::Commit.create(rugged, Rugged::Commit.create(rugged,
message: commit.revert_message, message: commit.revert_message,
author: committer, author: committer,
...@@ -1012,8 +1029,6 @@ class Repository ...@@ -1012,8 +1029,6 @@ class Repository
end end
def cherry_pick(user, commit, base_branch, cherry_pick_tree_id = nil) def cherry_pick(user, commit, base_branch, cherry_pick_tree_id = nil)
source_sha = raw_ensure_branch(base_branch, source_commit: commit).
first.dereferenced_target.sha
cherry_pick_tree_id ||= check_cherry_pick_content(commit, base_branch) cherry_pick_tree_id ||= check_cherry_pick_content(commit, base_branch)
return false unless cherry_pick_tree_id return false unless cherry_pick_tree_id
...@@ -1021,8 +1036,11 @@ class Repository ...@@ -1021,8 +1036,11 @@ class Repository
update_branch_with_hooks( update_branch_with_hooks(
user, user,
base_branch, base_branch,
source_branch: cherry_pick_tree_id) do source_commit: commit) do
source_sha = find_branch(base_branch).dereferenced_target.sha
committer = user_to_committer(user) committer = user_to_committer(user)
Rugged::Commit.create(rugged, Rugged::Commit.create(rugged,
message: commit.message, message: commit.message,
author: { author: {
...@@ -1130,12 +1148,19 @@ class Repository ...@@ -1130,12 +1148,19 @@ class Repository
# Whenever `source_branch` is passed, if `branch` doesn't exist, it would # Whenever `source_branch` is passed, if `branch` doesn't exist, it would
# be created from `source_branch`. # be created from `source_branch`.
def update_branch_with_hooks(current_user, branch, source_branch: nil) def update_branch_with_hooks(
current_user, branch,
source_branch: nil, source_commit: nil, source_project: project)
update_autocrlf_option update_autocrlf_option
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
target_branch, new_branch_added = target_branch, new_branch_added =
raw_ensure_branch(branch, source_branch: source_branch) raw_ensure_branch(
branch,
source_branch: source_branch,
source_project: source_project
)
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
was_empty = empty? was_empty = empty?
# Make commit # Make commit
...@@ -1244,11 +1269,31 @@ class Repository ...@@ -1244,11 +1269,31 @@ class Repository
Gitlab::Metrics.add_event(event, { path: path_with_namespace }.merge(tags)) Gitlab::Metrics.add_event(event, { path: path_with_namespace }.merge(tags))
end end
def raw_ensure_branch(branch_name, source_commit: nil, source_branch: nil) def raw_ensure_branch(
branch_name, source_commit: nil, source_branch: nil, source_project: nil)
old_branch = find_branch(branch_name) old_branch = find_branch(branch_name)
if source_commit && source_branch
raise ArgumentError,
'Should only pass either :source_branch or :source_commit, not both'
end
if old_branch if old_branch
[old_branch, false] [old_branch, false]
elsif project != source_project
unless source_branch
raise ArgumentError,
'Should also pass :source_branch if' +
' :source_project is different from current project'
end
fetch_ref(
source_project.repository.path_to_repo,
"#{Gitlab::Git::BRANCH_REF_PREFIX}#{source_branch}",
"#{Gitlab::Git::BRANCH_REF_PREFIX}#{branch}"
)
[find_branch(branch_name), true]
elsif source_commit || source_branch elsif source_commit || source_branch
oldrev = Gitlab::Git::BLANK_SHA oldrev = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
......
require_relative 'base_service' require_relative 'base_service'
class CreateBranchService < BaseService class CreateBranchService < BaseService
def execute(branch_name, ref, source_project: @project) def execute(branch_name, ref)
failure = validate_new_branch(branch_name) failure = validate_new_branch(branch_name)
return failure if failure return failure if failure
new_branch = if source_project != @project new_branch = repository.add_branch(current_user, branch_name, ref)
@project.fetch_ref(source_project, branch_name, ref)
else
repository.add_branch(current_user, branch_name, ref)
end
if new_branch if new_branch
success(new_branch) success(new_branch)
......
...@@ -23,7 +23,7 @@ module Files ...@@ -23,7 +23,7 @@ module Files
validate validate
# Create new branch if it different from source_branch # Create new branch if it different from source_branch
ensure_target_branch if different_branch? validate_target_branch if different_branch?
result = commit result = commit
if result if result
...@@ -71,17 +71,6 @@ module Files ...@@ -71,17 +71,6 @@ module Files
end end
end end
def ensure_target_branch
validate_target_branch
if @source_project != project
# Make sure we have the source_branch in target project,
# and use source_branch as target_branch directly to avoid
# unnecessary source branch in target project.
@project.fetch_ref(@source_project, @target_branch, @source_branch)
end
end
def validate_target_branch def validate_target_branch
result = ValidateNewBranchService.new(project, current_user). result = ValidateNewBranchService.new(project, current_user).
execute(@target_branch) execute(@target_branch)
......
...@@ -11,6 +11,7 @@ module Files ...@@ -11,6 +11,7 @@ module Files
message: @commit_message, message: @commit_message,
author_email: @author_email, author_email: @author_email,
author_name: @author_name, author_name: @author_name,
source_project: @source_project,
source_branch: @source_branch) source_branch: @source_branch)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment