Commit b0e2977c authored by Patrick Steinhardt's avatar Patrick Steinhardt

Rename functions to set repository's full path

In order to make it easy for an admin to see which project a given
repository belongs to with hashed storages, we always write the
project path (e.g. "gitlab-com/gitlab") into the repo's gitconfig. This
is done via `#write_repository_config()` and `#write_config()`, both of
which only support writing this specific config entry.

Rename both functions to make it clear that they only support writing
the path. Direct access to the gitconfig is going away soon anyway, so
these functions can never be extended to write additional config
entries.
parent 710cde96
......@@ -528,7 +528,7 @@ class Namespace < ApplicationRecord
def write_projects_repository_config
all_projects.find_each do |project|
project.write_repository_config
project.set_full_path
project.track_project_repository
end
end
......
......@@ -1889,11 +1889,11 @@ class Project < ApplicationRecord
.update_all(deployed: deployment.present?, pages_deployment_id: deployment&.id)
end
def write_repository_config(gl_full_path: full_path)
def set_full_path(gl_full_path: full_path)
# We'd need to keep track of project full path otherwise directory tree
# created with hashed storage enabled cannot be usefully imported using
# the import rake task.
repository.raw_repository.write_config(full_path: gl_full_path)
repository.raw_repository.set_full_path(full_path: gl_full_path)
rescue Gitlab::Git::Repository::NoRepository => e
Gitlab::AppLogger.error("Error writing to .git/config for project #{full_path} (#{id}): #{e.message}.")
nil
......@@ -1917,7 +1917,7 @@ class Project < ApplicationRecord
after_create_default_branch
join_pool_repository
refresh_markdown_cache!
write_repository_config
set_full_path
end
def update_project_counter_caches
......
......@@ -83,7 +83,7 @@ module Projects
def update_repository_configuration
project.reload_repository!
project.write_repository_config
project.set_full_path
project.track_project_repository
end
......
......@@ -92,7 +92,7 @@ module Projects
# Skip writing the config for project imports/forks because it
# will always fail since the Git directory doesn't exist until
# a background job creates it (see Project#add_import_job).
@project.write_repository_config unless @project.import?
@project.set_full_path unless @project.import?
unless @project.gitlab_project_import?
@project.create_wiki unless skip_wiki?
......
......@@ -14,7 +14,7 @@ module Projects
result = move_repositories
if result
project.write_repository_config
project.set_full_path
project.track_project_repository
else
rollback_folder_move
......
......@@ -14,7 +14,7 @@ module Projects
result = move_repositories
if result
project.write_repository_config
project.set_full_path
project.track_project_repository
else
rollback_folder_move
......
......@@ -135,7 +135,7 @@ module Projects
end
def update_repository_configuration(full_path)
project.write_repository_config(gl_full_path: full_path)
project.set_full_path(gl_full_path: full_path)
project.track_project_repository
end
......
......@@ -142,7 +142,7 @@ to do so. In a Rails console session, run the following to migrate a project:
```ruby
project = Project.find_by_full_path('gitlab-org/gitlab')
project.write_repository_config
project.set_full_path
```
In a Rails console session, run the following to migrate all of a namespace's
......
......@@ -73,7 +73,7 @@ module Gitlab
if project.persisted? && mv_repositories(project)
log " * Created #{project.name} (#{project_full_path})".color(:green)
project.write_repository_config
project.set_full_path
ProjectCacheWorker.perform_async(project.id)
else
......
......@@ -905,7 +905,7 @@ module Gitlab
end
# rubocop:enable Metrics/ParameterLists
def write_config(full_path:)
def set_full_path(full_path:)
return unless full_path.present?
# This guard avoids Gitaly log/error spam
......
......@@ -1729,14 +1729,14 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do
end
end
describe '#write_config' do
describe '#set_full_path' do
before do
repository_rugged.config["gitlab.fullpath"] = repository_path
end
context 'is given a path' do
it 'writes it to disk' do
repository.write_config(full_path: "not-the/real-path.git")
repository.set_full_path(full_path: "not-the/real-path.git")
config = File.read(File.join(repository_path, "config"))
......@@ -1747,7 +1747,7 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do
context 'it is given an empty path' do
it 'does not write it to disk' do
repository.write_config(full_path: "")
repository.set_full_path(full_path: "")
config = File.read(File.join(repository_path, "config"))
......@@ -1760,10 +1760,10 @@ RSpec.describe Gitlab::Git::Repository, :seed_helper do
it 'raises NoRepository and does not call Gitaly WriteConfig' do
repository = Gitlab::Git::Repository.new('default', 'does/not/exist.git', '', 'group/project')
expect(repository.gitaly_repository_client).not_to receive(:write_config)
expect(repository.gitaly_repository_client).not_to receive(:set_full_path)
expect do
repository.write_config(full_path: 'foo/bar.git')
repository.set_full_path(full_path: 'foo/bar.git')
end.to raise_error(Gitlab::Git::Repository::NoRepository)
end
end
......
......@@ -5229,7 +5229,7 @@ RSpec.describe Project, factory_default: :keep do
expect(InternalId).to receive(:flush_records!).with(project: project)
expect(ProjectCacheWorker).to receive(:perform_async).with(project.id, [], [:repository_size])
expect(DetectRepositoryLanguagesWorker).to receive(:perform_async).with(project.id)
expect(project).to receive(:write_repository_config)
expect(project).to receive(:set_full_path)
project.after_import
end
......@@ -5298,25 +5298,25 @@ RSpec.describe Project, factory_default: :keep do
end
end
describe '#write_repository_config' do
describe '#set_full_path' do
let_it_be(:project) { create(:project, :repository) }
it 'writes full path in .git/config when key is missing' do
project.write_repository_config
project.set_full_path
expect(rugged_config['gitlab.fullpath']).to eq project.full_path
end
it 'updates full path in .git/config when key is present' do
project.write_repository_config(gl_full_path: 'old/path')
project.set_full_path(gl_full_path: 'old/path')
expect { project.write_repository_config }.to change { rugged_config['gitlab.fullpath'] }.from('old/path').to(project.full_path)
expect { project.set_full_path }.to change { rugged_config['gitlab.fullpath'] }.from('old/path').to(project.full_path)
end
it 'does not raise an error with an empty repository' do
project = create(:project_empty_repo)
expect { project.write_repository_config }.not_to raise_error
expect { project.set_full_path }.not_to raise_error
end
end
......
......@@ -335,7 +335,7 @@ RSpec.describe Projects::CreateService, '#execute' do
it 'does not write repository config' do
expect_next_instance_of(Project) do |project|
expect(project).not_to receive(:write_repository_config)
expect(project).not_to receive(:set_full_path)
end
imported_project
......
......@@ -96,13 +96,13 @@ RSpec.describe Projects::HashedStorage::MigrateRepositoryService do
end
it 'handles Gitlab::Git::CommandError' do
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
expect(project).to receive(:set_full_path).and_raise(Gitlab::Git::CommandError)
expect { service.execute }.not_to raise_exception
end
it 'ensures rollback when Gitlab::Git::CommandError' do
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
expect(project).to receive(:set_full_path).and_raise(Gitlab::Git::CommandError)
expect(service).to receive(:rollback_folder_move).and_call_original
service.execute
......
......@@ -96,13 +96,13 @@ RSpec.describe Projects::HashedStorage::RollbackRepositoryService, :clean_gitlab
end
it 'handles Gitlab::Git::CommandError' do
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
expect(project).to receive(:set_full_path).and_raise(Gitlab::Git::CommandError)
expect { service.execute }.not_to raise_exception
end
it 'ensures rollback when Gitlab::Git::CommandError' do
expect(project).to receive(:write_repository_config).and_raise(Gitlab::Git::CommandError)
expect(project).to receive(:set_full_path).and_raise(Gitlab::Git::CommandError)
expect(service).to receive(:rollback_folder_move).and_call_original
service.execute
......
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