summaryrefslogtreecommitdiff
path: root/bin/create-hooks
blob: 1adc8098400f74f81c9780b2c9b2df4c1a332f6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby

# Recreate GitLab hooks in the Git repositories managed by GitLab.
#
# This script is used when restoring a GitLab backup.

require_relative '../lib/gitlab_init'
require File.join(ROOT_PATH, 'lib', 'gitlab_metrics')
require 'fileutils'

def create_hooks(path)
  global_hooks_directory = File.join(ROOT_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 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}."
      FileUtils.mv(local_hooks_directory, "#{local_hooks_directory}.old.#{Time.now.to_i}")
    end
    FileUtils.ln_sf(global_hooks_directory, local_hooks_directory)
  else
    $logger.info "Hooks already exist for #{path}."
    true
  end
end

repository_storage_paths = ARGV

repository_storage_paths.each do |repo_path|
  Dir["#{repo_path.chomp('/')}/**/*.git"].each do |repo|
    begin
      GitlabMetrics.measure('command-create-hooks') do
        create_hooks(repo)
      end
    rescue Errno::ENOENT
      # The user must have deleted their repository. Ignore.
    end
  end
end