Guest User

Untitled

a guest
Dec 18th, 2017
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #!/bin/zsh
  2. ## Author: Mikhaylov Anton aka anion155 (anion155@gmail.com)
  3. ## version: 0.1
  4. ## zsh-version: 5.2
  5. ##
  6. ## Script to sync one remote to another.
  7. ## Usefull when you are updating fork to master repository.
  8. ##
  9. ## What does script does?
  10. ## 1) Push everything from upstream rep to origin.
  11. ## 2) Delete all non-upstream branches from origin.
  12. ## All deleted branches will be saved as local refs.
  13. ##
  14.  
  15. UPSTREAM=$1
  16. ORIGIN=$2
  17.  
  18. function usage() {
  19. echo "Usage:"
  20. echo "$0 <upstream-remote> <target-remote>"
  21. echo ""
  22. echo "Example which ensures remote named 'maxandersen' have all the same branches and tags as 'origin'"
  23. echo "$0 origin upstream"
  24. exit 1
  25. }
  26.  
  27. if [ -z "$UPSTREAM" -o -z "$ORIGIN" ]; then
  28. echo "Missing arguments.\n"
  29. usage
  30. fi
  31.  
  32. remotes=( `git remote` )
  33. if (( ${remotes[(i)$ORIGIN]} > ${#remotes} || ${remotes[(i)$UPSTREAM]} > ${#remotes} )); then
  34. echo "Remotes isn't exists.\n"
  35. usage
  36. fi
  37. unset remotes
  38.  
  39. function pushUpstream() {
  40. for brname in ${u_branches[@]}; do
  41. git push $ORIGIN $UPSTREAM/$brname:$brname 2>&1 | grep -v 'Everything up-to-date' | grep --color -E '^error:|\[rejected\]|$'
  42. [ ${pipestatus[1]} -ne 0 ] && echo
  43. done
  44. }
  45.  
  46. function deleteOldBranches() {
  47. for brname in ${o_branches[@]}; do
  48. if (( ${u_branches[(i)$brname]} <= ${#u_branches} )); then
  49. continue
  50. fi
  51. if ! $SILENT_DELETE; then
  52. echo -n " Do you want to delete origin branch '$brname', it's not upstream? "
  53. read -k 1 "?(n) "; if [[ $REPLY =~ $'^[^yY]|[\n]$' ]]; then
  54. echo; continue
  55. fi; echo
  56. fi
  57. local commit=`git rev-parse --short $ORIGIN/$brname`
  58. echo -n " Deleting branch '$brname' at '$commit' from origin."
  59. if (( ${l_branches[(i)$brname]} > ${#l_branches} )); then
  60. echo -n " Local copy will be stored at '$brname' branch."
  61. git branch $brname $commit
  62. fi; echo
  63. git push $ORIGIN :$brname >& /dev/null
  64. done
  65. }
  66.  
  67. function main() {
  68. echo "Syncyng '$ORIGIN' to '$UPSTREAM' remotes\n"
  69.  
  70. local l_branches=( `git branch -l | sed 's/^\*//'` )
  71. local o_branches=( `git branch -r | grep -v HEAD | grep $ORIGIN | sed -e "s/$ORIGIN\///"` )
  72. local u_branches=( `git branch -r | grep -v HEAD | grep $UPSTREAM | sed -e "s/$UPSTREAM\///"` )
  73.  
  74. echo "Fetch remotes..."
  75. git fetch $ORIGIN >/dev/null
  76. git fetch $UPSTREAM >/dev/null
  77. echo "done\n"
  78.  
  79. echo "Push branches from upstream to origin..."
  80. pushUpstream
  81. echo "done\n"
  82.  
  83. local SILENT_DELETE=true
  84. echo -n "Delete all non upstream branches from origin..."
  85. read -k 1 "? - Are you sure? (y) "; if ! [[ $REPLY =~ $'^[yY\n]$' ]]; then
  86. SILENT_DELETE=false
  87. fi; echo
  88. deleteOldBranches
  89. echo "done\n"
  90. }
  91.  
  92. main
Add Comment
Please, Sign In to add comment