summaryrefslogtreecommitdiff
path: root/scripts/gversion
blob: eda0dc05163a75fdc1198e388439c19609f874c7 (plain)
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
#!/bin/sh

# Parse git version into its separate components.
#
# Copyright (C) 2017 Robert Krawitz
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


usage() {
    echo "Usage: $cmd [info] [default] [version]"
    echo "       info can be empty, or one of major, minor, micro, extra, git"
    echo "       info can also be extra-git, core-version (major.minor),"
    echo "            base-version (major.minor.micro),"
    echo "            full-version (major.minor.micro[-extra]), or all"
    echo "       default is the default value if not in a git repository."
    echo "       version is the input version in lieu of git describe (for testing)"
    exit 1
}

cmd="$0"

if [ "$#" -gt 3 ] ; then
    usage
fi

root="/home/rlk/sandbox/gutenprint-5.2"

if [ -z "$3" ] ; then
    if [ -d "$root/.git" ] ; then
	git describe --dirty --always --first-parent 2>/dev/null | sed 's/^[^0-9]*-//' > "$root/git-version-stamp"
    fi

    if [ -f "$root/git-version-stamp" ] ; then
	description=`cat "$root/git-version-stamp"`
    else
	description='(unknown)'
    fi
else
    description=$3
fi

# If we don't have git or this is not a repository, simply return the default value
if [ -z "$description" ] ; then
    echo "$2"
    exit 0
fi

get_major() {
    echo "$1" | sed -e 's/[-_\.].*//'
}

get_minor() {
    echo "$1" | sed -e 's/^[0-9]*[-_\.]//' \
		    -e 's/[-_\.].*//'
}

get_micro() {
    echo "$1" | sed -e 's/^[0-9]*[-_\.][0-9]*[-_\.]//' \
		    -e 's/[-_\.].*//'
}

get_extra() {
    echo "$1" | sed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \
		    -e 's/-\([0-9]*-\|\)g[0-9a-zA-Z]*\(-dirty\|\)$//' \
    		    -e 's/^_/-/'
}

get_git() {
    echo "$1" | sed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \
		    -e 's/^[-_\.][a-zA-Z][a-zA-Z]*[0-9]*\([a-zA-Z]\|\)//' \
		    -e 's/^[-_\.]/-/'
}

get_extra_git() {
    echo "$1" | sed -e 's/^[0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)//' \
		    -e 's/^[-_\.]/-/'
}

get_core_version() {
    echo "$1" | sed -e 's/^\([0-9]*[-_\.][0-9]*\).*/\1/' \
		    -e 's/[-_\.]/./g'
}

get_base_version() {
    echo "$1" | sed -e 's/^\([0-9]*[-_\.][0-9]*[-_\.][0-9]*\([a-zA-Z]\|\)\).*/\1/' \
		    -e 's/[-_\.]/./g'
}

get_full_version() {
    echo "$1" | sed -e 's/-[0-9]*-g[0-9a-z]*\(-dirty\|\)$//' \
		    -e 's/[-_\.]/=/g' \
		    -e 's/=/./' \
		    -e 's/=/./' \
		    -e 's/=/-/'
}

get_all() {
    echo "$1" | sed -e 's/[-_\.]/=/g' \
		    -e 's/=/./' \
		    -e 's/=/./' \
		    -e 's/=/-/g'
}

case "$1" in
    major)        get_major        "$description" ;;
    minor)        get_minor        "$description" ;;
    micro)        get_micro        "$description" ;;
    extra)        get_extra        "$description" ;;
    git)          get_git          "$description" ;;
    extra-git)    get_extra_git    "$description" ;;
    core-version) get_core_version "$description" ;;
    base-version) get_base_version "$description" ;;
    full-version) get_full_version "$description" ;;
    ''|all)       get_all          "$description" ;;
    *)            usage                           ;;
esac

exit 0