Class: Historian::Git::Hooker

Inherits:
Object
  • Object
show all
Defined in:
lib/historian/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_dir, hook) ⇒ Hooker

Returns a new instance of Hooker.



9
10
11
12
# File 'lib/historian/git.rb', line 9

def initialize(repo_dir, hook)
  self.repo_dir = repo_dir
  self.hook = hook
end

Instance Attribute Details

#hookObject

Returns the value of attribute hook.



7
8
9
# File 'lib/historian/git.rb', line 7

def hook
  @hook
end

#repo_dirObject

Returns the value of attribute repo_dir.



7
8
9
# File 'lib/historian/git.rb', line 7

def repo_dir
  @repo_dir
end

Instance Method Details

#create_hook_scriptObject



14
15
16
17
18
19
20
# File 'lib/historian/git.rb', line 14

def create_hook_script
  File.open(hook_script, "w") do |f|
    f.puts "#!/bin/bash"
    f.puts "historian #{hook.to_s} $@"
  end
  File.chmod 0755, hook_script
end

#create_hook_scripts_dirObject



22
23
24
# File 'lib/historian/git.rb', line 22

def create_hook_scripts_dir
  Dir.mkdir hook_scripts_dir unless File.directory? hook_scripts_dir
end

#create_hook_wrapper_scriptObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/historian/git.rb', line 26

def create_hook_wrapper_script
  File.open(hook_wrapper_script, "w") do |f|
    script = <<-EOF
      #!/bin/bash
      #
      # Git hook modified by Historian. Do not modify this file.
      # Add more hooks in the #{hook_to_git}.d directory.
      #
      BASE=`dirname $0`
      for S in $BASE/#{hook_to_git}.d/*
      do
      $S $@ || exit $?
      done
    EOF
    f.puts script.gsub(/^\s+/,'')
  end
  File.chmod 0755, hook_wrapper_script
end

#hook_scriptObject



49
50
51
# File 'lib/historian/git.rb', line 49

def hook_script
  File.join hook_scripts_dir, "historian"
end

#hook_scripts_dirObject



53
54
55
# File 'lib/historian/git.rb', line 53

def hook_scripts_dir
  File.join hooks_dir, hook_to_git + ".d"
end

#hook_to_gitObject



57
58
59
# File 'lib/historian/git.rb', line 57

def hook_to_git
  hook.to_s.gsub "_", "-"
end

#hook_wrapper_scriptObject



61
62
63
# File 'lib/historian/git.rb', line 61

def hook_wrapper_script
  File.join hooks_dir, hook_to_git
end

#hooks_dirObject



65
66
67
# File 'lib/historian/git.rb', line 65

def hooks_dir
  File.join repo_dir, ".git", "hooks"
end

#installObject



69
70
71
72
73
# File 'lib/historian/git.rb', line 69

def install
  create_hook_scripts_dir
  create_hook_script
  create_hook_wrapper_script
end

#installed?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/historian/git.rb', line 75

def installed?
  File.exists? hook_script
end

#other_git_hook_exists?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/historian/git.rb', line 45

def other_git_hook_exists?
  File.exists?(hook_wrapper_script) && !File.directory?(hook_scripts_dir)
end

#preserve_originalObject



79
80
81
82
# File 'lib/historian/git.rb', line 79

def preserve_original
  create_hook_scripts_dir
  FileUtils.cp hook_wrapper_script, File.join(hook_scripts_dir, "original")
end

#updateObject



84
85
86
# File 'lib/historian/git.rb', line 84

def update
  create_hook_script
end