Skip to content

Instantly share code, notes, and snippets.

@zachelko
Last active August 29, 2015 13:57
Show Gist options
  • Save zachelko/9380394 to your computer and use it in GitHub Desktop.
Save zachelko/9380394 to your computer and use it in GitHub Desktop.
git post-commit hook to tweet your commit messages using twidge
#!/usr/bin/python3.3
import sys
import subprocess
# The git hook will invoke this script with some data in STDIN
# So, let's re-assign stdin to /dev/tty so we can grab user input
# and decide whether or not to tweet this commit
sys.stdin = open('/dev/tty')
shouldTweet = input('Tweet this commit? y/n: ')
if shouldTweet != "y" and shouldTweet != "Y":
quit()
MAX_LEN = 140
tweets = []
def trimString(inputStr):
if len(inputStr) <= MAX_LEN:
#print("tweet: " + inputStr)
tweets.append(inputStr)
else:
tweet = inputStr[:MAX_LEN - 3] + "..."
#print("tweet: " + tweet)
tweets.append(tweet)
trimString(inputStr[MAX_LEN - 3:])
p = subprocess.Popen(['git', 'log', '-1', 'HEAD'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
out = out.decode("utf-8").split("\n")
out = [segment for segment in out if segment]
author = out[1] # "Author: Zach <zachelko@gmail.com>"
tweet = author + " "
message = out[3].strip()
trimString(author + " " + message)
tweetStr = ""
for tweet in tweets:
tweetOption = "update"
subprocess.call(["twidge", tweetOption, tweet])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment