Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # git-update
  4. # Pull down latest from git remote and remove dead branches
  5. #
  6. # To create an alias to `git update`, put this on your path, then:
  7. # git config --global alias.update '!git-update'
  8. #
  9. # Copyright (c) 2016 Leftclick.com.au
  10. # License: MIT
  11. #
  12.  
  13. # Defaults
  14. branch=master
  15. upstream=upstream
  16. delayPeriod=
  17. displayUsage=false
  18. errorMessage=
  19.  
  20. # Parse options
  21. while getopts d:h param; do
  22. case $param in
  23. d)
  24. delayPeriod="$OPTARG"
  25. ;;
  26. h)
  27. displayUsage=true
  28. ;;
  29. *)
  30. errorMessage="Unknown argument, please check usage instructions"
  31. displayUsage=true
  32. ;;
  33. esac
  34. done
  35. shift $(expr $OPTIND - 1)
  36.  
  37. # Check for too many arguments
  38. if [ $# -gt 2 ]; then
  39. errorMessage="Too many arguments, please check usage instructions"
  40. displayUsage=true
  41. fi
  42.  
  43. # Display usage if requested or arguments are invalid
  44. if [ "${displayUsage}" = true ]; then
  45. if [ "${errorMessage}" != "" ]; then
  46. echo "${errorMessage}" 1>&2
  47. fi
  48. echo "Usage: $0 [-d <delay>] [<remote>] [<branch>]"
  49. echo "Where:"
  50. echo " <delay> is the number of seconds delay between steps (default 1)"
  51. echo " <branch> is the name of the branch to switch to and pull from (default 'master')"
  52. echo " <remote> is the name of the remote to pull from (default 'upstream')"
  53. if [ "${errorMessage}" != "" ]; then
  54. exit 1
  55. else
  56. exit 0
  57. fi
  58. fi
  59.  
  60. # Wait the `delayPeriod` or until user presses ENTER, if a `delayPeriod` is set
  61. function delay {
  62. if [ -n "${delayPeriod}" ]; then
  63. read -t ${delayPeriod} -p "Waiting for ${delayPeriod}s; press ENTER to skip waiting, or CTRL+C to cancel"
  64. fi
  65. echo
  66. }
  67.  
  68. # Main processing sequence
  69. echo "Switching to ${branch}"
  70. git checkout "${branch}"
  71. delay
  72.  
  73. echo "Fetching data"
  74. fetchOutput=$( { git fetch -p; } 2>&1 )
  75. echo "${fetchOutput}"
  76. delay
  77.  
  78. echo "Pulling latest from upstream"
  79. git pull "${upstream}" "${branch}"
  80. delay
  81.  
  82. echo "Pushing to origin"
  83. git push
  84. delay
  85.  
  86. echo "Removing dead branches"
  87. while read -r line; do
  88. if [[ "${line}" == *"[deleted]"* ]]; then
  89. deadBranch=$( echo "${line}" | sed "s/^.*\[deleted\].*-> \w*\///" )
  90. echo "Removing branch: ${deadBranch}"
  91. git branch -d "${deadBranch}"
  92. fi
  93. done <<< "${fetchOutput}"
  94.  
  95. echo "Done, displaying branches and status"
  96. git branch -v
  97. git status
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement