Commit 836ace5b authored by Douwe Maan's avatar Douwe Maan

Merge branch 'create-hooks-broken-symlink' into 'master'

Handle broken symlinks in create-hooks

If a repository contained a broken symlink named 'hooks', this would
raise ENOENT in lib/gitlab_projects.rb, which got ignored in
bin/create-hooks. This commit fixes that by making sure we handle
broken symlinks in lib/gitlab_projects.rb.

See merge request !19
parents c31e6165 a10d4723
...@@ -21,11 +21,19 @@ class GitlabProjects ...@@ -21,11 +21,19 @@ class GitlabProjects
def self.create_hooks(path) def self.create_hooks(path)
local_hooks_directory = File.join(path, 'hooks') local_hooks_directory = File.join(path, 'hooks')
real_local_hooks_directory = :not_found
begin
real_local_hooks_directory = File.realpath(local_hooks_directory)
rescue Errno::ENOENT
# real_local_hooks_directory == :not_found
end
if File.realpath(local_hooks_directory) != File.realpath(GLOBAL_HOOKS_DIRECTORY) if real_local_hooks_directory != File.realpath(GLOBAL_HOOKS_DIRECTORY)
if File.exist?(local_hooks_directory)
$logger.info "Moving existing hooks directory and symlinking global hooks directory for #{path}." $logger.info "Moving existing hooks directory and symlinking global hooks directory for #{path}."
FileUtils.mv(local_hooks_directory, "#{local_hooks_directory}.old.#{Time.now.to_i}") FileUtils.mv(local_hooks_directory, "#{local_hooks_directory}.old.#{Time.now.to_i}")
FileUtils.ln_s(GLOBAL_HOOKS_DIRECTORY, local_hooks_directory) end
FileUtils.ln_sf(GLOBAL_HOOKS_DIRECTORY, local_hooks_directory)
else else
$logger.info "Hooks already exist for #{path}." $logger.info "Hooks already exist for #{path}."
true true
......
...@@ -12,6 +12,51 @@ describe GitlabProjects do ...@@ -12,6 +12,51 @@ describe GitlabProjects do
FileUtils.rm_rf(tmp_repos_path) FileUtils.rm_rf(tmp_repos_path)
end end
describe :create_hooks do
let(:repo_path) { File.join(tmp_repos_path, 'hook-test.git') }
let(:hooks_dir) { File.join(repo_path, 'hooks') }
let(:target_hooks_dir) { File.join(ROOT_PATH, 'hooks') }
let(:existing_target) { File.join(repo_path, 'foobar') }
before do
FileUtils.rm_rf(repo_path)
FileUtils.mkdir_p(repo_path)
end
context 'hooks is a directory' do
let(:existing_file) { File.join(hooks_dir, 'my-file') }
before do
FileUtils.mkdir_p(hooks_dir)
FileUtils.touch(existing_file)
GitlabProjects.create_hooks(repo_path)
end
it { File.readlink(hooks_dir).should == target_hooks_dir }
it { Dir[File.join(repo_path, "hooks.old.*/my-file")].count.should == 1 }
end
context 'hooks is a valid symlink' do
before do
FileUtils.mkdir_p existing_target
File.symlink(existing_target, hooks_dir)
GitlabProjects.create_hooks(repo_path)
end
it { File.readlink(hooks_dir).should == target_hooks_dir }
end
context 'hooks is a broken symlink' do
before do
FileUtils.rm_f(existing_target)
File.symlink(existing_target, hooks_dir)
GitlabProjects.create_hooks(repo_path)
end
it { File.readlink(hooks_dir).should == target_hooks_dir }
end
end
describe :initialize do describe :initialize do
before do before do
argv('add-project', repo_name) argv('add-project', repo_name)
......
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