upgrade.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env zsh
  2. # Protect against running with shells other than zsh
  3. if [ -z "$ZSH_VERSION" ]; then
  4. exec zsh "$0" "$@"
  5. fi
  6. # Protect against unwanted sourcing
  7. case "$ZSH_EVAL_CONTEXT" in
  8. *:file) echo "error: this file should not be sourced" && return ;;
  9. esac
  10. cd "$ZSH"
  11. # Use colors, but only if connected to a terminal
  12. # and that terminal supports them.
  13. setopt typeset_silent
  14. typeset -a RAINBOW
  15. typeset RED GREEN YELLOW BLUE BOLD DIM UNDER RESET
  16. if [ -t 1 ]; then
  17. RAINBOW=(
  18. "$(printf '\033[38;5;196m')"
  19. "$(printf '\033[38;5;202m')"
  20. "$(printf '\033[38;5;226m')"
  21. "$(printf '\033[38;5;082m')"
  22. "$(printf '\033[38;5;021m')"
  23. "$(printf '\033[38;5;093m')"
  24. "$(printf '\033[38;5;163m')"
  25. )
  26. RED=$(printf '\033[31m')
  27. GREEN=$(printf '\033[32m')
  28. YELLOW=$(printf '\033[33m')
  29. BLUE=$(printf '\033[34m')
  30. BOLD=$(printf '\033[1m')
  31. DIM=$(printf '\033[2m')
  32. UNDER=$(printf '\033[4m')
  33. RESET=$(printf '\033[0m')
  34. fi
  35. # Update upstream remote to ohmyzsh org
  36. git remote -v | while read remote url extra; do
  37. case "$url" in
  38. https://github.com/robbyrussell/oh-my-zsh(|.git))
  39. git remote set-url "$remote" "https://github.com/ohmyzsh/ohmyzsh.git"
  40. break ;;
  41. git@github.com:robbyrussell/oh-my-zsh(|.git))
  42. git remote set-url "$remote" "git@github.com:ohmyzsh/ohmyzsh.git"
  43. break ;;
  44. esac
  45. done
  46. # Set git-config values known to fix git errors
  47. # Line endings (#4069)
  48. git config core.eol lf
  49. git config core.autocrlf false
  50. # zeroPaddedFilemode fsck errors (#4963)
  51. git config fsck.zeroPaddedFilemode ignore
  52. git config fetch.fsck.zeroPaddedFilemode ignore
  53. git config receive.fsck.zeroPaddedFilemode ignore
  54. # autostash on rebase (#7172)
  55. resetAutoStash=$(git config --bool rebase.autoStash 2>/dev/null)
  56. git config rebase.autoStash true
  57. local ret=0
  58. # repository settings
  59. remote=${"$(git config --local oh-my-zsh.remote)":-origin}
  60. branch=${"$(git config --local oh-my-zsh.branch)":-master}
  61. # repository state
  62. last_head=$(git symbolic-ref --quiet --short HEAD || git rev-parse HEAD)
  63. # checkout update branch
  64. git checkout -q "$branch" -- || exit 1
  65. # branch commit before update (used in changelog)
  66. last_commit=$(git rev-parse "$branch")
  67. # Update Oh My Zsh
  68. printf "${BLUE}%s${RESET}\n" "Updating Oh My Zsh"
  69. if git pull --rebase --stat $remote $branch; then
  70. # Check if it was really updated or not
  71. if [[ "$(git rev-parse HEAD)" = "$last_commit" ]]; then
  72. message="Oh My Zsh is already at the latest version."
  73. else
  74. message="Hooray! Oh My Zsh has been updated!"
  75. # Save the commit prior to updating
  76. git config oh-my-zsh.lastVersion "$last_commit"
  77. # Print changelog to the terminal
  78. if [[ "$1" = --interactive ]]; then
  79. "$ZSH/tools/changelog.sh" HEAD "$last_commit"
  80. fi
  81. printf "${BLUE}%s \`${BOLD}%s${RESET}${BLUE}\`${RESET}\n" "You can see the changelog with" "omz changelog"
  82. fi
  83. printf '%s %s__ %s %s %s %s %s__ %s\n' $RAINBOW $RESET
  84. printf '%s ____ %s/ /_ %s ____ ___ %s__ __ %s ____ %s_____%s/ /_ %s\n' $RAINBOW $RESET
  85. printf '%s / __ \%s/ __ \ %s / __ `__ \%s/ / / / %s /_ / %s/ ___/%s __ \ %s\n' $RAINBOW $RESET
  86. printf '%s/ /_/ /%s / / / %s / / / / / /%s /_/ / %s / /_%s(__ )%s / / / %s\n' $RAINBOW $RESET
  87. printf '%s\____/%s_/ /_/ %s /_/ /_/ /_/%s\__, / %s /___/%s____/%s_/ /_/ %s\n' $RAINBOW $RESET
  88. printf '%s %s %s %s /____/ %s %s %s %s\n' $RAINBOW $RESET
  89. printf '\n'
  90. printf "${BLUE}%s${RESET}\n" "$message"
  91. printf "${BLUE}${BOLD}%s ${UNDER}%s${RESET}\n" "To keep up with the latest news and updates, follow us on Twitter:" "https://twitter.com/ohmyzsh"
  92. printf "${BLUE}${BOLD}%s ${UNDER}%s${RESET}\n" "Want to get involved in the community? Join our Discord:" "https://discord.gg/ohmyzsh"
  93. printf "${BLUE}${BOLD}%s ${UNDER}%s${RESET}\n" "Get your Oh My Zsh swag at:" "https://shop.planetargon.com/collections/oh-my-zsh"
  94. else
  95. ret=$?
  96. printf "${RED}%s${RESET}\n" 'There was an error updating. Try again later?'
  97. fi
  98. # go back to HEAD previous to update
  99. git checkout -q "$last_head" --
  100. # Unset git-config values set just for the upgrade
  101. case "$resetAutoStash" in
  102. "") git config --unset rebase.autoStash ;;
  103. *) git config rebase.autoStash "$resetAutoStash" ;;
  104. esac
  105. # Exit with `1` if the update failed
  106. exit $ret