• Tip Jar

Git Hooks and GNU Social

If you have any projects managed by git, you may want to let people know when you have pushed some new code. Using a git hook makes this pretty simple. If you want to post to GNU Social, for example, the following pre-push hook will post the changes for you:

#!/bin/sh

# You can just put these settings in ~/.gnusocialrc instead of this file
# Make sure to chmod 600 ~/.gnusocialrc especially if you store your password

# Your GNU Social username
user=""
# Password, if not set you will be prompted
password=""
# Your node address, do not add trailing slashes, for example, https://example.org
node=""

# remote is $1 e.g. origin
# url is $2

if [ -r ~/.gnusocialrc ]; then
source ~/.gnusocialrc
fi

branch="$(git branch | grep "\* ")"
branch="${branch:2}"
commit="$(git log -1 --pretty=%B)"
pusher="$(git log | grep -m1 "Author: ")"
pusher="${pusher#Author: }"
pusher="${pusher% < *@*.*>}"

updateMessage="$pusher pushed changes to the $branch branch of the ${2##*/} project: $2
$commit"
if [ -z "$password" ]; then
echo "Please enter the password for ${user}'s GNU Social account:"
curl -s -u "$user" --data-urlencode status="$updateMessage" -d source="git" ${node}/api/statuses/update.json &> /dev/null && postStatus=0 || postStatus=1
else
curl -s -u "$user:$password" --data-urlencode status="$updateMessage" -d source="git" ${node}/api/statuses/update.json &> /dev/null && postStatus=0 || postStatus=1
fi
if [ $postStatus -eq 0 ]; then
echo "Changes posted to ${node}"
else
echo "There was a problem posting changes to ${node}."
fi
exit 0

Save this in each git project you want to notify your followers about under the .git/hooks directory as pre-push and it will be called every time you push code to the repository. You can either put your GNU Social information in the script itself, or just place the info it requests in ~/.gnusocialrc:

# Your GNU Social username
user=""
# Password, if not set you will be prompted
password=""
# Your node address, do not add trailing slashes, for example, https://example.org
node=""

Here is a plain text link to the script. Thanks for reading, and I hope you find this useful.

Bookmark the permalink.

One Comment

Comments are closed

  • Tip Jar