Created
December 14, 2011 21:54
-
-
Save piersadrian/1478735 to your computer and use it in GitHub Desktop.
Automating Git with Gitsync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Gitsync sychronizes Git's origin repository to the local copy by | |
# first fetching all changes, presenting a simple diff of the changes, | |
# then asking for confirmation before merging the changes into the | |
# local branch and pushing changes. | |
# | |
# Thanks to Aaron Crane (http://aaroncrane.co.uk/2009/03/git_branch_prompt/) | |
# | |
# Author: Piers Mainwaring. | |
# License: Public Domain (no rights reserved) | |
function find_git_branch { | |
local dir=. head | |
until [ "$dir" -ef / ]; do | |
if [ -f "$dir/.git/HEAD" ]; then | |
head=$(< "$dir/.git/HEAD") | |
if [[ $head == ref:\ refs/heads/* ]]; then | |
git_branch=" ${head#*/*/}" | |
elif [[ $head != '' ]]; then | |
git_branch=' (detached)' | |
else | |
git_branch=' (unknown)' | |
fi | |
return | |
fi | |
dir="../$dir" | |
done | |
git_branch='' | |
} | |
find_git_branch | |
echo "Fetching updates from the origin server..." | |
git fetch origin | |
diff=`git diff --stat \`echo $git_branch\` origin/\`echo $git_branch\`` | |
log=`git log -n 3 origin/\`echo $git_branch\`` | |
if [ "$diff" == "" ]; then | |
echo -e "________________________" | |
echo -e "Everything is synced fully, so there's nothing to do here. Carry on!" | |
exit 0 | |
fi | |
echo -e "\n________________________" | |
echo -e "OK. There are changes between the local and public versions of$git_branch. Files changed:\n" | |
echo -e "$diff" | |
sleep 3 | |
echo -e "\n________________________" | |
echo -e "And the most recent remote commits:\n" | |
sleep 0.5 | |
echo -e "$log" | |
echo -e "\n________________________" | |
echo -n "Is it OK to merge your commits and any new work from the public timeline? If not, enter 'n': " | |
read perform_merge | |
if [ "$perform_merge" == "n" ]; then | |
echo -e "\nStopping. No code has been merged yet, so go investigate and come back when you're comfortable." | |
exit 0 | |
fi | |
echo "________________________" | |
echo -e "Cool! Merging your commits with new work from the public timeline...\n" | |
git merge --no-ff origin/`echo $git_branch` | |
echo -e "\n________________________" | |
echo -n "Is it OK to update the origin server with these changes? If not, enter 'n': " | |
read perform_push | |
if [ "$perform_push" == "n" ]; then | |
echo -e "\nStopping. Your commits have been merged with new work from the public timeline, but your" | |
echo -e "new work has not yet been pushed to the origin server. Run 'git push' to finish up." | |
exit 0 | |
fi | |
echo "________________________" | |
echo -e "OK. Pushing your commits to the origin server.\n" | |
git push origin HEAD:`echo $git_branch` | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment