Commit e46a3d2f authored by Sean McGivern's avatar Sean McGivern

Merge branch 'git-operation-user' into 'master'

Move GitHooksService into Gitlab::Git

See merge request !13739
parents 88892e43 a1a91499
...@@ -1192,7 +1192,7 @@ class Repository ...@@ -1192,7 +1192,7 @@ class Repository
end end
def initialize_raw_repository def initialize_raw_repository
Gitlab::Git::Repository.new(project.repository_storage, disk_path + '.git') Gitlab::Git::Repository.new(project.repository_storage, disk_path + '.git', Gitlab::GlRepository.gl_repository(project, false))
end end
def circuit_breaker def circuit_breaker
......
...@@ -17,7 +17,7 @@ module Commits ...@@ -17,7 +17,7 @@ module Commits
new_commit = create_commit! new_commit = create_commit!
success(result: new_commit) success(result: new_commit)
rescue ValidationError, ChangeError, Gitlab::Git::Index::IndexError, Repository::CommitError, GitHooksService::PreReceiveError => ex rescue ValidationError, ChangeError, Gitlab::Git::Index::IndexError, Repository::CommitError, Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
...@@ -14,7 +14,7 @@ class CreateBranchService < BaseService ...@@ -14,7 +14,7 @@ class CreateBranchService < BaseService
else else
error('Invalid reference name') error('Invalid reference name')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
...@@ -16,7 +16,7 @@ class DeleteBranchService < BaseService ...@@ -16,7 +16,7 @@ class DeleteBranchService < BaseService
else else
error('Failed to remove branch') error('Failed to remove branch')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
class GitHooksService
PreReceiveError = Class.new(StandardError)
attr_accessor :oldrev, :newrev, :ref
def execute(user, project, oldrev, newrev, ref)
@project = project
@user = Gitlab::GlId.gl_id(user)
@oldrev = oldrev
@newrev = newrev
@ref = ref
%w(pre-receive update).each do |hook_name|
status, message = run_hook(hook_name)
unless status
raise PreReceiveError, message
end
end
yield(self).tap do
run_hook('post-receive')
end
end
private
def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @project)
hook.trigger(@user, oldrev, newrev, ref)
end
end
class GitOperationService class GitOperationService
attr_reader :user, :repository attr_reader :committer, :repository
def initialize(committer, new_repository)
committer = Gitlab::Git::Committer.from_user(committer) if committer.is_a?(User)
@committer = committer
def initialize(new_user, new_repository)
@user = new_user
@repository = new_repository @repository = new_repository
end end
...@@ -118,9 +120,9 @@ class GitOperationService ...@@ -118,9 +120,9 @@ class GitOperationService
end end
def with_hooks(ref, newrev, oldrev) def with_hooks(ref, newrev, oldrev)
GitHooksService.new.execute( Gitlab::Git::HooksService.new.execute(
user, committer,
repository.project, repository,
oldrev, oldrev,
newrev, newrev,
ref) do |service| ref) do |service|
......
...@@ -49,7 +49,7 @@ module MergeRequests ...@@ -49,7 +49,7 @@ module MergeRequests
raise MergeError, 'Conflicts detected during merge' unless commit_id raise MergeError, 'Conflicts detected during merge' unless commit_id
merge_request.update(merge_commit_sha: commit_id) merge_request.update(merge_commit_sha: commit_id)
rescue GitHooksService::PreReceiveError => e rescue Gitlab::Git::HooksService::PreReceiveError => e
raise MergeError, e.message raise MergeError, e.message
rescue StandardError => e rescue StandardError => e
raise MergeError, "Something went wrong during merge: #{e.message}" raise MergeError, "Something went wrong during merge: #{e.message}"
......
...@@ -13,7 +13,7 @@ module Tags ...@@ -13,7 +13,7 @@ module Tags
new_tag = repository.add_tag(current_user, tag_name, target, message) new_tag = repository.add_tag(current_user, tag_name, target, message)
rescue Rugged::TagError rescue Rugged::TagError
return error("Tag #{tag_name} already exists") return error("Tag #{tag_name} already exists")
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
return error(ex.message) return error(ex.message)
end end
......
...@@ -21,7 +21,7 @@ module Tags ...@@ -21,7 +21,7 @@ module Tags
else else
error('Failed to remove tag') error('Failed to remove tag')
end end
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
......
...@@ -13,7 +13,7 @@ class ValidateNewBranchService < BaseService ...@@ -13,7 +13,7 @@ class ValidateNewBranchService < BaseService
end end
success success
rescue GitHooksService::PreReceiveError => ex rescue Gitlab::Git::HooksService::PreReceiveError => ex
error(ex.message) error(ex.message)
end end
end end
...@@ -15,7 +15,7 @@ class Gitlab::Seeder::CycleAnalytics ...@@ -15,7 +15,7 @@ class Gitlab::Seeder::CycleAnalytics
# to disable the `pre_receive` hook in order to remove this # to disable the `pre_receive` hook in order to remove this
# dependency on the GitLab API. # dependency on the GitLab API.
def stub_git_pre_receive! def stub_git_pre_receive!
GitHooksService.class_eval do Gitlab::Git::HooksService.class_eval do
def run_hook(name) def run_hook(name)
[true, ''] [true, '']
end end
......
...@@ -11,7 +11,7 @@ class MigrateRepoSize < ActiveRecord::Migration ...@@ -11,7 +11,7 @@ class MigrateRepoSize < ActiveRecord::Migration
path = File.join(namespace_path, project['project_path'] + '.git') path = File.join(namespace_path, project['project_path'] + '.git')
begin begin
repo = Gitlab::Git::Repository.new('default', path) repo = Gitlab::Git::Repository.new('default', path, '')
if repo.empty? if repo.empty?
print '-' print '-'
else else
......
module Gitlab
module Git
class Committer
attr_reader :name, :email, :gl_id
def self.from_user(user)
new(user.name, user.email, Gitlab::GlId.gl_id(user))
end
def initialize(name, email, gl_id)
@name = name
@email = email
@gl_id = gl_id
end
def ==(other)
[name, email, gl_id] == [other.name, other.email, other.gl_id]
end
end
end
end
# Gitaly note: JV: looks like this is only used by GitHooksService in # Gitaly note: JV: looks like this is only used by Gitlab::Git::HooksService in
# app/services. We shouldn't bother migrating this until we know how # app/services. We shouldn't bother migrating this until we know how
# GitHooksService will be migrated. # Gitlab::Git::HooksService will be migrated.
module Gitlab module Gitlab
module Git module Git
class Hook class Hook
GL_PROTOCOL = 'web'.freeze GL_PROTOCOL = 'web'.freeze
attr_reader :name, :repo_path, :path attr_reader :name, :path, :repository
def initialize(name, project) def initialize(name, repository)
@name = name @name = name
@project = project @repository = repository
@repo_path = project.repository.path
@path = File.join(repo_path.strip, 'hooks', name) @path = File.join(repo_path.strip, 'hooks', name)
end end
def repo_path
repository.path
end
def exists? def exists?
File.exist?(path) File.exist?(path)
end end
...@@ -44,7 +47,7 @@ module Gitlab ...@@ -44,7 +47,7 @@ module Gitlab
'GL_ID' => gl_id, 'GL_ID' => gl_id,
'PWD' => repo_path, 'PWD' => repo_path,
'GL_PROTOCOL' => GL_PROTOCOL, 'GL_PROTOCOL' => GL_PROTOCOL,
'GL_REPOSITORY' => Gitlab::GlRepository.gl_repository(@project, false) 'GL_REPOSITORY' => repository.gl_repository
} }
options = { options = {
......
module Gitlab
module Git
class HooksService
PreReceiveError = Class.new(StandardError)
attr_accessor :oldrev, :newrev, :ref
def execute(committer, repository, oldrev, newrev, ref)
@repository = repository
@gl_id = committer.gl_id
@oldrev = oldrev
@newrev = newrev
@ref = ref
%w(pre-receive update).each do |hook_name|
status, message = run_hook(hook_name)
unless status
raise PreReceiveError, message
end
end
yield(self).tap do
run_hook('post-receive')
end
end
private
def run_hook(name)
hook = Gitlab::Git::Hook.new(name, @repository)
hook.trigger(@gl_id, oldrev, newrev, ref)
end
end
end
end
...@@ -49,13 +49,14 @@ module Gitlab ...@@ -49,13 +49,14 @@ module Gitlab
# Rugged repo object # Rugged repo object
attr_reader :rugged attr_reader :rugged
attr_reader :storage attr_reader :storage, :gl_repository, :relative_path
# 'path' must be the path to a _bare_ git repository, e.g. # 'path' must be the path to a _bare_ git repository, e.g.
# /path/to/my-repo.git # /path/to/my-repo.git
def initialize(storage, relative_path) def initialize(storage, relative_path, gl_repository)
@storage = storage @storage = storage
@relative_path = relative_path @relative_path = relative_path
@gl_repository = gl_repository
storage_path = Gitlab.config.repositories.storages[@storage]['path'] storage_path = Gitlab.config.repositories.storages[@storage]['path']
@path = File.join(storage_path, @relative_path) @path = File.join(storage_path, @relative_path)
......
...@@ -99,6 +99,6 @@ feature 'Import/Export - project import integration test', js: true do ...@@ -99,6 +99,6 @@ feature 'Import/Export - project import integration test', js: true do
end end
def project_hook_exists?(project) def project_hook_exists?(project)
Gitlab::Git::Hook.new('post-receive', project).exists? Gitlab::Git::Hook.new('post-receive', project.repository.raw_repository).exists?
end end
end end
...@@ -36,8 +36,8 @@ feature 'Master deletes tag' do ...@@ -36,8 +36,8 @@ feature 'Master deletes tag' do
context 'when pre-receive hook fails', js: true do context 'when pre-receive hook fails', js: true do
before do before do
allow_any_instance_of(GitHooksService).to receive(:execute) allow_any_instance_of(Gitlab::Git::HooksService).to receive(:execute)
.and_raise(GitHooksService::PreReceiveError, 'Do not delete tags') .and_raise(Gitlab::Git::HooksService::PreReceiveError, 'Do not delete tags')
end end
scenario 'shows the error message' do scenario 'shows the error message' do
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Blame, seed_helper: true do describe Gitlab::Git::Blame, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
let(:blame) do let(:blame) do
Gitlab::Git::Blame.new(repository, SeedRepo::Commit::ID, "CONTRIBUTING.md") Gitlab::Git::Blame.new(repository, SeedRepo::Commit::ID, "CONTRIBUTING.md")
end end
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Blob, seed_helper: true do describe Gitlab::Git::Blob, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
describe 'initialize' do describe 'initialize' do
let(:blob) { Gitlab::Git::Blob.new(name: 'test') } let(:blob) { Gitlab::Git::Blob.new(name: 'test') }
......
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Branch, seed_helper: true do describe Gitlab::Git::Branch, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
subject { repository.branches } subject { repository.branches }
......
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Commit, seed_helper: true do describe Gitlab::Git::Commit, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
let(:commit) { described_class.find(repository, SeedRepo::Commit::ID) } let(:commit) { described_class.find(repository, SeedRepo::Commit::ID) }
let(:rugged_commit) do let(:rugged_commit) do
repository.rugged.lookup(SeedRepo::Commit::ID) repository.rugged.lookup(SeedRepo::Commit::ID)
...@@ -9,7 +9,7 @@ describe Gitlab::Git::Commit, seed_helper: true do ...@@ -9,7 +9,7 @@ describe Gitlab::Git::Commit, seed_helper: true do
describe "Commit info" do describe "Commit info" do
before do before do
repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '').rugged
@committer = { @committer = {
email: 'mike@smith.com', email: 'mike@smith.com',
...@@ -59,7 +59,7 @@ describe Gitlab::Git::Commit, seed_helper: true do ...@@ -59,7 +59,7 @@ describe Gitlab::Git::Commit, seed_helper: true do
after do after do
# Erase the new commit so other tests get the original repo # Erase the new commit so other tests get the original repo
repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '').rugged
repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID) repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID)
end end
end end
...@@ -144,7 +144,7 @@ describe Gitlab::Git::Commit, seed_helper: true do ...@@ -144,7 +144,7 @@ describe Gitlab::Git::Commit, seed_helper: true do
end end
context 'with broken repo' do context 'with broken repo' do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_BROKEN_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_BROKEN_REPO_PATH, '') }
it 'returns nil' do it 'returns nil' do
expect(described_class.find(repository, SeedRepo::Commit::ID)).to be_nil expect(described_class.find(repository, SeedRepo::Commit::ID)).to be_nil
......
require 'spec_helper'
describe Gitlab::Git::Committer do
let(:name) { 'Jane Doe' }
let(:email) { 'janedoe@example.com' }
let(:gl_id) { 'user-123' }
subject { described_class.new(name, email, gl_id) }
describe '#==' do
def eq_other(name, email, gl_id)
eq(described_class.new(name, email, gl_id))
end
it { expect(subject).to eq_other(name, email, gl_id) }
it { expect(subject).not_to eq_other(nil, nil, nil) }
it { expect(subject).not_to eq_other(name + 'x', email, gl_id) }
it { expect(subject).not_to eq_other(name, email + 'x', gl_id) }
it { expect(subject).not_to eq_other(name, email, gl_id + 'x') }
end
end
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Compare, seed_helper: true do describe Gitlab::Git::Compare, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: false) } let(:compare) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: false) }
let(:compare_straight) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: true) } let(:compare_straight) { Gitlab::Git::Compare.new(repository, SeedRepo::BigCommit::ID, SeedRepo::Commit::ID, straight: true) }
......
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Diff, seed_helper: true do describe Gitlab::Git::Diff, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
before do before do
@raw_diff_hash = { @raw_diff_hash = {
......
...@@ -10,7 +10,8 @@ describe Gitlab::Git::Hook do ...@@ -10,7 +10,8 @@ describe Gitlab::Git::Hook do
describe "#trigger" do describe "#trigger" do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:repo_path) { project.repository.path } let(:repository) { project.repository.raw_repository }
let(:repo_path) { repository.path }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:gl_id) { Gitlab::GlId.gl_id(user) } let(:gl_id) { Gitlab::GlId.gl_id(user) }
...@@ -48,7 +49,7 @@ describe Gitlab::Git::Hook do ...@@ -48,7 +49,7 @@ describe Gitlab::Git::Hook do
it "returns success with no errors" do it "returns success with no errors" do
create_hook(hook_name) create_hook(hook_name)
hook = described_class.new(hook_name, project) hook = described_class.new(hook_name, repository)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
...@@ -66,7 +67,7 @@ describe Gitlab::Git::Hook do ...@@ -66,7 +67,7 @@ describe Gitlab::Git::Hook do
context "when the hook is unsuccessful" do context "when the hook is unsuccessful" do
it "returns failure with errors" do it "returns failure with errors" do
create_failing_hook(hook_name) create_failing_hook(hook_name)
hook = described_class.new(hook_name, project) hook = described_class.new(hook_name, repository)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
...@@ -80,7 +81,7 @@ describe Gitlab::Git::Hook do ...@@ -80,7 +81,7 @@ describe Gitlab::Git::Hook do
context "when the hook doesn't exist" do context "when the hook doesn't exist" do
it "returns success with no errors" do it "returns success with no errors" do
hook = described_class.new('unknown_hook', project) hook = described_class.new('unknown_hook', repository)
blank = Gitlab::Git::BLANK_SHA blank = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch' ref = Gitlab::Git::BRANCH_REF_PREFIX + 'new_branch'
......
require 'spec_helper' require 'spec_helper'
describe GitHooksService do describe Gitlab::Git::HooksService, seed_helper: true do
include RepoHelpers let(:committer) { Gitlab::Git::Committer.new('Jane Doe', 'janedoe@example.com', 'user-456') }
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, 'project-123') }
let(:user) { create(:user) }
let(:project) { create(:project, :repository) }
let(:service) { described_class.new } let(:service) { described_class.new }
before do before do
@blankrev = Gitlab::Git::BLANK_SHA @blankrev = Gitlab::Git::BLANK_SHA
@oldrev = sample_commit.parent_id @oldrev = SeedRepo::Commit::PARENT_ID
@newrev = sample_commit.id @newrev = SeedRepo::Commit::ID
@ref = 'refs/heads/feature' @ref = 'refs/heads/feature'
end end
...@@ -20,7 +18,7 @@ describe GitHooksService do ...@@ -20,7 +18,7 @@ describe GitHooksService do
hook = double(trigger: [true, nil]) hook = double(trigger: [true, nil])
expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook) expect(Gitlab::Git::Hook).to receive(:new).exactly(3).times.and_return(hook)
service.execute(user, project, @blankrev, @newrev, @ref) { } service.execute(committer, repository, @blankrev, @newrev, @ref) { }
end end
end end
...@@ -30,8 +28,8 @@ describe GitHooksService do ...@@ -30,8 +28,8 @@ describe GitHooksService do
expect(service).not_to receive(:run_hook).with('post-receive') expect(service).not_to receive(:run_hook).with('post-receive')
expect do expect do
service.execute(user, project, @blankrev, @newrev, @ref) service.execute(committer, repository, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
...@@ -42,8 +40,8 @@ describe GitHooksService do ...@@ -42,8 +40,8 @@ describe GitHooksService do
expect(service).not_to receive(:run_hook).with('post-receive') expect(service).not_to receive(:run_hook).with('post-receive')
expect do expect do
service.execute(user, project, @blankrev, @newrev, @ref) service.execute(committer, repository, @blankrev, @newrev, @ref)
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Git::Index, seed_helper: true do describe Gitlab::Git::Index, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
let(:index) { described_class.new(repository) } let(:index) { described_class.new(repository) }
before do before do
......
...@@ -17,7 +17,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -17,7 +17,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
end end
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
describe "Respond to" do describe "Respond to" do
subject { repository } subject { repository }
...@@ -56,14 +56,14 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -56,14 +56,14 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#rugged" do describe "#rugged" do
describe 'when storage is broken', broken_storage: true do describe 'when storage is broken', broken_storage: true do
it 'raises a storage exception when storage is not available' do it 'raises a storage exception when storage is not available' do
broken_repo = described_class.new('broken', 'a/path.git') broken_repo = described_class.new('broken', 'a/path.git', '')
expect { broken_repo.rugged }.to raise_error(Gitlab::Git::Storage::Inaccessible) expect { broken_repo.rugged }.to raise_error(Gitlab::Git::Storage::Inaccessible)
end end
end end
it 'raises a no repository exception when there is no repo' do it 'raises a no repository exception when there is no repo' do
broken_repo = described_class.new('default', 'a/path.git') broken_repo = described_class.new('default', 'a/path.git', '')
expect { broken_repo.rugged }.to raise_error(Gitlab::Git::Repository::NoRepository) expect { broken_repo.rugged }.to raise_error(Gitlab::Git::Repository::NoRepository)
end end
...@@ -257,7 +257,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -257,7 +257,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
describe '#submodule_url_for' do describe '#submodule_url_for' do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
let(:ref) { 'master' } let(:ref) { 'master' }
def submodule_url(path) def submodule_url(path)
...@@ -295,7 +295,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -295,7 +295,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
end end
context '#submodules' do context '#submodules' do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
context 'where repo has submodules' do context 'where repo has submodules' do
let(:submodules) { repository.send(:submodules, 'master') } let(:submodules) { repository.send(:submodules, 'master') }
...@@ -391,7 +391,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -391,7 +391,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#delete_branch" do describe "#delete_branch" do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.delete_branch("feature") @repo.delete_branch("feature")
end end
...@@ -407,7 +407,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -407,7 +407,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#create_branch" do describe "#create_branch" do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
end end
it "should create a new branch" do it "should create a new branch" do
...@@ -445,7 +445,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -445,7 +445,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#remote_delete" do describe "#remote_delete" do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.remote_delete("expendable") @repo.remote_delete("expendable")
end end
...@@ -461,7 +461,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -461,7 +461,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#remote_add" do describe "#remote_add" do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.remote_add("new_remote", SeedHelper::GITLAB_GIT_TEST_REPO_URL) @repo.remote_add("new_remote", SeedHelper::GITLAB_GIT_TEST_REPO_URL)
end end
...@@ -477,7 +477,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -477,7 +477,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe "#remote_update" do describe "#remote_update" do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.remote_update("expendable", url: TEST_NORMAL_REPO_PATH) @repo.remote_update("expendable", url: TEST_NORMAL_REPO_PATH)
end end
...@@ -506,7 +506,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -506,7 +506,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
before(:context) do before(:context) do
# Add new commits so that there's a renamed file in the commit history # Add new commits so that there's a renamed file in the commit history
repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '').rugged
@commit_with_old_name_id = new_commit_edit_old_file(repo) @commit_with_old_name_id = new_commit_edit_old_file(repo)
@rename_commit_id = new_commit_move_file(repo) @rename_commit_id = new_commit_move_file(repo)
@commit_with_new_name_id = new_commit_edit_new_file(repo) @commit_with_new_name_id = new_commit_edit_new_file(repo)
...@@ -514,7 +514,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -514,7 +514,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
after(:context) do after(:context) do
# Erase our commits so other tests get the original repo # Erase our commits so other tests get the original repo
repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH).rugged repo = Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '').rugged
repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID) repo.references.update("refs/heads/master", SeedRepo::LastCommit::ID)
end end
...@@ -849,7 +849,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -849,7 +849,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe '#autocrlf' do describe '#autocrlf' do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.rugged.config['core.autocrlf'] = true @repo.rugged.config['core.autocrlf'] = true
end end
...@@ -864,7 +864,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -864,7 +864,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe '#autocrlf=' do describe '#autocrlf=' do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH) @repo = Gitlab::Git::Repository.new('default', TEST_MUTABLE_REPO_PATH, '')
@repo.rugged.config['core.autocrlf'] = false @repo.rugged.config['core.autocrlf'] = false
end end
...@@ -933,7 +933,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -933,7 +933,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
context 'with local and remote branches' do context 'with local and remote branches' do
let(:repository) do let(:repository) do
Gitlab::Git::Repository.new('default', File.join(TEST_MUTABLE_REPO_PATH, '.git')) Gitlab::Git::Repository.new('default', File.join(TEST_MUTABLE_REPO_PATH, '.git'), '')
end end
before do before do
...@@ -1186,7 +1186,7 @@ describe Gitlab::Git::Repository, seed_helper: true do ...@@ -1186,7 +1186,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
describe '#local_branches' do describe '#local_branches' do
before(:all) do before(:all) do
@repo = Gitlab::Git::Repository.new('default', File.join(TEST_MUTABLE_REPO_PATH, '.git')) @repo = Gitlab::Git::Repository.new('default', File.join(TEST_MUTABLE_REPO_PATH, '.git'), '')
end end
after(:all) do after(:all) do
......
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Tag, seed_helper: true do describe Gitlab::Git::Tag, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
shared_examples 'Gitlab::Git::Repository#tags' do shared_examples 'Gitlab::Git::Repository#tags' do
describe 'first tag' do describe 'first tag' do
......
require "spec_helper" require "spec_helper"
describe Gitlab::Git::Tree, seed_helper: true do describe Gitlab::Git::Tree, seed_helper: true do
let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH) } let(:repository) { Gitlab::Git::Repository.new('default', TEST_REPO_PATH, '') }
context :repo do context :repo do
let(:tree) { Gitlab::Git::Tree.where(repository, SeedRepo::Commit::ID) } let(:tree) { Gitlab::Git::Tree.where(repository, SeedRepo::Commit::ID) }
......
...@@ -384,7 +384,7 @@ describe Gitlab::GitAccess do ...@@ -384,7 +384,7 @@ describe Gitlab::GitAccess do
def stub_git_hooks def stub_git_hooks
# Running the `pre-receive` hook is expensive, and not necessary for this test. # Running the `pre-receive` hook is expensive, and not necessary for this test.
allow_any_instance_of(GitHooksService).to receive(:execute) do |service, &block| allow_any_instance_of(Gitlab::Git::HooksService).to receive(:execute) do |service, &block|
block.call(service) block.call(service)
end end
end end
......
...@@ -34,7 +34,7 @@ describe Gitlab::ImportExport::RepoRestorer do ...@@ -34,7 +34,7 @@ describe Gitlab::ImportExport::RepoRestorer do
it 'has the webhooks' do it 'has the webhooks' do
restorer.restore restorer.restore
expect(Gitlab::Git::Hook.new('post-receive', project)).to exist expect(Gitlab::Git::Hook.new('post-receive', project.repository.raw_repository)).to exist
end end
end end
end end
...@@ -8,6 +8,7 @@ describe Repository, models: true do ...@@ -8,6 +8,7 @@ describe Repository, models: true do
let(:repository) { project.repository } let(:repository) { project.repository }
let(:broken_repository) { create(:project, :broken_storage).repository } let(:broken_repository) { create(:project, :broken_storage).repository }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:committer) { Gitlab::Git::Committer.from_user(user) }
let(:commit_options) do let(:commit_options) do
author = repository.user_to_committer(user) author = repository.user_to_committer(user)
...@@ -846,7 +847,7 @@ describe Repository, models: true do ...@@ -846,7 +847,7 @@ describe Repository, models: true do
expect do expect do
repository.add_branch(user, 'new_feature', 'master') repository.add_branch(user, 'new_feature', 'master')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
it 'does not create the branch' do it 'does not create the branch' do
...@@ -854,7 +855,7 @@ describe Repository, models: true do ...@@ -854,7 +855,7 @@ describe Repository, models: true do
expect do expect do
repository.add_branch(user, 'new_feature', 'master') repository.add_branch(user, 'new_feature', 'master')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
expect(repository.find_branch('new_feature')).to be_nil expect(repository.find_branch('new_feature')).to be_nil
end end
end end
...@@ -884,8 +885,8 @@ describe Repository, models: true do ...@@ -884,8 +885,8 @@ describe Repository, models: true do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
it 'runs without errors' do it 'runs without errors' do
expect_any_instance_of(GitHooksService).to receive(:execute) expect_any_instance_of(Gitlab::Git::HooksService).to receive(:execute)
.with(user, project, old_rev, blank_sha, 'refs/heads/feature') .with(committer, repository, old_rev, blank_sha, 'refs/heads/feature')
expect { repository.rm_branch(user, 'feature') }.not_to raise_error expect { repository.rm_branch(user, 'feature') }.not_to raise_error
end end
...@@ -905,7 +906,7 @@ describe Repository, models: true do ...@@ -905,7 +906,7 @@ describe Repository, models: true do
expect do expect do
repository.rm_branch(user, 'feature') repository.rm_branch(user, 'feature')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
it 'does not delete the branch' do it 'does not delete the branch' do
...@@ -913,7 +914,7 @@ describe Repository, models: true do ...@@ -913,7 +914,7 @@ describe Repository, models: true do
expect do expect do
repository.rm_branch(user, 'feature') repository.rm_branch(user, 'feature')
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
expect(repository.find_branch('feature')).not_to be_nil expect(repository.find_branch('feature')).not_to be_nil
end end
end end
...@@ -925,23 +926,23 @@ describe Repository, models: true do ...@@ -925,23 +926,23 @@ describe Repository, models: true do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
before do before do
service = GitHooksService.new service = Gitlab::Git::HooksService.new
expect(GitHooksService).to receive(:new).and_return(service) expect(Gitlab::Git::HooksService).to receive(:new).and_return(service)
expect(service).to receive(:execute) expect(service).to receive(:execute)
.with(user, project, old_rev, new_rev, 'refs/heads/feature') .with(committer, repository, old_rev, new_rev, 'refs/heads/feature')
.and_yield(service).and_return(true) .and_yield(service).and_return(true)
end end
it 'runs without errors' do it 'runs without errors' do
expect do expect do
GitOperationService.new(user, repository).with_branch('feature') do GitOperationService.new(committer, repository).with_branch('feature') do
new_rev new_rev
end end
end.not_to raise_error end.not_to raise_error
end end
it 'ensures the autocrlf Git option is set to :input' do it 'ensures the autocrlf Git option is set to :input' do
service = GitOperationService.new(user, repository) service = GitOperationService.new(committer, repository)
expect(service).to receive(:update_autocrlf_option) expect(service).to receive(:update_autocrlf_option)
...@@ -952,7 +953,7 @@ describe Repository, models: true do ...@@ -952,7 +953,7 @@ describe Repository, models: true do
it 'updates the head' do it 'updates the head' do
expect(repository.find_branch('feature').dereferenced_target.id).to eq(old_rev) expect(repository.find_branch('feature').dereferenced_target.id).to eq(old_rev)
GitOperationService.new(user, repository).with_branch('feature') do GitOperationService.new(committer, repository).with_branch('feature') do
new_rev new_rev
end end
...@@ -974,7 +975,7 @@ describe Repository, models: true do ...@@ -974,7 +975,7 @@ describe Repository, models: true do
end end
expect do expect do
GitOperationService.new(user, target_project.repository) GitOperationService.new(committer, target_project.repository)
.with_branch('feature', .with_branch('feature',
start_project: project, start_project: project,
&:itself) &:itself)
...@@ -996,7 +997,7 @@ describe Repository, models: true do ...@@ -996,7 +997,7 @@ describe Repository, models: true do
repository.add_branch(user, branch, old_rev) repository.add_branch(user, branch, old_rev)
expect do expect do
GitOperationService.new(user, repository).with_branch(branch) do GitOperationService.new(committer, repository).with_branch(branch) do
new_rev new_rev
end end
end.not_to raise_error end.not_to raise_error
...@@ -1014,7 +1015,7 @@ describe Repository, models: true do ...@@ -1014,7 +1015,7 @@ describe Repository, models: true do
# Updating 'master' to new_rev would lose the commits on 'master' that # Updating 'master' to new_rev would lose the commits on 'master' that
# are not contained in new_rev. This should not be allowed. # are not contained in new_rev. This should not be allowed.
expect do expect do
GitOperationService.new(user, repository).with_branch(branch) do GitOperationService.new(committer, repository).with_branch(branch) do
new_rev new_rev
end end
end.to raise_error(Repository::CommitError) end.to raise_error(Repository::CommitError)
...@@ -1026,10 +1027,10 @@ describe Repository, models: true do ...@@ -1026,10 +1027,10 @@ describe Repository, models: true do
allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([false, '']) allow_any_instance_of(Gitlab::Git::Hook).to receive(:trigger).and_return([false, ''])
expect do expect do
GitOperationService.new(user, repository).with_branch('feature') do GitOperationService.new(committer, repository).with_branch('feature') do
new_rev new_rev
end end
end.to raise_error(GitHooksService::PreReceiveError) end.to raise_error(Gitlab::Git::HooksService::PreReceiveError)
end end
end end
...@@ -1044,7 +1045,7 @@ describe Repository, models: true do ...@@ -1044,7 +1045,7 @@ describe Repository, models: true do
expect(repository).not_to receive(:expire_emptiness_caches) expect(repository).not_to receive(:expire_emptiness_caches)
expect(repository).to receive(:expire_branches_cache) expect(repository).to receive(:expire_branches_cache)
GitOperationService.new(user, repository) GitOperationService.new(committer, repository)
.with_branch('new-feature') do .with_branch('new-feature') do
new_rev new_rev
end end
......
...@@ -76,7 +76,7 @@ describe Files::UpdateService do ...@@ -76,7 +76,7 @@ describe Files::UpdateService do
let(:branch_name) { "#{project.default_branch}-new" } let(:branch_name) { "#{project.default_branch}-new" }
it 'fires hooks only once' do it 'fires hooks only once' do
expect(GitHooksService).to receive(:new).once.and_call_original expect(Gitlab::Git::HooksService).to receive(:new).once.and_call_original
subject.execute subject.execute
end end
......
...@@ -217,7 +217,7 @@ describe MergeRequests::MergeService do ...@@ -217,7 +217,7 @@ describe MergeRequests::MergeService do
it 'logs and saves error if there is an PreReceiveError exception' do it 'logs and saves error if there is an PreReceiveError exception' do
error_message = 'error message' error_message = 'error message'
allow(service).to receive(:repository).and_raise(GitHooksService::PreReceiveError, error_message) allow(service).to receive(:repository).and_raise(Gitlab::Git::HooksService::PreReceiveError, error_message)
allow(service).to receive(:execute_hooks) allow(service).to receive(:execute_hooks)
service.execute(merge_request) service.execute(merge_request)
......
...@@ -41,7 +41,7 @@ describe Tags::CreateService do ...@@ -41,7 +41,7 @@ describe Tags::CreateService do
it 'returns an error' do it 'returns an error' do
expect(repository).to receive(:add_tag) expect(repository).to receive(:add_tag)
.with(user, 'v1.1.0', 'master', 'Foo') .with(user, 'v1.1.0', 'master', 'Foo')
.and_raise(GitHooksService::PreReceiveError, 'something went wrong') .and_raise(Gitlab::Git::HooksService::PreReceiveError, 'something went wrong')
response = service.execute('v1.1.0', 'master', 'Foo') response = service.execute('v1.1.0', 'master', 'Foo')
......
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