Skip to content

Instantly share code, notes, and snippets.

@kylefrost
Last active July 31, 2021 19:24
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save kylefrost/f3841e76749dc64f4b5e to your computer and use it in GitHub Desktop.
Save kylefrost/f3841e76749dc64f4b5e to your computer and use it in GitHub Desktop.
How-To: Tweet All Commit Messages

Creating the post-commit file

Note: If you want to use your personal Twitter account to post commits, go to Step 2

  1. Create a new Twitter account for your commit messages. Example
  2. Go to http://dev.twitter.com and Sign In with your Twitter account you are posting commit messages to.
  3. Hover over your username in the top-right corner after signing in and select "My Applications"
  4. Create a new application
  5. The name, description, and site can all be whatever you want, but leave Callback URL empty
  6. Under "Application Settings" click "modify app permissions" next to "Access level"
  7. Select "Read and Write" and then click "Update Settings"
  8. On the tabs at the top, go to "API Keys"
  9. Scroll to the bottom and under "Your Access Token click "Create my access token"
  10. Copy down into a text document the "API Key" and "API Secret" under "Application Settings" as well as "Access Token" and "Access Token Secret" under the genereted "Your Access Token" - you need these later
  • You may have to wait a few moments and refresh in order to see your access token
  1. You can close Twitter Developer now
  2. Download oauth_sign and http_post, save them somewhere that you can easily find them
  3. Open your favorite Terminal application and cd to the directory of the uncompressed oauth_sign zip (e.g. cd /path/to/oauth_sign/)
  4. Run sudo make then sudo make install from inside the oauth_sign directory
  5. Now cd into the directory of the uncompressed http_post zip
  6. Run sudo make SSL_DEFS="-DUSE_SSL" SSL_LIBS="-lssl -lcrypto" then sudo make install
  7. Open a new Terminal window and make sure they are there by typing oauth_sign and http_post and it doesn't say command not found
  8. You can delete the left over files now
  9. Save the post-commit file from this gist and edit the file with your personal API Keys that you put in the text document earlier
  10. Copy post-commit into your local git repository in .git/hooks/post-commit
  11. Run chmod a+x on the post-commit in order to make it executable
  12. Now it should send a tweet every time you make a commit message from the repo you added it to

Add this file by default to every new git repo

  1. Create a .git_template/hooks/ folder in your home directory
  2. mkdir ~/.git_template
  3. mkdir ~/.git_template/hooks/
  4. Run git config --global init.templatedir '~/.git_template'
  5. Copy all the files from your current git repo's hooks folder (the one you put post-commit in) into the new hooks folder
  • e.g. cp -R /my/git/repo/.git/hooks/ ~/.git_template/hooks/
  1. Done! Every time you create a new repo on your machine, the post-commit file should be there
#!/bin/sh
# PATH modification needed for http_post and oauth_sign
export PATH=$PATH:/usr/local/bin
toplevel_path=`git rev-parse --show-toplevel`
toplevel_dir=`basename "$toplevel_path"`
branch=`git rev-parse --abbrev-ref HEAD`
subject=`git log --pretty=format:%s -n1`
hashtags="#code #$toplevel_dir"
tweet=$hashtags' ['$branch']: "'$subject'"'
# truncate tweets that are longer than 140 characters
if [ ${#tweet} -gt 140 ]
then
tweet_trunc=$(echo $tweet | cut -c1-137)
tweet=${tweet_trunc}...
fi
consumer_key="YOUR API KEY"
consumer_secret="YOUR API SECRET"
access_token="YOUR ACCESS TOKEN"
access_secret="YOUR ACCESS TOKEN SECRET"
url="https://api.twitter.com/1.1/statuses/update.json"
http_post -h Authorization "$(oauth_sign \
$consumer_key $consumer_secret \
$access_token $access_secret \
POST "$url" status="$tweet")" \
"$url" status="$tweet"
@riders994
Copy link

You are now required to give a call-back URL when creating a twitter app. This documentation needs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment