Commit 94a0a00f authored by Robert Speicher's avatar Robert Speicher

Merge branch 'autocrlf-lazy' into 'master'

Only set autocrlf when creating/updating files

Related issue: gitlab-org/gitlab-ce#13457
Details: 5619a6de1dd6fc1dfd4053810c7b11c677b7a495

See merge request !2859
parents 7f7eef2a 5b6d347f
...@@ -23,13 +23,11 @@ class Repository ...@@ -23,13 +23,11 @@ class Repository
def raw_repository def raw_repository
return nil unless path_with_namespace return nil unless path_with_namespace
@raw_repository ||= begin @raw_repository ||= Gitlab::Git::Repository.new(path_to_repo)
repo = Gitlab::Git::Repository.new(path_to_repo)
repo.autocrlf = :input
repo
rescue Gitlab::Git::Repository::NoRepository
nil
end end
def update_autocrlf_option
raw_repository.autocrlf = :input if raw_repository.autocrlf != :input
end end
# Return absolute path to repository # Return absolute path to repository
...@@ -40,7 +38,12 @@ class Repository ...@@ -40,7 +38,12 @@ class Repository
end end
def exists? def exists?
raw_repository return false unless raw_repository
raw_repository.rugged
true
rescue Gitlab::Git::Repository::NoRepository
false
end end
def empty? def empty?
...@@ -67,7 +70,7 @@ class Repository ...@@ -67,7 +70,7 @@ class Repository
end end
def commit(id = 'HEAD') def commit(id = 'HEAD')
return nil unless raw_repository return nil unless exists?
commit = Gitlab::Git::Commit.find(raw_repository, id) commit = Gitlab::Git::Commit.find(raw_repository, id)
commit = Commit.new(commit, @project) if commit commit = Commit.new(commit, @project) if commit
commit commit
...@@ -693,6 +696,8 @@ class Repository ...@@ -693,6 +696,8 @@ class Repository
end end
def commit_with_hooks(current_user, branch) def commit_with_hooks(current_user, branch)
update_autocrlf_option
oldrev = Gitlab::Git::BLANK_SHA oldrev = Gitlab::Git::BLANK_SHA
ref = Gitlab::Git::BRANCH_REF_PREFIX + branch ref = Gitlab::Git::BRANCH_REF_PREFIX + branch
was_empty = empty? was_empty = empty?
......
...@@ -200,13 +200,22 @@ describe Repository, models: true do ...@@ -200,13 +200,22 @@ describe Repository, models: true do
describe :commit_with_hooks do describe :commit_with_hooks do
context 'when pre hooks were successful' do context 'when pre hooks were successful' do
it 'should run without errors' do before do
expect_any_instance_of(GitHooksService).to receive(:execute).and_return(true) expect_any_instance_of(GitHooksService).to receive(:execute).
and_return(true)
end
it 'should run without errors' do
expect do expect do
repository.commit_with_hooks(user, 'feature') { sample_commit.id } repository.commit_with_hooks(user, 'feature') { sample_commit.id }
end.not_to raise_error end.not_to raise_error
end end
it 'should ensure the autocrlf Git option is set to :input' do
expect(repository).to receive(:update_autocrlf_option)
repository.commit_with_hooks(user, 'feature') { sample_commit.id }
end
end end
context 'when pre hooks failed' do context 'when pre hooks failed' do
...@@ -220,6 +229,25 @@ describe Repository, models: true do ...@@ -220,6 +229,25 @@ describe Repository, models: true do
end end
end end
describe '#exists?' do
it 'returns true when a repository exists' do
expect(repository.exists?).to eq(true)
end
it 'returns false when a repository does not exist' do
expect(repository.raw_repository).to receive(:rugged).
and_raise(Gitlab::Git::Repository::NoRepository)
expect(repository.exists?).to eq(false)
end
it 'returns false when there is no namespace' do
allow(repository).to receive(:path_with_namespace).and_return(nil)
expect(repository.exists?).to eq(false)
end
end
describe '#has_visible_content?' do describe '#has_visible_content?' do
subject { repository.has_visible_content? } subject { repository.has_visible_content? }
...@@ -249,6 +277,33 @@ describe Repository, models: true do ...@@ -249,6 +277,33 @@ describe Repository, models: true do
end end
end end
describe '#update_autocrlf_option' do
describe 'when autocrlf is not already set to :input' do
before do
repository.raw_repository.autocrlf = true
end
it 'sets autocrlf to :input' do
repository.update_autocrlf_option
expect(repository.raw_repository.autocrlf).to eq(:input)
end
end
describe 'when autocrlf is already set to :input' do
before do
repository.raw_repository.autocrlf = :input
end
it 'does nothing' do
expect(repository.raw_repository).to_not receive(:autocrlf=).
with(:input)
repository.update_autocrlf_option
end
end
end
describe '#empty?' do describe '#empty?' do
let(:empty_repository) { create(:project_empty_repo).repository } let(:empty_repository) { create(:project_empty_repo).repository }
......
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