Sha256: fcd152aae0f28d9daa0f1761c27cce150aac3f8967655e18e30d9d3ccc780ae2

Contents?: true

Size: 901 Bytes

Versions: 2

Compression:

Stored size: 901 Bytes

Contents

# vim:fileencoding=utf-8
require 'git/duet'
require 'fileutils'
require 'git/duet/command_methods'

class Git::Duet::InstallHookCommand
  include Git::Duet::CommandMethods

  HOOK = <<-EOF.gsub(/^ {2}/, '')
  #!/bin/bash
  exec git duet-pre-commit "$@"
  EOF

  def initialize(quiet = false)
    @quiet = quiet
  end

  def execute!
    Dir.chdir(`git rev-parse --show-toplevel`.chomp) do
      dest = File.join(Dir.pwd, '.git', 'hooks', 'pre-commit')
      return error_hook_exists(dest) if File.exist?(dest)
      File.open(dest, 'w') { |f| f.puts HOOK }
      FileUtils.chmod(0755, dest)
      info("git-duet-install-hook: Installed hook to #{dest}")
    end
    0
  end

  private

  def error_hook_exists(dest)
    error('git-duet-install-hook: ' <<
          "A pre-commit hook already exists at #{dest}!")
    error('git-duet-install-hook: Move it out of the way first, mkay?')
    1
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
git-duet-0.3.0 lib/git/duet/install_hook_command.rb
git-duet-0.2.0 lib/git/duet/install_hook_command.rb