Skip to content

Instantly share code, notes, and snippets.

@lmullen
Created July 27, 2013 17:41
Show Gist options
  • Select an option

  • Save lmullen/6095650 to your computer and use it in GitHub Desktop.

Select an option

Save lmullen/6095650 to your computer and use it in GitHub Desktop.

Revisions

  1. lmullen created this gist Jul 27, 2013.
    29 changes: 29 additions & 0 deletions post-commit
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/usr/bin/env ruby

    # Write git commit messages to a log file
    #
    # Lincoln A. Mullen | lincoln@lincolnmullen.com | http://lincolnmullen.com
    # MIT License <http://lmullen.mit-license.org/>
    #
    # 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:
    # 2013-07-26-11-25-55 [repository:branch] First line of commit message

    require "git"

    log_file = "/home/lmullen/todo/time-use.txt"
    repo = Git.open(Dir.pwd)
    repo_name = Dir.pwd[%r{[\.\-\w]+$}] # top directory
    date = repo.log.first.date.strftime("%Y-%m-%d-%H-%M-%S") # format date
    message = repo.log.first.message.lines.first.strip # just first line
    branch = repo.log.first.name
    File.open(log_file, "a") do |log|
    log.puts "#{date} [#{repo_name}:#{branch}] #{message}"
    end