From c021e0cd0a3706fc70ba1cc66f7af9f8c48414e7 Mon Sep 17 00:00:00 2001 From: deadc0de6 Date: Sat, 12 Dec 2020 15:28:55 +0100 Subject: [PATCH] update version checker --- scripts/dotdrop-version-manager.sh | 207 ++++++++++++++++++++--------- 1 file changed, 145 insertions(+), 62 deletions(-) diff --git a/scripts/dotdrop-version-manager.sh b/scripts/dotdrop-version-manager.sh index 5189513..d6f6a00 100755 --- a/scripts/dotdrop-version-manager.sh +++ b/scripts/dotdrop-version-manager.sh @@ -4,26 +4,27 @@ api="https://api.github.com/repos" pro="deadc0de6/dotdrop" +logs="/tmp/dotdrop-version.log" + +######################## +## git operations +######################## git_get_changes() { #git fetch origin - git remote update + git remote update >>${logs} 2>&1 } -# get current status -get_current() +get_current_commit() { - tag=$(get_current_tag) - [ "$tag" != "" ] && echo "tag: ${tag}" - branch=$(get_current_branch) - [ "$branch" != "" ] && echo "branch: ${branch}" + git rev-parse --short HEAD } # get current tag get_current_tag() { - git describe --tags --long 2>/dev/null + git describe --tags 2>/dev/null } get_current_branch() @@ -31,80 +32,139 @@ get_current_branch() git branch --show-current 2>/dev/null } +is_on_release() +{ + git describe --exact-match --tags HEAD >>${logs} 2>&1 + echo "$?" +} + +checkout() +{ + git checkout "$1" >>${logs} 2>&1 +} + +######################## +## api operations +######################## + # get latest release get_latest() { - curl "${api}/${pro}/releases/latest" 2>/dev/null | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g' + curl "${api}/${pro}/releases/latest" 2>>${logs} | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g' } # list all releases get_releases() { - curl "${api}/${pro}/releases" 2>/dev/null | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g' + curl "${api}/${pro}/releases" 2>>${logs} | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g' } # list all branches get_branches() { - curl "${api}/${pro}/branches" 2>/dev/null | grep name | sed 's/^.*: "\(.*\)",/\1/g' + curl "${api}/${pro}/branches" 2>>${logs} | grep name | sed 's/^.*: "\(.*\)",/\1/g' } -need_update() +######################## +## print status +######################## + +# get current status +get_current() { - git fetch origin >/dev/null 2>&1 + local tag + local commit + local branch + echo "current version:" + stable=$(is_on_release) + if [ "${stable}" = "0" ]; then + echo -e "\ton stable" + tag=$(get_current_tag) + echo -e "\trelease version: ${tag}" + else + echo -e "\ton unstable" + tag=$(get_current_tag) + echo -e "\ttag: ${tag}" + commit=$(get_current_commit) + echo -e "\tcommit: ${commit}" + fi + + branch=$(get_current_branch) + [ "$branch" != "" ] && echo -e "\tbranch: ${branch}" +} + + +# check if new stable release is available +need_update_stable() +{ + local last + local cur + local tag + git fetch origin >>${logs} 2>&1 last=$(get_latest) cur=$(get_current_tag) # get short tag if on a lightweight tag - tag=$(echo "$cur" | sed 's/\(v.*\)-[0-9]*.-.*$/\1/g') - nb=$(echo "$cur" | sed 's/v.*-\([0-9]*\)-.*$/\1/g') - commit=$(echo "$cur" | sed 's/v.*-[0-9]*-\(.*\)$/\1/g') - - if [ "${commit}" != "" ]; then - echo "you are on ${tag} commit ${commit}" - else - echo "you are on ${cur}" + if [ "${tag}" != "${last}" ]; then + echo "new stable version available: ${last}" && return fi + echo "your version is up-to-date" +} + +# check if new updates are available +need_update() +{ + local changes + git fetch origin >>${logs} 2>&1 # compare - if [ "${tag}" != "${last}" ]; then - echo "new version available: ${last}" && return - fi - changes=$(git status -s) + changes=$(git log HEAD..origin --oneline) [ "${changes}" != "" ] && echo "new updates available" && return echo "your version is up-to-date" } +######################## +## change operations +######################## + checkout_last_tag() { + local last + local cur last=$(get_latest) [ "${last}" = "" ] && echo "unable to get last release" && return cur=$(get_current_tag) [ "${cur}" = "${last}" ] && return - git_get_changes && git checkout "${last}" + git_get_changes && checkout "${last}" } # $1: tag checkout_tag() { + local cur cur=$(get_current_tag) [ "${cur}" = "${1}" ] && return - git_get_changes && git checkout "${1}" + git_get_changes && checkout "${1}" } checkout_branch() { - git_get_changes && git checkout "${1}" && git pull origin "${1}" + git_get_changes && checkout "${1}" && git pull origin "${1}" >/dev/null 2>&1 } +######################## +## helpers +######################## + # move to base of dotdrop move_to_base() { + local curpwd curpwd=$(pwd) git submodule | grep dotdrop >/dev/null 2>&1 if [ "$?" ]; then # dotdrop is a submodule - echo "dotdrop used as a submodule" + #echo "dotdrop used as a submodule" cd dotdrop || (echo "cannot change directory to dotdrop" && exit 1) return fi @@ -113,7 +173,7 @@ move_to_base() grep deadc0de6 dotdrop/version.py >/dev/null 2>&1 if [ "$?" ]; then # dotdrop is in current dir - echo "dotdrop is in current directory" + #echo "dotdrop is in current directory" return fi fi @@ -125,45 +185,68 @@ move_to_base() # print usage usage() { - echo "$(basename "${0}") current : current version tracked" - echo "$(basename "${0}") releases : list all releases" - echo "$(basename "${0}") branches : list all branches" - echo "$(basename "${0}") check : check for updates" - echo "$(basename "${0}") get latest : get latest stable release" - echo "$(basename "${0}") get master : get master branch" - echo "$(basename "${0}") get : get a specific version" - echo "$(basename "${0}") get branch : get a specific version" + echo "$(basename "${0}") print current : print the version you are on" + echo "$(basename "${0}") print releases : list available stable releases" + echo "$(basename "${0}") print branches : list available branches" + echo "$(basename "${0}") check unstable : check for new unstable updates" + echo "$(basename "${0}") check stable : check for new stable release" + echo "$(basename "${0}") get stable : change to latest stable release" + echo "$(basename "${0}") get unstable : change to latest unstable" + echo "$(basename "${0}") get version : change to a specific version" + echo "$(basename "${0}") get branch : change to a specific branch" + exit 1 } -[ "$1" = "" ] && usage && exit 1 -[ "$1" != "current" ] && \ - [ "$1" != "releases" ] && \ - [ "$1" != "get" ] && \ - [ "$1" != "branches" ] && \ +######################## +## entry point +######################## + +[ "$1" = "" ] && usage +[ "$1" != "print" ] && \ [ "$1" != "check" ] && \ - usage && exit 1 -[ "$1" = "get" ] && [ "$2" = "" ] && usage && exit 1 -[ "$1" = "get" ] && [ "$2" = "branch" ] && [ "$3" = "" ] && usage && exit 1 + [ "$1" != "get" ] && \ + usage +[ "$2" = "" ] && usage move_to_base -if [ "$1" = "current" ]; then - get_current -elif [ "$1" = "releases" ]; then - get_releases -elif [ "$1" = "branches" ]; then - get_branches -elif [ "$1" = "check" ]; then - need_update -elif [ "$1" = "get" ]; then - if [ "$2" = "latest" ]; then - checkout_last_tag - elif [ "$2" = "master" ]; then - checkout_branch master - elif [ "$2" = "branch" ]; then - checkout_branch "${3}" +if [ "$1" = "print" ]; then + if [ "$2" = "current" ]; then + get_current + elif [ "$2" = "releases" ]; then + get_releases + elif [ "$2" = "branches" ]; then + get_branches else - checkout_tag "${2}" + usage + fi +elif [ "$1" = "check" ]; then + if [ "$2" = "stable" ]; then + get_current + need_update_stable + elif [ "$2" = "unstable" ]; then + get_current + need_update + else + usage + fi +elif [ "$1" = "get" ]; then + if [ "$2" = "stable" ]; then + checkout_last_tag + get_current + elif [ "$2" = "unstable" ]; then + checkout_branch master + get_current + elif [ "$2" = "branch" ]; then + [ "$3" = "" ] && usage + checkout_branch "${3}" + get_current + elif [ "$2" = "version" ]; then + [ "$3" = "" ] && usage + checkout_tag "${3}" + get_current + else + usage fi fi