2016-12-13 13:03:04 -05:00
|
|
|
class GitOperationService
|
|
|
|
attr_reader :user, :repository
|
|
|
|
|
|
|
|
def initialize(new_user, new_repository)
|
|
|
|
@user = new_user
|
|
|
|
@repository = new_repository
|
|
|
|
end
|
|
|
|
|
2016-12-05 14:13:15 -05:00
|
|
|
def add_branch(branch_name, newrev)
|
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
|
|
|
|
oldrev = Gitlab::Git::BLANK_SHA
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
update_ref_in_hooks(ref, newrev, oldrev)
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def rm_branch(branch)
|
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch.name
|
2017-01-04 13:53:45 -05:00
|
|
|
oldrev = branch.target
|
2016-12-05 14:13:15 -05:00
|
|
|
newrev = Gitlab::Git::BLANK_SHA
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
update_ref_in_hooks(ref, newrev, oldrev)
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_tag(tag_name, newrev, options = {})
|
|
|
|
ref = Gitlab::Git::TAG_REF_PREFIX + tag_name
|
|
|
|
oldrev = Gitlab::Git::BLANK_SHA
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
with_hooks(ref, newrev, oldrev) do |service|
|
2017-01-04 09:50:01 -05:00
|
|
|
# We want to pass the OID of the tag object to the hooks. For an
|
|
|
|
# annotated tag we don't know that OID until after the tag object
|
|
|
|
# (raw_tag) is created in the repository. That is why we have to
|
|
|
|
# update the value after creating the tag object. Only the
|
|
|
|
# "post-receive" hook will receive the correct value in this case.
|
2016-12-05 14:13:15 -05:00
|
|
|
raw_tag = repository.rugged.tags.create(tag_name, newrev, options)
|
|
|
|
service.newrev = raw_tag.target_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-04 13:53:45 -05:00
|
|
|
def rm_tag(tag)
|
|
|
|
ref = Gitlab::Git::TAG_REF_PREFIX + tag.name
|
|
|
|
oldrev = tag.target
|
|
|
|
newrev = Gitlab::Git::BLANK_SHA
|
|
|
|
|
|
|
|
update_ref_in_hooks(ref, newrev, oldrev) do
|
|
|
|
repository.rugged.tags.delete(tag_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-06 10:29:13 -05:00
|
|
|
# Whenever `start_branch_name` is passed, if `branch_name` doesn't exist,
|
|
|
|
# it would be created from `start_branch_name`.
|
|
|
|
# If `start_project` is passed, and the branch doesn't exist,
|
|
|
|
# it would try to find the commits from it instead of current repository.
|
2016-12-05 14:13:15 -05:00
|
|
|
def with_branch(
|
|
|
|
branch_name,
|
2017-01-06 10:29:13 -05:00
|
|
|
start_branch_name: nil,
|
|
|
|
start_project: repository.project,
|
2017-01-04 12:52:21 -05:00
|
|
|
&block)
|
2016-12-05 14:13:15 -05:00
|
|
|
|
2016-12-08 04:08:25 -05:00
|
|
|
check_with_branch_arguments!(
|
2017-01-06 10:29:13 -05:00
|
|
|
branch_name, start_branch_name, start_project)
|
2016-12-05 14:13:15 -05:00
|
|
|
|
2016-12-09 11:40:23 -05:00
|
|
|
update_branch_with_hooks(branch_name) do
|
2017-01-04 12:52:21 -05:00
|
|
|
repository.with_repo_branch_commit(
|
2017-01-06 10:29:13 -05:00
|
|
|
start_project.repository,
|
|
|
|
start_branch_name || branch_name,
|
2017-01-04 12:52:21 -05:00
|
|
|
&block)
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-12-08 04:08:25 -05:00
|
|
|
def update_branch_with_hooks(branch_name)
|
2016-12-07 09:37:43 -05:00
|
|
|
update_autocrlf_option
|
2016-12-05 14:13:15 -05:00
|
|
|
|
2016-12-07 09:37:43 -05:00
|
|
|
was_empty = repository.empty?
|
2016-12-06 05:47:24 -05:00
|
|
|
|
2016-12-07 09:37:43 -05:00
|
|
|
# Make commit
|
2016-12-09 11:40:23 -05:00
|
|
|
newrev = yield
|
2016-12-05 14:13:15 -05:00
|
|
|
|
2016-12-07 09:37:43 -05:00
|
|
|
unless newrev
|
|
|
|
raise Repository::CommitError.new('Failed to create commit')
|
|
|
|
end
|
2016-12-06 05:47:24 -05:00
|
|
|
|
2016-12-07 09:37:43 -05:00
|
|
|
branch = repository.find_branch(branch_name)
|
2017-01-26 08:59:12 -05:00
|
|
|
oldrev = find_oldrev_from_branch(newrev, branch)
|
2016-12-05 14:13:15 -05:00
|
|
|
|
2016-12-09 11:40:23 -05:00
|
|
|
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
|
2016-12-13 13:29:35 -05:00
|
|
|
update_ref_in_hooks(ref, newrev, oldrev)
|
|
|
|
|
|
|
|
# If repo was empty expire cache
|
|
|
|
repository.after_create if was_empty
|
2017-01-04 12:10:35 -05:00
|
|
|
repository.after_create_branch if
|
|
|
|
was_empty || Gitlab::Git.blank_ref?(oldrev)
|
2016-12-07 09:37:43 -05:00
|
|
|
|
|
|
|
newrev
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
|
2017-01-26 08:59:12 -05:00
|
|
|
def find_oldrev_from_branch(newrev, branch)
|
|
|
|
return Gitlab::Git::BLANK_SHA unless branch
|
|
|
|
|
|
|
|
oldrev = branch.target
|
|
|
|
|
|
|
|
if oldrev == repository.rugged.merge_base(newrev, branch.target)
|
|
|
|
oldrev
|
|
|
|
else
|
|
|
|
raise Repository::CommitError.new('Branch diverged')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
def update_ref_in_hooks(ref, newrev, oldrev)
|
|
|
|
with_hooks(ref, newrev, oldrev) do
|
|
|
|
update_ref(ref, newrev, oldrev)
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
def with_hooks(ref, newrev, oldrev)
|
2016-12-05 14:13:15 -05:00
|
|
|
GitHooksService.new.execute(
|
|
|
|
user,
|
|
|
|
repository.path_to_repo,
|
|
|
|
oldrev,
|
|
|
|
newrev,
|
|
|
|
ref) do |service|
|
|
|
|
|
2017-01-26 06:35:19 -05:00
|
|
|
yield(service)
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-13 13:29:35 -05:00
|
|
|
def update_ref(ref, newrev, oldrev)
|
2016-12-05 14:13:15 -05:00
|
|
|
# We use 'git update-ref' because libgit2/rugged currently does not
|
|
|
|
# offer 'compare and swap' ref updates. Without compare-and-swap we can
|
|
|
|
# (and have!) accidentally reset the ref to an earlier state, clobbering
|
|
|
|
# commits. See also https://github.com/libgit2/libgit2/issues/1534.
|
|
|
|
command = %W[#{Gitlab.config.git.bin_path} update-ref --stdin -z]
|
|
|
|
_, status = Gitlab::Popen.popen(
|
|
|
|
command,
|
|
|
|
repository.path_to_repo) do |stdin|
|
2016-12-13 13:29:35 -05:00
|
|
|
stdin.write("update #{ref}\x00#{newrev}\x00#{oldrev}\x00")
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
unless status.zero?
|
|
|
|
raise Repository::CommitError.new(
|
2016-12-13 13:29:35 -05:00
|
|
|
"Could not update branch #{Gitlab::Git.branch_name(ref)}." \
|
2016-12-05 14:13:15 -05:00
|
|
|
" Please refresh and try again.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_autocrlf_option
|
|
|
|
if repository.raw_repository.autocrlf != :input
|
|
|
|
repository.raw_repository.autocrlf = :input
|
|
|
|
end
|
|
|
|
end
|
2016-12-07 09:37:43 -05:00
|
|
|
|
2016-12-08 04:27:50 -05:00
|
|
|
def check_with_branch_arguments!(
|
2017-01-06 10:29:13 -05:00
|
|
|
branch_name, start_branch_name, start_project)
|
2016-12-07 09:37:43 -05:00
|
|
|
return if repository.branch_exists?(branch_name)
|
|
|
|
|
2017-01-06 10:29:13 -05:00
|
|
|
if repository.project != start_project
|
|
|
|
unless start_branch_name
|
2016-12-07 09:37:43 -05:00
|
|
|
raise ArgumentError,
|
2017-01-06 10:29:13 -05:00
|
|
|
'Should also pass :start_branch_name if' +
|
|
|
|
' :start_project is different from current project'
|
2016-12-07 09:37:43 -05:00
|
|
|
end
|
|
|
|
|
2017-01-06 10:29:13 -05:00
|
|
|
unless start_project.repository.branch_exists?(start_branch_name)
|
2016-12-13 14:03:20 -05:00
|
|
|
raise ArgumentError,
|
2016-12-07 09:37:43 -05:00
|
|
|
"Cannot find branch #{branch_name} nor" \
|
2017-01-06 10:29:13 -05:00
|
|
|
" #{start_branch_name} from" \
|
|
|
|
" #{start_project.path_with_namespace}"
|
2016-12-07 09:37:43 -05:00
|
|
|
end
|
2017-01-06 10:29:13 -05:00
|
|
|
elsif start_branch_name
|
|
|
|
unless repository.branch_exists?(start_branch_name)
|
2016-12-13 14:03:20 -05:00
|
|
|
raise ArgumentError,
|
2016-12-07 09:37:43 -05:00
|
|
|
"Cannot find branch #{branch_name} nor" \
|
2017-01-06 10:29:13 -05:00
|
|
|
" #{start_branch_name} from" \
|
2016-12-13 14:03:20 -05:00
|
|
|
" #{repository.project.path_with_namespace}"
|
2016-12-07 09:37:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-12-05 14:13:15 -05:00
|
|
|
end
|