summaryrefslogtreecommitdiff
path: root/bump-version.sh
blob: 02846125f835d9cbadf93c609a523892e1085895 (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
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
#!/bin/sh
# -*- ENCODING: UTF-8 -*-
# bump-version.sh - script name 
# a script to update version numbers in git projects
# Author: ziggys
# This piece of software is released to Public Domain

# scriptinfo
SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME%%.sh}"
VERSION="0.0.2"                                   # usage
AUTHOR="ziggys"
LICENSE="Public Domain"

# usage
usage () {
test "$#" = 0 || printf "%s %s\\n" "${SCRIPTNAME}" "$@"
cat << EOF | fold -s
${SCRIPTNAME} version ${VERSION}
  by ${AUTHOR}
  License: ${LICENSE}

${SCRIPTNAME} is a shellscript to bump version numbers in git projects.

Usage:  ${SCRIPTNAME} [option] <args>
Usage:  ${SCRIPTNAME} [-p|-u|-r] version_file -m "message"
   or:  ${SCRIPTNAME} -h

Options
   -p     version file     Bump to new patch version
   -u     version_file     Bump to new minor/updated version
   -r     version_file     Bump to new mayor release version
   -h      HELP            Print this help and exit

For more details about this script go to
Official Repository: https://git.p4g.club/git/${SCRIPTNAME}.git
Mirror Repository: https://gitgud.io/ziggys/${SCRIPTNAME}.git
EOF
exit 0 
}

# use a file to describe new version
describe_usefile () {
DESCRIBEF="$(mktemp -p "${TMPDIR}")"
printf "\\n/* Write a version  description, save the file and close %s\\n \
  /* Description  will be added to VERSION notice in %s\\n \
  /* Lines starting with '/*' will be ignored\\n \
  /* Leave it 'blank' if you won't like a version description\\n" \
  "${EDITOR:=vim}" "${VFILE}" > "${DESCRIBEF}"

  sed -i 's/^ *//g' "${DESCRIBEF}"
  ${EDITOR} "${DESCRIBEF}"
  DESCRIPTION="$(sed '/^\/\*/ d' "${DESCRIBEF}")"

  test -z "${DESCRIPTION}" \
    && printf "Version will be described from default" \
    && DESCRIPTION="version ${NEXT}"
}

# obtain current version
TMPDIR="$(mktemp -d)"
VFILE="$2"
CURRENT="$(sed '/^VERSION=/ !d; s/VERSION="//; s/".*$//' "${VFILE}")"

if [ -z "${CURRENT}" ]; then
  printf "%s couldn't find version number in %s\\n" "${SCRIPTNAME}" "${VFILE}"
  printf "Please, edit %s or use a file containing a valid string VERSION=\"x.y.z\"" "${VFILE}"
  exit 0
else
  printf "Current version is: %s\\n" "${CURRENT}"
fi

# calculate new version
case "$1" in
  -p | --patch)
    NEXT="$(echo "${CURRENT}" | \
      awk '{split($0,a,".");
        a[3]++;
        b=a[1]"."a[2]"."a[3]}
        END{OFS="."; print b}
        ')"

    printf "Patch version will be: %s\\n" "${NEXT}"
    ;;

  -u | --update)
    NEXT="$(echo "${CURRENT}" | \
      awk '{split($0,a,".");
        a[2]++;
        b=a[1]"."a[2]"."0}
        END{OFS="."; print b}
        ')"

    printf "Update version will be: %s\\n" "${NEXT}"
    ;;

  -r | --release)
    NEXT="$(echo "${CURRENT}" | \
      awk '{split($0,a,".");
        a[1]++;
        b=a[1]"."0"."0}
        END{OFS="."; print b}
        ')"

    NTAG="v${NEXT}"
    
    printf "Release version will be: %s\\n" "${NEXT}"
    printf "Release tag will be: %s\\n" "${NTAG}"
    ;;
  
  -c | --custom)
    printf "Please, provide a valid version number (x.y.z): "
    read -r CNEXT

    NEXT="${CNEXT}"
    printf "Next version will be: %s\\n" "${NEXT}"
    ;;
  
  *) usage "$@" ;;
esac

# describe new version for commit and tag 
case "$3" in
  -m | --message ) DESCRIPTION="$4" ;;
  *) describe_usefile ;;
esac

# bump version
sed -i '/^VERSION=/ s/'"${CURRENT}"'/'"${NEXT}"'/' "${VFILE}" 
sed -i '/^VERSION=/ s/# .*$/# '"${DESCRIPTION}"'/' "${VFILE}"

# commit changes
printf "Would you like to commit changes in %s? (yes/all/no): " "${VFILE}"
read -r CCOMMIT

case "${CCOMMIT}" in
  y | yes | Y | YES | Yes )
    printf "\\nAdding %s to stage and commiting changes" "${VFILE}"
    git add "${VFILE}"
    git commit -m "${DESCRIPTION}"
    ISTAG="${NTAG}"
    ;;
  a | all | A | ALL | All )
    FILES="$(find . | grep -v "git" | sed 's/.\///g; 1 d')"
    for file in $FILES; do
      sed -i '/^VERSION=/ s/'"${CURRENT}"'/'"${NEXT}"'/' "${file}" 
      sed -i '/^VERSION=/ s/# .*$/# '"${DESCRIPTION}"'/' "${file}"
    done

    printf "\\nAdding\\n%s\\nto stage and commiting changes\\n" "${FILES}"
    git add .
    git commit -m "${DESCRIPTION}"
    ISTAG="${NTAG}"
    ;;
  n | no | N | NO | No )
    printf "\\nNo files will be added to stage nor commited"
    ;;
esac

# tag if needed
if [ ! -z "${ISTAG}" ]; then
  CHASH="$(git log --pretty=format:'%h' -n 1)"
  git tag -a "${NTAG}" "${CHASH}" -m "${DESCRIPTION}"
fi

rm -rf "${TMPDIR}"
exit 0