Skip to content

Commit 501ce37

Browse files
committed
Fix of 'Commits being passed to custom hooks are already reachable when using the UI'
1 parent a27212a commit 501ce37

File tree

3 files changed

+43
-59
lines changed

3 files changed

+43
-59
lines changed

CHANGELOG

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Please view this file on the master branch, on stable branches it's out of date.
2+
v 8.11.0 (unreleased)
3+
- Fix of 'Commits being passed to custom hooks are already reachable when using the UI'
24

35
v 8.10.0 (unreleased)
46
- Fix profile activity heatmap to show correct day name (eanplatter)

app/models/repository.rb

+28-44
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ def commit_dir(user, path, message, branch)
704704
options[:commit] = {
705705
message: message,
706706
branch: ref,
707+
update_ref: false,
707708
}
708709

709710
raw_repository.mkdir(path, options)
@@ -719,6 +720,7 @@ def commit_file(user, path, content, message, branch, update)
719720
options[:commit] = {
720721
message: message,
721722
branch: ref,
723+
update_ref: false,
722724
}
723725

724726
options[:file] = {
@@ -739,7 +741,8 @@ def remove_file(user, path, message, branch)
739741
options[:author] = committer
740742
options[:commit] = {
741743
message: message,
742-
branch: ref
744+
branch: ref,
745+
update_ref: false,
743746
}
744747

745748
options[:file] = {
@@ -779,11 +782,10 @@ def merge(user, merge_request, options = {})
779782
merge_index = rugged.merge_commits(our_commit, their_commit)
780783
return false if merge_index.conflicts?
781784

782-
commit_with_hooks(user, merge_request.target_branch) do |tmp_ref|
785+
commit_with_hooks(user, merge_request.target_branch) do
783786
actual_options = options.merge(
784787
parents: [our_commit, their_commit],
785788
tree: merge_index.write_tree(rugged),
786-
update_ref: tmp_ref
787789
)
788790

789791
commit_id = Rugged::Commit.create(rugged, actual_options)
@@ -798,15 +800,14 @@ def revert(user, commit, base_branch, revert_tree_id = nil)
798800

799801
return false unless revert_tree_id
800802

801-
commit_with_hooks(user, base_branch) do |ref|
803+
commit_with_hooks(user, base_branch) do
802804
committer = user_to_committer(user)
803805
source_sha = Rugged::Commit.create(rugged,
804806
message: commit.revert_message,
805807
author: committer,
806808
committer: committer,
807809
tree: revert_tree_id,
808-
parents: [rugged.lookup(source_sha)],
809-
update_ref: ref)
810+
parents: [rugged.lookup(source_sha)])
810811
end
811812
end
812813

@@ -816,7 +817,7 @@ def cherry_pick(user, commit, base_branch, cherry_pick_tree_id = nil)
816817

817818
return false unless cherry_pick_tree_id
818819

819-
commit_with_hooks(user, base_branch) do |ref|
820+
commit_with_hooks(user, base_branch) do
820821
committer = user_to_committer(user)
821822
source_sha = Rugged::Commit.create(rugged,
822823
message: commit.message,
@@ -827,8 +828,7 @@ def cherry_pick(user, commit, base_branch, cherry_pick_tree_id = nil)
827828
},
828829
committer: committer,
829830
tree: cherry_pick_tree_id,
830-
parents: [rugged.lookup(source_sha)],
831-
update_ref: ref)
831+
parents: [rugged.lookup(source_sha)])
832832
end
833833
end
834834

@@ -929,20 +929,6 @@ def fetch_ref(source_path, source_ref, target_ref)
929929
Gitlab::Popen.popen(args, path_to_repo)
930930
end
931931

932-
def with_tmp_ref(oldrev = nil)
933-
random_string = SecureRandom.hex
934-
tmp_ref = "refs/tmp/#{random_string}/head"
935-
936-
if oldrev && !Gitlab::Git.blank_ref?(oldrev)
937-
rugged.references.create(tmp_ref, oldrev)
938-
end
939-
940-
# Make commit in tmp ref
941-
yield(tmp_ref)
942-
ensure
943-
rugged.references.delete(tmp_ref) rescue nil
944-
end
945-
946932
def commit_with_hooks(current_user, branch)
947933
update_autocrlf_option
948934

@@ -955,33 +941,31 @@ def commit_with_hooks(current_user, branch)
955941
oldrev = target_branch.target
956942
end
957943

958-
with_tmp_ref(oldrev) do |tmp_ref|
959-
# Make commit in tmp ref
960-
newrev = yield(tmp_ref)
944+
# Make commit
945+
newrev = yield(ref)
961946

962-
unless newrev
963-
raise CommitError.new('Failed to create commit')
964-
end
947+
unless newrev
948+
raise CommitError.new('Failed to create commit')
949+
end
950+
951+
GitHooksService.new.execute(current_user, path_to_repo, oldrev, newrev, ref) do
952+
if was_empty || !target_branch
953+
# Create branch
954+
rugged.references.create(ref, newrev)
955+
else
956+
# Update head
957+
current_head = find_branch(branch).target
965958

966-
GitHooksService.new.execute(current_user, path_to_repo, oldrev, newrev, ref) do
967-
if was_empty || !target_branch
968-
# Create branch
969-
rugged.references.create(ref, newrev)
959+
# Make sure target branch was not changed during pre-receive hook
960+
if current_head == oldrev
961+
rugged.references.update(ref, newrev)
970962
else
971-
# Update head
972-
current_head = find_branch(branch).target
973-
974-
# Make sure target branch was not changed during pre-receive hook
975-
if current_head == oldrev
976-
rugged.references.update(ref, newrev)
977-
else
978-
raise CommitError.new('Commit was rejected because branch received new push')
979-
end
963+
raise CommitError.new('Commit was rejected because branch received new push')
980964
end
981965
end
982-
983-
newrev
984966
end
967+
968+
newrev
985969
end
986970

987971
def ls_files(ref)

app/services/create_branch_service.rb

+13-15
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,19 @@ def execute(branch_name, ref, source_project: @project)
1515
return error('Branch already exists')
1616
end
1717

18-
new_branch = nil
19-
20-
if source_project != @project
21-
repository.with_tmp_ref do |tmp_ref|
22-
repository.fetch_ref(
23-
source_project.repository.path_to_repo,
24-
"refs/heads/#{ref}",
25-
tmp_ref
26-
)
27-
28-
new_branch = repository.add_branch(current_user, branch_name, tmp_ref)
29-
end
30-
else
31-
new_branch = repository.add_branch(current_user, branch_name, ref)
32-
end
18+
new_branch = if source_project != @project
19+
repository.fetch_ref(
20+
source_project.repository.path_to_repo,
21+
"refs/heads/#{ref}",
22+
"refs/heads/#{branch_name}"
23+
)
24+
25+
repository.after_create_branch
26+
27+
repository.find_branch(branch_name)
28+
else
29+
repository.add_branch(current_user, branch_name, ref)
30+
end
3331

3432
if new_branch
3533
success(new_branch)

0 commit comments

Comments
 (0)