1#!/bin/sh 2 3# Parse git version into its separate components. 4# 5# Copyright (C) 2017 Robert Krawitz 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 2, or (at your option) 10# any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <https://www.gnu.org/licenses/>. 19 20 21usage() { 22 echo "Usage: $cmd [info] [default] [version]" 23 echo " info can be empty, or one of major, minor, micro, extra, git, hash, pkg" 24 echo " info can also be extra-git, core-version (major.minor)," 25 echo " base-version (major.minor.micro)," 26 echo " full-version (major.minor.micro[-extra]), or all" 27 echo " default is the default value if not in a git repository." 28 echo " version is the input version in lieu of git describe (for testing)" 29 exit 1 30} 31 32cmd="$0" 33 34if [ "$#" -gt 3 ] ; then 35 usage 36fi 37 38root="/home/rlk/sandbox/gimp-print-source" 39gutenprint_version=gutenprint-5.3.3 40gutenprint_base=gutenprint-5.3.3 41gutenprint_release=gutenprint-5.3 42 43if [ -z "$3" ] ; then 44 if [ -d "$root/.git" ] ; then 45 tag=$(git describe --tags --dirty --first-parent --candidates=1000 --match "${gutenprint_version//./_}*" 2>/dev/null) 46 [[ -z $tag ]] && tag=$(git describe --tags --dirty --first-parent --candidates=1000 --match "${gutenprint_base//./_}*" 2>/dev/null) 47 [[ -z $tag ]] && tag=$(git describe --tags --dirty --first-parent --candidates=1000 --match "${gutenprint_release//./_}*" 2>/dev/null) 48 [[ -z $tag ]] && tag=$(git describe --tags --dirty --first-parent --candidates=1000 --match "gutenprint*" 2>/dev/null) 49 [[ -z $tag ]] && tag=$(git describe --tags --dirty --always --first-parent 2>/dev/null) 50 echo $tag | sed 's/^[^0-9]*-//' > "$root/git-version-stamp" 51 fi 52 53 if [ -f "$root/git-version-stamp" ] ; then 54 # shellcheck disable=SC2006 55 description=`cat "$root/git-version-stamp"` 56 else 57 description='(unknown)' 58 fi 59else 60 description=$3 61fi 62 63# If we don't have git or this is not a repository, simply return the default value 64if [ -z "$description" ] ; then 65 echo "$2" 66 exit 0 67fi 68 69xed() { 70 if [ -n "$PREFIX" ] ; then 71 # shellcheck disable=SC1117 72 sed -e h -e "s/$/ $PREFIX => /" -e x -e 's/^[^0-9]*-//' "$@" -e H -e x -e "s/\n//" 73 else 74 sed -e 's/^[^0-9]*-//' "$@" 75 fi 76} 77 78get_major() { 79 xed -e 's/[-_\.].*//' 80} 81 82get_minor() { 83 xed -e 's/^[0-9]*[-_\.]//' \ 84 -e 's/[-_\.].*//' 85} 86 87get_micro() { 88 xed -e 's/^[0-9]*[-_\.][0-9]*[-_\.]//' \ 89 -e 's/[-_\.].*//' 90} 91 92get_extra() { 93 xed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \ 94 -e 's/-\([0-9]*-\|\)g[0-9a-zA-Z]*\(-dirty\|\)$//' \ 95 -e 's/^_/-/' 96} 97 98get_git() { 99 xed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \ 100 -e 's/^[-_\.][a-zA-Z][a-zA-Z]*[0-9]*\([a-zA-Z]\|\)//' \ 101 -e 's/^[-_\.]/-/' \ 102 -e 's/-0-g[0-9a-f]*\(-dirty\|\)//' 103} 104 105get_git_hash() { 106 xed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \ 107 -e 's/^[-_\.][a-zA-Z][a-zA-Z]*[0-9]*\([a-zA-Z]\|\)//' \ 108 -e 's/^[-_\.]/-/' \ 109 -e 's/^-[0-9]*-g//' \ 110 -e 's/-dirty$//' 111} 112 113get_extra_git() { 114 xed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \ 115 -e 's/^[-_\.]/-/' \ 116 -e 's/-0-g[0-9a-f]*\(-dirty\|\)$/\1/' 117} 118 119get_core_version() { 120 xed -e 's/^\([0-9]*[-_\.][0-9]*\).*/\1/' \ 121 -e 's/[-_\.]/./g' 122} 123 124get_base_version() { 125 xed -e 's/^\([0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)\).*/\1/' \ 126 -e 's/[-_\.]/./g' 127} 128 129get_full_version() { 130 xed -e 's/-[0-9]*-g[0-9a-z]*\(-dirty\|\)$//' \ 131 -e 's/[-_\.]/=/g' \ 132 -e 's/=/./' \ 133 -e 's/=/./' \ 134 -e 's/=/-/' 135} 136 137get_all() { 138 xed -e 's/[-_\.]/=/g' \ 139 -e 's/=/./' \ 140 -e 's/=/./' \ 141 -e 's/=/-/g' \ 142 -e 's/-0-g[0-9a-f]*\(-dirty\|\)$/\1/' 143} 144 145get_everything() { 146 xed -e 's/[-_\.]/=/g' \ 147 -e 's/=/./' \ 148 -e 's/=/./' \ 149 -e 's/=/-/g' 150} 151 152get_pkg() { 153 echo "$gutenprint_version" 154} 155 156doit() { 157 case "$1" in 158 major) get_major ;; 159 minor) get_minor ;; 160 micro) get_micro ;; 161 extra) get_extra ;; 162 git) get_git ;; 163 git-hash) get_git_hash ;; 164 extra-git) get_extra_git ;; 165 core-version) get_core_version ;; 166 base-version) get_base_version ;; 167 full-version) get_full_version ;; 168 ''|all) get_all ;; 169 everything) get_everything ;; 170 pkg) get_pkg ;; 171 *) usage ;; 172 esac 173} 174 175case "$description" in 176 -) doit "$1" ;; 177 --) PREFIX="$1" doit "$1" ;; 178 *) echo "$description" | doit "$1" ;; 179esac 180 181exit 0 182