Skip to content

Instantly share code, notes, and snippets.

@dhovart
Last active August 29, 2015 13:56
Show Gist options
  • Save dhovart/9129629 to your computer and use it in GitHub Desktop.
Save dhovart/9129629 to your computer and use it in GitHub Desktop.
Git post-checkout and post-commit scripts to ( trigger | stop ) a hcl timer when ( switching branches | committing )
#!/usr/bin/env ruby
# encoding: utf-8
# Git post-checkout script to trigger hcl.
# To be put in .git/hooks/post-checkout and made executable.
# Misc. enhancement ideas :
# automatically map git branches to hcl tasks, storing data in ~/.hcl-git.yml
# prompt for input w/ readline & autocomplete hcl aliases
# create complementary post-commit script asking wether to stop timer or not
require 'HCl'
# Catch interrupt signals to exit with <Ctrl-c> without raising an exception
trap('SIGINT') { abort }
prev_head, new_head, checkout_type = *ARGV
ignored_branches = ['master', 'staging']
current_branch = `git rev-parse --abbrev-ref HEAD`.chomp
prev_branch = `git rev-parse --abbrev-ref @{-1}`.chomp
# checkout_type : branch = '1', file = '0'
if checkout_type != '1' || ignored_branches.include?(current_branch) || current_branch == prev_branch
abort
end
# Git hooks scripts are not interactive - the following allows us to reassign sdtin to the current tty and prompt for input
$stdin.reopen('/dev/tty')
print "Start timer? [n]/y "
abort unless $stdin.gets.chomp.casecmp('y') == 0
print "Please specify an hcl alias: "
hcl_alias = $stdin.gets.chomp
abort "No alias specified - aborting." if hcl_alias.empty?
print "What are you working on? (optional) "
task = $stdin.gets.chomp
print "Intial time spent on task (optional): "
initial_time = $stdin.gets.chomp
app = HCl::App.new
begin
app.start("+#{initial_time}", "@#{hcl_alias}", task)
rescue Exception => e
abort e.message
end
#!/usr/bin/env ruby
# encoding: utf-8
# Git post-commit script to stop a running hcl timer.
# To be put in .git/hooks/post-commit and made executable.
require 'HCl'
# Catch interrupt signals to exit with <Ctrl-c> without raising an exception
trap('SIGINT') { abort }
app = HCl::App.new
entry = HCl::DayEntry.with_timer(app.http) || HCl::DayEntry.with_timer(app.http, Date.today - 1)
abort if entry.nil?
# Git hooks scripts are not interactive - the following allows us to reassign sdtin to the current tty and prompt for input
$stdin.reopen('/dev/tty')
print "There currently is a timer set for “#{entry.to_s}”.\nWould you like to stop it? [y]/n "
begin
app.stop if $stdin.gets.chomp.casecmp('n') != 0
puts "Stopped"
rescue Exception => e
abort e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment