Skip to content

Instantly share code, notes, and snippets.

@olets
Forked from lmullen/post-commit
Last active May 2, 2018 19:58
Show Gist options
  • Save olets/083e73a1b49284f5ff60d7532a6166e9 to your computer and use it in GitHub Desktop.
Save olets/083e73a1b49284f5ff60d7532a6166e9 to your computer and use it in GitHub Desktop.
A script to log commits from a #git hook
#!/usr/bin/env ruby
# Write git commit messages to a log file
#
# Lincoln A. Mullen | [email protected] | http://lincolnmullen.com
# MIT License <http://lmullen.mit-license.org/>
# Fork by Henry Bley-Vroman / @olets
#
# You will have to install the git gem for this to work:
# gem install git
#
# Name this file 'post-commit' and drop it in the directory '.git/hooks' in
# any repository that you want to log. Make sure the file is executable. You
# can also add this to your git templates, which will put it in every new
# repository or to existing repositories by re-running git init.
#
# A commit message in the log should look this this (large spaces are tabs)
# YYYY-MM-DD HH:MM:SS repo:branch commit message SHA
require "git"
log_file = ENV['HOME'] + "/commit_log.txt"
repo = Git.open(Dir.pwd)
repo_name = Dir.pwd[%r{[\.\-\w]+$}]
commit = repo.log.first
date = commit.date.strftime("%Y-%m-%d %H:%M:%S")
message = commit.message.lines.first.strip # just first line
branch = commit.name
short_sha = commit.sha[/.{7}/] # first seven characters of sha
File.open(log_file, "a") do |log|
log.puts "#{date}\t#{repo_name}:#{branch}\t#{message}\t#{short_sha}"
end
puts "Commit logged to #{log_file}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment