1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
#!/usr/bin/env bash
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2020, deadc0de6
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 >>${logs} 2>&1
}
get_current_commit()
{
git rev-parse --short HEAD
}
# get current tag
get_current_tag()
{
git describe --tags 2>/dev/null
}
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>>${logs} | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g'
}
# list all releases
get_releases()
{
curl "${api}/${pro}/releases" 2>>${logs} | grep 'tag_name' | sed 's/^.*: "\(.*\)",/\1/g'
}
# list all branches
get_branches()
{
curl "${api}/${pro}/branches" 2>>${logs} | grep name | sed 's/^.*: "\(.*\)",/\1/g'
}
########################
## print status
########################
# get current status
get_current()
{
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
# shellcheck disable=SC2001
tag=$(echo "$cur" | sed 's/\(v.*\)-[0-9]*.-.*$/\1/g')
if [ "${tag}" != "${last}" ]; then
echo "new stable version available: ${last}" && exit 1
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
changes=$(git log HEAD..origin --oneline)
[ "${changes}" != "" ] && echo "new updates available" && exit 1
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 && checkout "${last}"
}
# $1: tag
checkout_tag()
{
local cur
cur=$(get_current_tag)
[ "${cur}" = "${1}" ] && return
git_get_changes && checkout "${1}"
}
checkout_branch()
{
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"
cd dotdrop || (echo "cannot change directory to dotdrop" && exit 1)
return
fi
if [ -e dotdrop/version.py ]; then
grep deadc0de6 dotdrop/version.py >/dev/null 2>&1
if [ "$?" ]; then
# dotdrop is in current dir
#echo "dotdrop is in current directory"
return
fi
fi
echo "dotdrop wasn't found"
exit 1
}
# print usage
usage()
{
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 <version> : change to a specific version"
echo "$(basename "${0}") get branch <branch> : change to a specific branch"
exit 1
}
########################
## entry point
########################
[ "$1" = "" ] && usage
[ "$1" != "print" ] && \
[ "$1" != "check" ] && \
[ "$1" != "get" ] && \
usage
[ "$2" = "" ] && usage
move_to_base
if [ "$1" = "print" ]; then
if [ "$2" = "current" ]; then
get_current
elif [ "$2" = "releases" ]; then
get_releases
elif [ "$2" = "branches" ]; then
get_branches
else
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
cd "${curpwd}" || true
exit 0
|