Skip to content

Instantly share code, notes, and snippets.

@alexmchale
Last active August 29, 2015 13:57
Show Gist options
  • Save alexmchale/9915238 to your computer and use it in GitHub Desktop.
Save alexmchale/9915238 to your computer and use it in GitHub Desktop.
A post-commit hook for Git to print (and copy to clipboard on OSX) the likely GitHub URL for the commit
#!/usr/bin/env ruby
=begin
This hook will print the likely GitHub URL for each new commit.
Install this hook with wget:
wget 'https://gist.githubusercontent.com/alexmchale/9915238/raw/post-commit' -O .git/hooks/post-commit; chmod u+x .git/hooks/post-commit
Install this hook with curl:
curl 'https://gist.githubusercontent.com/alexmchale/9915238/raw/post-commit' > .git/hooks/post-commit; chmod u+x .git/hooks/post-commit
=end
require "shellwords"
revision = `git log --pretty=oneline -1 HEAD | cut -d ' ' -f 1`
(_, username, repository) = `git remote -v | grep github.com | head -1`.match(%r|github.com[:/](.*?)/(.*?).git|).to_a
url = "https://github.com/#{ username }/#{ repository }/commit/#{ revision }"
escaped_url = Shellwords.escape(url)
system "echo #{ escaped_url } | /usr/bin/pbcopy general" if File.executable? "/usr/bin/pbcopy"
puts
puts url
puts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment