Skip to content

Instantly share code, notes, and snippets.

@guyc
Created April 5, 2011 03:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guyc/902957 to your computer and use it in GitHub Desktop.
Save guyc/902957 to your computer and use it in GitHub Desktop.
git hook to add the first subdirectory name in the repo to the commit message.
#!/usr/bin/ruby
#
# Save this file as GITDIR/.git/hooks/prepare-commit-msg
# where GITDIR is the top directory of your git repository.
# then chmod +x prepare-commit-msg
#
# This hook is invoked by 'git-commit' right after preparing the default log
# message, and before the editor is started.
#
# It takes one to three parameters. The first is the name of the file that the commit
# log message. The second is the source of the commit message, and can be: message
# (if a -m or -F option was given); template (if a -t option was given or the
# configuration option commit.template is set); merge (if the commit is a merge
# or a .git/MERGE_MSG file exists); squash (if a .git/SQUASH_MSG file exists);
# or commit, followed by a commit SHA1 (if a -c, -C or \--amend option was given).
file_name,source,sha1 = ARGV
# in our intended use case only file_name seems to be set
if source=='commit' || source.nil?
# Dir.getwd has changed to root of repo at this point
# ENV['PWD'] points to the users pwd
pwd = ENV['PWD']
repo_dir = Dir.getwd
if pwd =~ Regexp.new("#{repo_dir}\/([^\/]+)")
top_dir = $1
lines = File.open(file_name,'r'){|f| f.readlines}
lines[0] = "#{top_dir}:"+lines[0]
File.open(file_name,'w'){|f| f.write(lines)}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment