Commit c772464b authored by Lin Jen-Shin's avatar Lin Jen-Shin

Introduce MergeRequest#write_ref and Repository#write_ref

so that we don't have to fetch it for non-forks
parent 64e13d19
...@@ -792,11 +792,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -792,11 +792,7 @@ class MergeRequest < ActiveRecord::Base
end end
def fetch_ref def fetch_ref
target_project.repository.fetch_ref( write_ref
source_project.repository.path_to_repo,
"refs/heads/#{source_branch}",
ref_path
)
update_column(:ref_fetched, true) update_column(:ref_fetched, true)
end end
...@@ -939,4 +935,18 @@ class MergeRequest < ActiveRecord::Base ...@@ -939,4 +935,18 @@ class MergeRequest < ActiveRecord::Base
true true
end end
private
def write_ref
if for_fork?
target_project.repository.fetch_ref(
source_project.repository.path_to_repo,
"refs/heads/#{source_branch}",
ref_path
)
else
source_project.repository.write_ref(ref_path, source_branch_sha)
end
end
end end
...@@ -1048,9 +1048,7 @@ class Project < ActiveRecord::Base ...@@ -1048,9 +1048,7 @@ class Project < ActiveRecord::Base
def change_head(branch) def change_head(branch)
if repository.branch_exists?(branch) if repository.branch_exists?(branch)
repository.before_change_head repository.before_change_head
repository.rugged.references.create('HEAD', repository.write_ref('HEAD', "refs/heads/#{branch}")
"refs/heads/#{branch}",
force: true)
repository.copy_gitattributes(branch) repository.copy_gitattributes(branch)
repository.after_change_head repository.after_change_head
reload_default_branch reload_default_branch
......
...@@ -224,7 +224,7 @@ class Repository ...@@ -224,7 +224,7 @@ class Repository
# This will still fail if the file is corrupted (e.g. 0 bytes) # This will still fail if the file is corrupted (e.g. 0 bytes)
begin begin
rugged.references.create(keep_around_ref_name(sha), sha, force: true) write_ref(keep_around_ref_name(sha), sha)
rescue Rugged::ReferenceError => ex rescue Rugged::ReferenceError => ex
Rails.logger.error "Unable to create keep-around reference for repository #{path}: #{ex}" Rails.logger.error "Unable to create keep-around reference for repository #{path}: #{ex}"
rescue Rugged::OSError => ex rescue Rugged::OSError => ex
...@@ -237,6 +237,10 @@ class Repository ...@@ -237,6 +237,10 @@ class Repository
ref_exists?(keep_around_ref_name(sha)) ref_exists?(keep_around_ref_name(sha))
end end
def write_ref(ref_path, sha)
rugged.references.create(ref_path, sha, force: true)
end
def diverging_commit_counts(branch) def diverging_commit_counts(branch)
root_ref_hash = raw_repository.rev_parse_target(root_ref).oid root_ref_hash = raw_repository.rev_parse_target(root_ref).oid
cache.fetch(:"diverging_commit_counts_#{branch.name}") do cache.fetch(:"diverging_commit_counts_#{branch.name}") do
......
...@@ -72,10 +72,10 @@ FactoryGirl.define do ...@@ -72,10 +72,10 @@ FactoryGirl.define do
target_project = merge_request.target_project target_project = merge_request.target_project
source_project = merge_request.source_project source_project = merge_request.source_project
# Fake `fetch_ref` if we don't have repository # Fake `write_ref` if we don't have repository
# We have too many existing tests replying on this behaviour # We have too many existing tests replying on this behaviour
unless [target_project, source_project].all?(&:repository_exists?) unless [target_project, source_project].all?(&:repository_exists?)
allow(target_project.repository).to receive(:fetch_ref) allow(merge_request).to receive(:write_ref)
end end
end end
......
...@@ -1196,8 +1196,9 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1196,8 +1196,9 @@ describe Gitlab::Git::Repository, seed_helper: true do
def create_remote_branch(repository, remote_name, branch_name, source_branch_name) def create_remote_branch(repository, remote_name, branch_name, source_branch_name)
source_branch = repository.branches.find { |branch| branch.name == source_branch_name } source_branch = repository.branches.find { |branch| branch.name == source_branch_name }
rugged = repository.rugged repository.write_ref(
rugged.references.create("refs/remotes/#{remote_name}/#{branch_name}", source_branch.dereferenced_target.sha) "refs/remotes/#{remote_name}/#{branch_name}",
source_branch.dereferenced_target.sha)
end end
# Build the options hash that's passed to Rugged::Commit#create # Build the options hash that's passed to Rugged::Commit#create
......
...@@ -315,15 +315,17 @@ describe API::MergeRequests do ...@@ -315,15 +315,17 @@ describe API::MergeRequests do
let!(:fork_project) { create(:project, forked_from_project: project, namespace: user2.namespace, creator_id: user2.id) } let!(:fork_project) { create(:project, forked_from_project: project, namespace: user2.namespace, creator_id: user2.id) }
let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) } let!(:unrelated_project) { create(:project, namespace: create(:user).namespace, creator_id: user2.id) }
before :each do |each| before do
fork_project.team << [user2, :reporter] fork_project.add_reporter(user2)
allow_any_instance_of(MergeRequest).to receive(:write_ref)
end end
it "returns merge_request" do it "returns merge_request" do
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
title: 'Test merge_request', source_branch: "feature_conflict", target_branch: "master", title: 'Test merge_request', source_branch: "feature_conflict", target_branch: "master",
author: user2, target_project_id: project.id, description: 'Test description for Test merge_request' author: user2, target_project_id: project.id, description: 'Test description for Test merge_request'
expect(response).to have_http_status(201) expect(response).to have_gitlab_http_status(201)
expect(json_response['title']).to eq('Test merge_request') expect(json_response['title']).to eq('Test merge_request')
expect(json_response['description']).to eq('Test description for Test merge_request') expect(json_response['description']).to eq('Test description for Test merge_request')
end end
...@@ -334,7 +336,7 @@ describe API::MergeRequests do ...@@ -334,7 +336,7 @@ describe API::MergeRequests do
expect(fork_project.forked_from_project).to eq(project) expect(fork_project.forked_from_project).to eq(project)
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id title: 'Test merge_request', source_branch: "master", target_branch: "master", author: user2, target_project_id: project.id
expect(response).to have_http_status(201) expect(response).to have_gitlab_http_status(201)
expect(json_response['title']).to eq('Test merge_request') expect(json_response['title']).to eq('Test merge_request')
end end
...@@ -348,25 +350,25 @@ describe API::MergeRequests do ...@@ -348,25 +350,25 @@ describe API::MergeRequests do
author: user2, author: user2,
target_project_id: project.id target_project_id: project.id
expect(response).to have_http_status(422) expect(response).to have_gitlab_http_status(422)
end end
it "returns 400 when source_branch is missing" do it "returns 400 when source_branch is missing" do
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
expect(response).to have_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
it "returns 400 when target_branch is missing" do it "returns 400 when target_branch is missing" do
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id title: 'Test merge_request', target_branch: "master", author: user2, target_project_id: project.id
expect(response).to have_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
it "returns 400 when title is missing" do it "returns 400 when title is missing" do
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: project.id target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: project.id
expect(response).to have_http_status(400) expect(response).to have_gitlab_http_status(400)
end end
context 'when target_branch is specified' do context 'when target_branch is specified' do
...@@ -377,7 +379,7 @@ describe API::MergeRequests do ...@@ -377,7 +379,7 @@ describe API::MergeRequests do
source_branch: 'markdown', source_branch: 'markdown',
author: user, author: user,
target_project_id: fork_project.id target_project_id: fork_project.id
expect(response).to have_http_status(422) expect(response).to have_gitlab_http_status(422)
end end
it 'returns 422 if targeting a different fork' do it 'returns 422 if targeting a different fork' do
...@@ -387,14 +389,14 @@ describe API::MergeRequests do ...@@ -387,14 +389,14 @@ describe API::MergeRequests do
source_branch: 'markdown', source_branch: 'markdown',
author: user2, author: user2,
target_project_id: unrelated_project.id target_project_id: unrelated_project.id
expect(response).to have_http_status(422) expect(response).to have_gitlab_http_status(422)
end end
end end
it "returns 201 when target_branch is specified and for the same project" do it "returns 201 when target_branch is specified and for the same project" do
post v3_api("/projects/#{fork_project.id}/merge_requests", user2), post v3_api("/projects/#{fork_project.id}/merge_requests", user2),
title: 'Test merge_request', target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: fork_project.id title: 'Test merge_request', target_branch: 'master', source_branch: 'markdown', author: user2, target_project_id: fork_project.id
expect(response).to have_http_status(201) expect(response).to have_gitlab_http_status(201)
end end
end end
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