18977d7e405299522481c334c971d38c2f36c96e
[packages/rpm-build-tools.git] / relup.sh
1 #!/bin/bash
2 # script to run after "release bump" style change.
3 # takes Release from spec and creates commit with message
4 # groups similiar commits together.
5 # "- release $rel"
6
7 set -e
8
9 get_dump() {
10         local specfile="$1"
11         local rpm_dump
12         local success="y"
13         if [ -x /usr/bin/rpm-specdump ]; then
14                 rpm_dump=$(rpm-specdump "$specfile" 2>&1) || success="n"
15         else
16                 rpm_dump=$(rpm --specfile "$specfile" --define 'prep %dump' -q 2>&1) || success="n"
17         fi
18         if [ "$success" != "y" ]; then
19                 echo >&2 "$rpm_dump"
20                 echo >&2 "You need icon files being present in SOURCES."
21                 exit 1
22         fi
23         echo "$rpm_dump"
24 }
25
26 usage="Usage:
27 ${0##*/} [-i] [-g] [-u] [-t|-n] [-m <MESSAGE>] <SPECLIST>
28
29 Options:
30 -i
31   Try to increment package release
32 -g
33  get packages if missing, do nothing else
34 -u
35  update packages (git pull)
36 -t | -n
37   Test mode (dry-run). do not commit
38 -m
39   Specify commit message
40
41 "
42
43 get_release() {
44         local specfile="$1"
45         rel=$(awk '/^%define[   ]+_?rel[        ]+/{print $NF}' $specfile)
46         if [ -z "$rel" ]; then
47                 dump=$(get_dump "$specfile")
48                 rel=$(echo "$dump" | awk '$2~/^(PACKAGE_)?RELEASE$/{print $NF; exit}')
49         fi
50         echo $rel
51 }
52
53 set_release() {
54         local specfile="$1"
55         local rel="$2"
56         local newrel="$3"
57         sed -i -e "
58                 s/^\(%define[ \t]\+_\?rel[ \t]\+\)$rel\$/\1$newrel/
59                 s/^\(Release:[ \t]\+\)$rel\$/\1$newrel/
60         " $specfile
61 }
62
63 bump_release() {
64         local release=$1 rel
65
66         rel=$(expr ${release} + 1)
67         echo $rel
68 }
69
70 # normalize spec
71 # takes as input:
72 # - PACKAGE/
73 # - ./PACKAGE/
74 # - PACKAGE
75 # - PACKAGE.spec
76 # - ./PACKAGE.spec
77 # - PACKAGE/PACKAGE
78 # - PACKAGE/PACKAGE.spec
79 # - ./PACKAGE/PACKAGE.spec
80 # - rpm/PACKAGE/PACKAGE
81 # - rpm/PACKAGE/PACKAGE.spec
82 # - ./rpm/PACKAGE/PACKAGE.spec
83 # returns PACKAGE
84 package_name() {
85         local specfile="${1%/}" package
86
87         # strip branch
88         specfile=${specfile%:*}
89         # basename
90         specfile=${specfile##*/}
91         # strip .spec
92         package=${specfile%.spec}
93
94         echo $package
95 }
96
97 get_branch() {
98         local specfile="${1%/}" branch
99
100         branch=${specfile#*:}
101
102         if [ "$branch" != "$specfile" ]; then
103                 echo "$branch"
104         else
105                 echo ""
106         fi
107 }
108
109 if [ ! -x /usr/bin/getopt ]; then
110         echo >&1 "You need to install util-linux to use relup.sh"
111         exit 1
112 fi
113
114 t=$(getopt -o 'm:inguth' -n "${0##*/}" -- "$@") || exit $?
115 # Note the quotes around `$t': they are essential!
116 eval set -- "$t"
117
118 while true; do
119         case "$1" in
120         -i)
121                 inc=1
122                 ;;
123         -u)
124                 update=1
125                 ;;
126         -g)
127                 get=1
128                 ;;
129         -t | -n)
130                 test=1
131                 ;;
132         -m)
133                 shift
134                 message="${1#- }"
135                 ;;
136         -h)
137                 echo "$usage"
138                 exit 0
139                 ;;
140         --)
141                 shift
142                 break
143         ;;
144         *)
145                 echo 2>&1 "Internal error: [$1] not recognized!"
146                 exit 1
147                 ;;
148         esac
149         shift
150 done
151
152 topdir=$(rpm -E '%{_topdir}')
153
154 n="$(echo -e '\nn')"
155 n="${n%%n}"
156
157 cd "$topdir"
158 for pkg in "$@"; do
159         branch=$(get_branch "$pkg")
160         # pkg: package %{name}
161         pkg=$(package_name "$pkg")
162
163         # spec: package/package.spec
164         spec=$(rpm -D "name $pkg" -E '%{_specdir}/%{name}.spec')
165         spec=${spec#$topdir/}
166
167         # pkgdir: package/
168         pkgdir=${spec%/*}
169
170         # specname: only spec filename
171         specname=${spec##*/}
172
173         # start real work
174         if [ -n "$branch" ]; then
175                 echo "$pkg:$branch ..."
176         else
177                 echo "$pkg ..."
178         fi
179
180         # get package
181         [ "$get" = 1 -a -d "$pkgdir" ] && continue
182
183         if [ "$update" = "1" -o "$get" = "1" ]; then
184                 if [ -n "$branch" ]; then
185                         ./builder -g -ns "$spec" -r $branch
186                 else
187                         ./builder -g -ns "$spec"
188                 fi
189         fi
190
191         [ "$get" = 1 ] && continue
192
193         # update .spec files
194         rel=$(get_release "$spec")
195         if [ "$inc" = 1 ]; then
196                 if [[ $rel = *%* ]]; then
197                         relmacro=${rel#*%}
198                         newrel=$(bump_release ${rel%%%*})
199                         set_release "$spec" $rel "${newrel}%${relmacro}"
200                 else
201                         newrel=$(bump_release ${rel})
202                         set_release "$spec" $rel $newrel
203                 fi
204
205                 # refetch release
206                 rel=$(get_release "$spec")
207         fi
208
209         # commit the changes
210         msg=""
211         [ -n "$message" ] && msg="$msg- $message$n"
212         msg="$msg- release ${rel%%%*} (by relup.sh)"
213         echo git commit -m "$msg" $spec
214         if [ "$test" != 1 ]; then
215                 cd $pkgdir
216                 git commit -m "$msg" $specname
217                 git push
218                 cd ..
219         fi
220 done
This page took 0.240568 seconds and 2 git commands to generate.