Skip to content

Instantly share code, notes, and snippets.

@jberkel
Created February 24, 2009 00:51
Show Gist options
  • Save jberkel/69304 to your computer and use it in GitHub Desktop.
Save jberkel/69304 to your computer and use it in GitHub Desktop.
post-receive hook for marley
#!/usr/bin/env ruby
#based on /usr/share/doc/git-core/contrib/hooks/post-receive-email
require 'syslog'
MARLEY_DIR="/home/git/marley"
def log(msg)
Syslog.open
Syslog.debug(msg)
Syslog.close
end
def parse_rev(old_rev, new_rev, ref_name)
raise ArgumentError, "need all parameters" unless old_rev && new_rev && ref_name
oldrev=`git rev-parse #{old_rev}`
newrev=`git rev-parse #{new_rev}`
# --- Interpret
# 0000->1234 (create)
# 1234->2345 (update)
# 2345->0000 (delete)
if oldrev =~ /\A0*\E/
change_type="create"
else
if newrev =~ /\A0*\E/
change_type="delete"
else
change_type="update"
end
end
# --- Get the revision types
newrev_type=`git cat-file -t #{newrev}`.to_s.strip
oldrev_type=`git cat-file -t #{oldrev}`.to_s.strip
case change_type
when "create", "update":
rev=newrev
rev_type=newrev_type
when "delete":
rev=oldrev
rev_type=oldrev_type
end
case ref_name
when %r{refs/tags/(\w+)}:
refname_type = rev_type == 'commit' ? 'tag' : 'annotated tag'
short_refname = $1
when %r{refs/(heads|remotes)/(\w+)}:
refname_type = $1 == 'heads' ? 'branch' : 'tracking branch'
short_refname = $2
else
raise ArgumentError, "unknown ref_name: '#{ref_name}'"
end
{
:refname_type => refname_type,
:short_refname => short_refname,
:rev_type => rev_type,
:rev => rev
}
end
def update_marley(dir, commit)
if commit[:rev_type] == 'commit' && commit[:short_refname] == 'zegoggles'
log "updating blog with commit rev: #{commit[:rev]}"
print <<-`EOC`
unset GIT_DIR
cd #{dir} && git pull origin #{commit[:short_refname]} && touch tmp/restart.txt
EOC
$?
else
0
end
end
if $0 == __FILE__
# Allow dual mode: run from the command line just like the update hook, or if no arguments are given then run as a hook script
commit_info = if ARGV.size == 3
parse_rev(ARGV[0], ARGV[1], ARGV[2])
else
stdin = STDIN.read
if stdin.scan(/^([0-9a-z]+)\s([0-9a-z]+)\s(.+)$/)
parse_rev($1, $2, $3)
else
raise ArgumentError, "invalid git hook received: #{stdin.strip}"
end
end
exit(update_marley(MARLEY_DIR, commit_info))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment