Class: RGitHook::Runner

Inherits:
Object show all
Defined in:
lib/rgithook/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Runner

::nodoc

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
# File 'lib/rgithook/runner.rb', line 6

def initialize(repo) #::nodoc::
   raise ArgumentError unless repo.is_a? ::Grit::Repo
   @_repo = repo
   @_hooks = {}
   @_bg_hooks = {}
   @options = Plugin.options_to_hash
end

Instance Attribute Details

#bindingObject (readonly)

Returns the value of attribute binding.



4
5
6
# File 'lib/rgithook/runner.rb', line 4

def binding
  @binding
end

Instance Method Details

#load(file) ⇒ Object



124
125
126
127
# File 'lib/rgithook/runner.rb', line 124

def load(file)
   hooks = File.file?(file) && File.read(file) || ''
   eval(hooks, binding, file,0)
end

#load_options(options_file) ⇒ Object

::nodoc


173
174
175
176
# File 'lib/rgithook/runner.rb', line 173

def load_options(options_file)
   @options = YAML.load(File.read(options_file)) if File.file?(options_file)
   @options ||={}
end

#on(hook, options = {}, &block) ⇒ Object

Define a hook that is executed when git-commands call these hook These is the very most important part of rgithook. These are all the hooks that you can run from git.

# GIT_DIR/hooks/rgithook.rb (edit by rgithook -e)

#These will make that you will never can commit anthing that brake the tests
on :pre_commit do
  system(rake test)
end

#These will send test results of the master branch when the repo is updated
on :post_receive do |old_rev,new_rev,ref|
  if ref =~ /master$/
    %x(rake test | mail -s 'Updated #{ref}' [email protected])
  end
end

Also you can make the hooks run in a separate process with background option

#These will run in background
on :post_receive, :background => true do |old_rev,new_rev,ref|
  run_tests(repo) # These is a long runing operation
end

The abailable hooks within git docs are:

  • on :applypatch_msg do |commit_msg_path| - Check the commit log message taken by applypatch from an e-mail message. The hook should exit with non-zero status after issuing an appropriate message if it wants to stop the commit. The hook is allowed to edit the commit message file.<br/> commit_msg_path - Is the file_path of the commit_message

  • on :commit_msg do |commit_msg_path| - Check the commit log message. Called by git-commit with one argument, the name of the file that has the commit message. The hook should exit with non-zero status after issuing an appropriate message if it wants to stop the commit. The hook is allowed to edit the commit message file. These hook have params: commit_msg_path - Is the file_path of the commit_message

  • on :post_commit do - Is called after a successful commit is made.

  • on :post_receive do |old_rev,new_rev,ref| - Is run after receive-pack has accepted a pack and the repository has been updated. old_rev/new_rev Is the string old/new sha1 commit id. ref is the string of the updated ref (ref/head/master). These is the perfect hook to put your code in a bare central repo.

    # Send to the authors off the commits the results of their commits
    on :post_receive do |old_rev,new_rev,ref|
      if ref =~ /master$/
        emails = repo.commits_between(old_rev, new_rev).map{|c| c.author.email}.uniq
        test = %x(rake test)
        emails.each do |mail|
          IO.popen("mail -s 'Updated #{repo_name}' #{mail}",'w') {|mail| mail.write test}
        end
      end
    end
    
  • on :post_update do - Prepare a packed repository for use over dumb transports.

  • on :pre_apply_patch do - Verify what is about to be committed by applypatch from an e-mail message. The hook should return non zero after issuing an appropriate message if it wants to stop the commit.

  • on :pre_commit do - Verify what is about to be committed. Called by git-commit with no arguments. The hook should return non-zero after issuing an appropriate message if it wants to stop the commit.

  • on :pre_rebase do - Run just before “git-rebase” starts doing its job, and can prevent the command from running by exiting with non-zero status.

  • on :prepare_commit_msg do |commit_msg_path,commit_src| - Prepare the commit log message. Called by git-commit with the name of the file that has the commit message, followed by the description of the commit message’s source. The hook’s purpose is to edit the commit message file. If the hook returns non-zero status, the commit is aborted.

In case of an unrescue exception rgithook will show a warning with the error and return 255 to git. In case of more than one block for the same hook, rgithook return to git the max value of all returns values of the same hook

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rgithook/runner.rb', line 102

def on(hook, options = {}, &block)
   raise ArgumentError, "No block given for #{hook.to_s} in #{caller[0]}" unless block_given?
   if HOOKS.include? hook.to_s
      if options.delete(:background)
         @_bg_hooks[hook] ||= []
         @_bg_hooks[hook] << block
      else
         @_hooks[hook] ||= []
         @_hooks[hook] << block
      end
   else
      raise ArgumentError, "Not available hook #{hook.to_s} in #{caller[0]}"
   end
end

#optionsObject

Hash with all the plugin options

option[:PluginName][:option] => option_value

option[PluginClass.to_sym][:option_group][:option_name] => option_value


168
169
170
# File 'lib/rgithook/runner.rb', line 168

def options
   @options
end

#repoObject



159
160
161
# File 'lib/rgithook/runner.rb', line 159

def repo
   @_repo
end

#run(code, file = nil, line = nil) ⇒ Object

Run arbitrary code in runner context



119
120
121
122
# File 'lib/rgithook/runner.rb', line 119

def run(code,file=nil,line=nil)
   file,line = caller.first.split(':') unless file && line
   eval(code,binding,file,line.to_i)
end

#run_background_hooks(hook_name, *args) ⇒ Object

Execute all the hooks in a new process Return an array with the pids of each hook process



151
152
153
154
155
156
157
# File 'lib/rgithook/runner.rb', line 151

def run_background_hooks(hook_name, *args)
   ret_vals = []
   @_bg_hooks[hook_name.to_sym] && @_bg_hooks[hook_name.to_sym].each do |hook|
      ret_vals << fork {hook.call(*args)}
   end
   ret_vals
end

#run_foreground_hooks(hook_name, *args) ⇒ Object



141
142
143
144
145
146
147
# File 'lib/rgithook/runner.rb', line 141

def run_foreground_hooks(hook_name,*args)
   ret_vals = []
   @_hooks[hook_name.to_sym] && @_hooks[hook_name.to_sym].each do |hook|
      ret_vals << hook.call(*args)
   end
   ret_vals
end

#run_hooks(hook_name, *args) ⇒ Object

Execute all the hooks defined in the configuration file Return a two dimension array. The first element is the outputs of the foreground_hooks. The second element is another array with the pids of the background_hooks

fg_hook_val, bg_hook_val = @runner.run_hooks(hook_name)


133
134
135
136
# File 'lib/rgithook/runner.rb', line 133

def run_hooks(hook_name, *args)
   Plugin.load!
   [run_foreground_hooks(hook_name,*args),run_background_hooks(hook_name,*args)]
end