Sha256: 45aeac1f5c50c15a2f3124bc2ecaa8562d0d6fe32fd19f040d2bd4dbe1effacc

Contents?: true

Size: 1.18 KB

Versions: 6

Compression:

Stored size: 1.18 KB

Contents

module Git::Story::Setup
  include Git::Story::Utils
  extend Git::Story::Utils

  MARKER = 'Installed by the git-story gem'

  HOOKS_DIR = '.git/hooks'
  PREPARE_COMMIT_MESSAGE_SRC = File.join(__dir__, 'prepare-commit-msg')
  PREPARE_COMMIT_MESSAGE_DST = File.join(HOOKS_DIR, 'prepare-commit-msg')

  module_function

  def perform(force: false)
    for filename in %w[ prepare-commit-msg pre-push ]
      if path = file_installed?(filename)
        if force
          install_file filename
        elsif File.read(path).match?(MARKER)
          ;
        else
          ask(
            prompt: "File #{path.inspect} not created by git-story."\
              " Overwrite? (y/n, default is %s) ",
            default: ?n,
          ) do |response|
            if response == ?y
              install_file filename
            end
          end
        end
      else
        install_file filename
      end
    end
  end

  def file_installed?(filename)
    path = File.join(HOOKS_DIR, filename)
    if File.exist?(path)
      path
    end
  end

  def install_file(filename)
    File.exist?(HOOKS_DIR) or mkdir_p(HOOKS_DIR)
    cp File.join(__dir__, filename), File.join(HOOKS_DIR, filename)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
git-story-workflow-1.4.2 lib/git/story/setup.rb
git-story-workflow-1.4.1 lib/git/story/setup.rb
git-story-workflow-1.4.0 lib/git/story/setup.rb
git-story-workflow-1.3.0 lib/git/story/setup.rb
git-story-workflow-1.2.0 lib/git/story/setup.rb
git-story-workflow-1.1.0 lib/git/story/setup.rb