|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +dir_name=$(dirname $0) |
| 4 | +source ${dir_name}/common.sh |
| 5 | + |
| 6 | +function isValidVersion () { |
| 7 | + echo "$1" | awk '/^[0-9]+\.[0-9]+(\.[0-9]+){0,2}(-rc[0-9]+|-beta[0-9]+)?$/ {print $0}' |
| 8 | +} |
| 9 | + |
| 10 | +function isBetaVersion () { |
| 11 | + echo "$1" | awk '/^[0-9]+\.[0-9]+(\.[0-9]+){0,2}-beta[0-9]+$/ {print $0}' |
| 12 | +} |
| 13 | + |
| 14 | +function isRCVersion () { |
| 15 | + echo "$1" | awk '/^[0-9]+\.[0-9]+(\.[0-9]+){0,2}-rc[0-9]+$/ {print $0}' |
| 16 | +} |
| 17 | + |
| 18 | +function latestVersion () { |
| 19 | + git tag -l | awk '/^0\.[0-9]+(\.[0-9]+){0,2}(-rc[0-9]+|-beta[0-9]+)?$/ { print $0 }' | sort -rV | head -n 1 |
| 20 | +} |
| 21 | + |
| 22 | +function latestNotRCVersion () { |
| 23 | + git tag -l | awk '/^0\.[0-9]+(\.[0-9]+){0,2}(-beta[0-9]+)?$/ { print $0 }' | sort -rV | head -n 1 |
| 24 | +} |
| 25 | + |
| 26 | +function betaNumber () { |
| 27 | + echo "$1" | awk -F 'beta' '{print $2}' |
| 28 | +} |
| 29 | + |
| 30 | +function rcNumber () { |
| 31 | + echo "$1" | awk -F 'rc' '{print $2}' |
| 32 | +} |
| 33 | + |
| 34 | +function gitRepoModified () { |
| 35 | + git status --short | awk '/^[ M][ M] / { print $0 }' |
| 36 | +} |
| 37 | + |
| 38 | +function increaseBetaVersion () { |
| 39 | + if [ $(isRCVersion "$1") ]; then |
| 40 | + # Unable to increase beta version number on a RC version, exit. |
| 41 | + return 1 |
| 42 | + fi |
| 43 | + if [ $(isBetaVersion "$1") ]; then |
| 44 | + echo "$1" | awk -F 'beta' '{printf("%sbeta%d\n", $1, $2+1)}' |
| 45 | + else |
| 46 | + echo "$1" | awk -F '.' '/^([0-9]+\.){1,3}([0-9]+)$/ { |
| 47 | + for(i=1;i<=NF;i++) { |
| 48 | + if (i==NF) { |
| 49 | + printf("%d-beta1\n", $i+1) |
| 50 | + } else { |
| 51 | + printf("%d.", $i) |
| 52 | + } |
| 53 | + } |
| 54 | + }' |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +function increaseRCVersion () { |
| 59 | + if [ $(isBetaVersion "$1") ]; then |
| 60 | + # Unable to increase rc version number on a beta version, exit. |
| 61 | + return 1 |
| 62 | + fi |
| 63 | + if [ $(isRCVersion "$1") ]; then |
| 64 | + echo "$1" | awk -F 'rc' '{printf("%src%d\n", $1, $2+1)}' |
| 65 | + else |
| 66 | + echo "$1" | awk -F '.' '/^([0-9]+\.){1,3}([0-9]+)$/ { |
| 67 | + for(i=1;i<=NF;i++) { |
| 68 | + if (i==NF) { |
| 69 | + printf("%d-rc1\n", $i+1) |
| 70 | + } else { |
| 71 | + printf("%d.", $i) |
| 72 | + } |
| 73 | + } |
| 74 | + }' |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +read -e -p "Your upstream git remote is? [origin]" |
| 79 | +remote=${REPLY:="origin"} |
| 80 | + |
| 81 | +info "Fetching latest changes and prune ${LGREEN}$remote${RESTORE}..." |
| 82 | +git fetch -p -t "$remote" |
| 83 | +if [ $? ]; then |
| 84 | + error "It is not able to fetch codes from your $remote, check if there is any issue." |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | +git remote prune "$remote" |
| 88 | +info "${GREEN}Done.${RESTORE}" |
| 89 | + |
| 90 | +latest_ver=$(latestNotRCVersion) |
| 91 | +beta_ver="0.0.1-beta1" |
| 92 | +if [ -z "$latest_ver" ]; then |
| 93 | + echo "Use ${YELLOW}$beta_ver${RESTORE} as the version number." |
| 94 | +else |
| 95 | + beta_ver=$(increaseBetaVersion "$latest_ver") |
| 96 | +fi |
| 97 | + |
| 98 | +beta_branch="$beta_ver-$(date "+%y%m%d")" |
| 99 | + |
| 100 | +info "\nThe latest non-RC version is: ${CYAN}'$latest_ver'${RESTORE}" |
| 101 | +info "This beta version number should be: ${CYAN}'$beta_ver'${RESTORE}" |
| 102 | +info "The beta release branch name should be: ${CYAN}'$beta_branch'${RESTORE}." |
| 103 | + |
| 104 | +is_remote_branch_exist=$(git branch -r | awk '/'"$beta_ver"'/ {print $0}') |
| 105 | +if [ "$is_remote_branch_exist" ]; then |
| 106 | + warning "Remote branch with beta version $beta_ver already exists, will quit." |
| 107 | + exit 1 |
| 108 | +fi |
| 109 | + |
| 110 | +latest_sha=$(git show --format=%h -s) |
| 111 | +notice "\nMake sure the latest commit SHA on master is:" |
| 112 | +notice "${GREEN}$latest_sha${RESTORE}" |
| 113 | +notice "If it is not the branch-off point you want, update the $remote/master first." |
| 114 | +notice "Also make sure you have the permisison to push branch onto your $remote." |
| 115 | + |
| 116 | +read -e -p "Continue? [Y/n] " -n 1 |
| 117 | +shouldContinue=${REPLY:=y} |
| 118 | +if [ ${shouldContinue,,} = n ]; then |
| 119 | + info "Sure, bye." |
| 120 | + exit 0 |
| 121 | +fi |
| 122 | + |
| 123 | +if [ "$(gitRepoModified)" ]; then |
| 124 | + read -e -p "Your working directory is modified, stash it? [Y/n]" -n 1 |
| 125 | + shouldStash=${REPLY:=y} |
| 126 | + if [ ${shouldStash,,} = y ]; then |
| 127 | + git stash |
| 128 | + else |
| 129 | + info "Change ignored." |
| 130 | + fi |
| 131 | +fi |
| 132 | + |
| 133 | +echo -e "\nCreating local branch: ${YELLOW}$beta_branch${RESTORE}..." |
| 134 | +git checkout -B "$beta_branch" "$latest_sha" |
| 135 | + |
| 136 | +echo -e "\nPushing ${YELLOW}$beta_branch${RESTORE} onto $remote..." |
| 137 | +git push "$remote" "$beta_branch":"$beta_branch" |
| 138 | +git branch --set-upstream-to="$remote/$beta_branch" |
| 139 | +echo "${GREEN}Done.${RESTORE}" |
0 commit comments