Module: Git::Story::Setup

Extended by:
Utils
Includes:
Utils
Defined in:
lib/git/story/setup.rb

Constant Summary collapse

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')

Class Method Summary collapse

Methods included from Utils

ask, capture, sh

Class Method Details

.file_installed?(filename) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/git/story/setup.rb', line 37

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

.install_file(filename) ⇒ Object



44
45
46
47
# File 'lib/git/story/setup.rb', line 44

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

.perform(force: false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/git/story/setup.rb', line 13

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