Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # git-update
- # Pull down latest from git remote and remove dead branches
- #
- # To create an alias to `git update`, put this on your path, then:
- # git config --global alias.update '!git-update'
- #
- # Copyright (c) 2016 Leftclick.com.au
- # License: MIT
- #
- # Defaults
- branch=master
- upstream=upstream
- delayPeriod=
- displayUsage=false
- errorMessage=
- # Parse options
- while getopts d:h param; do
- case $param in
- d)
- delayPeriod="$OPTARG"
- ;;
- h)
- displayUsage=true
- ;;
- *)
- errorMessage="Unknown argument, please check usage instructions"
- displayUsage=true
- ;;
- esac
- done
- shift $(expr $OPTIND - 1)
- # Check for too many arguments
- if [ $# -gt 2 ]; then
- errorMessage="Too many arguments, please check usage instructions"
- displayUsage=true
- fi
- # Display usage if requested or arguments are invalid
- if [ "${displayUsage}" = true ]; then
- if [ "${errorMessage}" != "" ]; then
- echo "${errorMessage}" 1>&2
- fi
- echo "Usage: $0 [-d <delay>] [<remote>] [<branch>]"
- echo "Where:"
- echo " <delay> is the number of seconds delay between steps (default 1)"
- echo " <branch> is the name of the branch to switch to and pull from (default 'master')"
- echo " <remote> is the name of the remote to pull from (default 'upstream')"
- if [ "${errorMessage}" != "" ]; then
- exit 1
- else
- exit 0
- fi
- fi
- # Wait the `delayPeriod` or until user presses ENTER, if a `delayPeriod` is set
- function delay {
- if [ -n "${delayPeriod}" ]; then
- read -t ${delayPeriod} -p "Waiting for ${delayPeriod}s; press ENTER to skip waiting, or CTRL+C to cancel"
- fi
- echo
- }
- # Main processing sequence
- echo "Switching to ${branch}"
- git checkout "${branch}"
- delay
- echo "Fetching data"
- fetchOutput=$( { git fetch -p; } 2>&1 )
- echo "${fetchOutput}"
- delay
- echo "Pulling latest from upstream"
- git pull "${upstream}" "${branch}"
- delay
- echo "Pushing to origin"
- git push
- delay
- echo "Removing dead branches"
- while read -r line; do
- if [[ "${line}" == *"[deleted]"* ]]; then
- deadBranch=$( echo "${line}" | sed "s/^.*\[deleted\].*-> \w*\///" )
- echo "Removing branch: ${deadBranch}"
- git branch -d "${deadBranch}"
- fi
- done <<< "${fetchOutput}"
- echo "Done, displaying branches and status"
- git branch -v
- git status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement