Skip to content
Snippets Groups Projects
Commit d03c605b authored by Lin Jen-Shin's avatar Lin Jen-Shin
Browse files

Unify parameters and callback after hooks

parent 46d752ce
No related branches found
No related tags found
No related merge requests found
Loading
@@ -11,7 +11,7 @@ class GitOperationService
Loading
@@ -11,7 +11,7 @@ class GitOperationService
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
oldrev = Gitlab::Git::BLANK_SHA oldrev = Gitlab::Git::BLANK_SHA
   
with_hooks_and_update_ref(ref, oldrev, newrev) update_ref_in_hooks(ref, newrev, oldrev)
end end
   
def rm_branch(branch) def rm_branch(branch)
Loading
@@ -19,14 +19,14 @@ class GitOperationService
Loading
@@ -19,14 +19,14 @@ class GitOperationService
oldrev = branch.dereferenced_target.id oldrev = branch.dereferenced_target.id
newrev = Gitlab::Git::BLANK_SHA newrev = Gitlab::Git::BLANK_SHA
   
with_hooks_and_update_ref(ref, oldrev, newrev) update_ref_in_hooks(ref, newrev, oldrev)
end end
   
def add_tag(tag_name, newrev, options = {}) def add_tag(tag_name, newrev, options = {})
ref = Gitlab::Git::TAG_REF_PREFIX + tag_name ref = Gitlab::Git::TAG_REF_PREFIX + tag_name
oldrev = Gitlab::Git::BLANK_SHA oldrev = Gitlab::Git::BLANK_SHA
   
with_hooks(ref, oldrev, newrev) do |service| with_hooks(ref, newrev, oldrev) do |service|
raw_tag = repository.rugged.tags.create(tag_name, newrev, options) raw_tag = repository.rugged.tags.create(tag_name, newrev, options)
service.newrev = raw_tag.target_id service.newrev = raw_tag.target_id
end end
Loading
@@ -82,25 +82,23 @@ class GitOperationService
Loading
@@ -82,25 +82,23 @@ class GitOperationService
end end
   
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name ref = Gitlab::Git::BRANCH_REF_PREFIX + branch_name
with_hooks_and_update_ref(ref, oldrev, newrev) do update_ref_in_hooks(ref, newrev, oldrev)
# If repo was empty expire cache
repository.after_create if was_empty # If repo was empty expire cache
repository.after_create_branch if was_empty || repository.after_create if was_empty
Gitlab::Git.blank_ref?(oldrev) repository.after_create_branch if was_empty ||
end Gitlab::Git.blank_ref?(oldrev)
   
newrev newrev
end end
   
def with_hooks_and_update_ref(ref, oldrev, newrev) def update_ref_in_hooks(ref, newrev, oldrev)
with_hooks(ref, oldrev, newrev) do |service| with_hooks(ref, newrev, oldrev) do
update_ref!(ref, newrev, oldrev) update_ref(ref, newrev, oldrev)
yield(service) if block_given?
end end
end end
   
def with_hooks(ref, oldrev, newrev) def with_hooks(ref, newrev, oldrev)
result = nil result = nil
   
GitHooksService.new.execute( GitHooksService.new.execute(
Loading
@@ -116,7 +114,7 @@ class GitOperationService
Loading
@@ -116,7 +114,7 @@ class GitOperationService
result result
end end
   
def update_ref!(name, newrev, oldrev) def update_ref(ref, newrev, oldrev)
# We use 'git update-ref' because libgit2/rugged currently does not # We use 'git update-ref' because libgit2/rugged currently does not
# offer 'compare and swap' ref updates. Without compare-and-swap we can # offer 'compare and swap' ref updates. Without compare-and-swap we can
# (and have!) accidentally reset the ref to an earlier state, clobbering # (and have!) accidentally reset the ref to an earlier state, clobbering
Loading
@@ -125,12 +123,12 @@ class GitOperationService
Loading
@@ -125,12 +123,12 @@ class GitOperationService
_, status = Gitlab::Popen.popen( _, status = Gitlab::Popen.popen(
command, command,
repository.path_to_repo) do |stdin| repository.path_to_repo) do |stdin|
stdin.write("update #{name}\x00#{newrev}\x00#{oldrev}\x00") stdin.write("update #{ref}\x00#{newrev}\x00#{oldrev}\x00")
end end
   
unless status.zero? unless status.zero?
raise Repository::CommitError.new( raise Repository::CommitError.new(
"Could not update branch #{name.sub('refs/heads/', '')}." \ "Could not update branch #{Gitlab::Git.branch_name(ref)}." \
" Please refresh and try again.") " Please refresh and try again.")
end end
end end
Loading
Loading
Loading
@@ -6,7 +6,7 @@ module Gitlab
Loading
@@ -6,7 +6,7 @@ module Gitlab
   
class << self class << self
def ref_name(ref) def ref_name(ref)
ref.gsub(/\Arefs\/(tags|heads)\//, '') ref.sub(/\Arefs\/(tags|heads)\//, '')
end end
   
def branch_name(ref) def branch_name(ref)
Loading
Loading
Loading
@@ -829,7 +829,6 @@ describe Repository, models: true do
Loading
@@ -829,7 +829,6 @@ describe Repository, models: true do
context 'when target branch is different from source branch' do context 'when target branch is different from source branch' do
before do before do
allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, '']) allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([true, ''])
allow(repository).to receive(:update_ref!)
end end
   
it 'expires branch cache' do it 'expires branch cache' do
Loading
@@ -1474,16 +1473,16 @@ describe Repository, models: true do
Loading
@@ -1474,16 +1473,16 @@ describe Repository, models: true do
end end
end end
   
describe '#update_ref!' do describe '#update_ref' do
it 'can create a ref' do it 'can create a ref' do
GitOperationService.new(nil, repository).send(:update_ref!, 'refs/heads/foobar', 'refs/heads/master', Gitlab::Git::BLANK_SHA) GitOperationService.new(nil, repository).send(:update_ref, 'refs/heads/foobar', 'refs/heads/master', Gitlab::Git::BLANK_SHA)
   
expect(repository.find_branch('foobar')).not_to be_nil expect(repository.find_branch('foobar')).not_to be_nil
end end
   
it 'raises CommitError when the ref update fails' do it 'raises CommitError when the ref update fails' do
expect do expect do
GitOperationService.new(nil, repository).send(:update_ref!, 'refs/heads/master', 'refs/heads/master', Gitlab::Git::BLANK_SHA) GitOperationService.new(nil, repository).send(:update_ref, 'refs/heads/master', 'refs/heads/master', Gitlab::Git::BLANK_SHA)
end.to raise_error(Repository::CommitError) end.to raise_error(Repository::CommitError)
end end
end end
Loading
Loading
Loading
@@ -108,7 +108,7 @@ describe GitGarbageCollectWorker do
Loading
@@ -108,7 +108,7 @@ describe GitGarbageCollectWorker do
parents: [old_commit], parents: [old_commit],
) )
GitOperationService.new(nil, project.repository).send( GitOperationService.new(nil, project.repository).send(
:update_ref!, :update_ref,
"refs/heads/#{SecureRandom.hex(6)}", "refs/heads/#{SecureRandom.hex(6)}",
new_commit_sha, new_commit_sha,
Gitlab::Git::BLANK_SHA Gitlab::Git::BLANK_SHA
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment