]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame_incremental - shrc.sh
rpm query is slow, cache it
[packages/rpm-build-tools.git] / shrc.sh
... / ...
CommitLineData
1# shell aliases and functions for PLD Developer
2
3# set $dist, used by functions below
4[ -n "$dist" ] || dist=$(awk '/PLD Linux/ {print tolower($NF)}' /etc/pld-release 2>/dev/null | tr -d '()')
5
6case "$dist" in
7ac|th|ti)
8 ;;
9*)
10 # invalid one ;)
11 dist=
12esac
13
14if [ "$dist" ]; then
15
16alias ipoldek-$dist="poldek -q --sn $dist --cmd"
17alias $dist-provides="ipoldek-$dist what-provides"
18alias $dist-verify=dist-verify
19alias $dist-requires=dist-requires
20
21# move AC-branch tag to current checkout
22# if AC-branch as branch exists, it is first removed
23ac-tag() {
24 # see if remote has branch present
25 local branch=AC-branch
26 if git show-ref -q refs/remotes/origin/$branch; then
27 git fetch --tags
28 if [ -z "$(git tag --points-at origin/$branch 2>/dev/null)" ]; then
29 echo >&2 "There's no tag pointing to current $branch; refusing to delete branch"
30 return 1
31 fi
32 # delete local branch if exists
33 git show-ref -q refs/heads/$branch && git branch -d $branch
34
35 # drop remote branch
36 git push --delete origin $branch
37 fi
38
39 git tag -f $branch
40 git push -f origin $branch
41}
42
43alias q='rpm -q --qf "%{N}-%|E?{%{E}:}|%{V}-%{R}.%{ARCH}\n"'
44
45# undo spec utf8
46# note: it will do it blindly, so any lang other than -pl is most likely broken
47specutfundo() {
48 local spec="$1"
49 iconv -futf8 -tlatin2 "$spec" > m
50 sed -e 's/\.UTF-8//' m > "$spec"
51 rm -f m
52}
53
54dist-requires() {
55 local opts deps
56 while [ $# -gt 0 ]; do
57 case "$1" in
58 --sn)
59 opts="$opts $1 $2"
60 shift
61 ;;
62 -*)
63 opts="$opts $1"
64 ;;
65 *)
66 deps="$deps $1"
67 ;;
68 esac
69 shift
70 done
71
72 case "$dist" in
73 ac)
74 opts="$opts --sn=$dist-updates"
75 ;;
76 esac
77
78 poldek -q -Q --sn $dist $opts --cmd what-requires $deps
79}
80
81dist-verify() {
82 local args sn
83 sn="--sn $dist"
84
85 case "$dist" in
86 ac)
87 sn="$sn --sn $dist-updates"
88
89 local a ignore
90 # typo
91 ignore="$ignore kdenetwork-kopete-tool-conectionstatus"
92 # obsoleted
93 ignore="$ignore gimp-plugin-swfdec wine-drv-arts ntp-ntptrace"
94 # quake2@MAIN is now quake2forge, original quake2 restored to quake2
95 ignore="$ignore quake2-3dfx quake2-sdl quake2-sgl quake2-snd-alsa quake2-snd-ao quake2-snd-oss quake2-snd-sdl quake2-static"
96 # obsoleted
97 ignore="$ignore mozilla-firefox-lang-en apache1-mod_perl-devel libyasm-static"
98 # renamed (courier-authlib.spec, r1.54)
99 ignore="$ignore courier-authlib-userdb courier-authlib-pipe"
100 # obsoleted, squid 2.6
101 ignore="$ignore squid-winbind_acl squid-winbind_auth"
102 # obsoleted with 1.0.4
103 ignore="$ignore python-numpy-FFT python-numpy-MA python-numpy-RNG"
104 # subpkgs renamed
105 ignore="$ignore apache1-doc apache1-index"
106 # obsoleted by kadu-module-mediaplayer-amarok
107 ignore="$ignore kadu-module-amarok"
108 # obsoleted by kadu-module-mediaplayer-xmms
109 ignore="$ignore kadu-module-xmms"
110 # obsoleted by kadu 0.6.0
111 ignore="$ignore kadu-theme-icons-crystal16 kadu-theme-icons-crystal22 kadu-theme-icons-nuvola16 kadu-theme-icons-nuvola22 kadu-module-iwait4u"
112 # obsoleted pear test packages
113 ignore="$ignore php-*-tests"
114 # obsoleted
115 ignore="$ignore nmap-X11"
116 # mksd dependency not distributale
117 ignore="$ignore samba-vfs-vscan-mks"
118 # ibbackup is not distributale
119 ignore="$ignore innobackup"
120 # use ac-updates
121 ignore="$ignore ntp-client ntp"
122 # php4 only(php-pecl-tidy), for php<5.2(php-pecl-filter)
123 ignore="$ignore php-pecl-tidy php-pecl-filter"
124
125 # renamed to vim-syntax-txt2tags
126 ignore="$ignore txt2tags-vim"
127
128 for a in $ignore; do
129 args="$args --ignore=$a"
130 done
131 ;;
132 esac
133
134 poldek $sn --up --upa -q "$@"
135 poldek $sn --uniq --noignore --verify=deps $args "$@"
136}
137
138# displays latest used tag for a specfile
139autotag() {
140 local out s
141 for s in "$@"; do
142 # strip branches
143 s=${s%:*}
144 # ensure package ends with .spec
145 s=${s%.spec}.spec
146 git fetch --tags
147 out=$(git for-each-ref --count=1 --sort=-authordate refs/tags/auto/$dist \
148 --format='%(refname:short)')
149 echo "$s:$out"
150 done
151}
152
153get-buildlog() {
154 local p=$1
155 if [ -z "$p" ]; then
156 echo >&2 "Usage: get-buildlog PACKAGE"
157 echo >&2 ""
158 echo >&2 "Grabs buildlogs from pld builder for all arch."
159 return
160 fi
161
162 local archlist
163 case "$dist" in
164 ac)
165 archlist='i686 i586 i386 athlon alpha sparc amd64 ppc'
166 ;;
167 th)
168 archlist='x86_64 i486 i686'
169 ;;
170 *)
171 echo >&2 "get-buildlog: $dist buildlogs are /dev/null"
172 return
173 esac
174
175 local url arch path ftp=ftp://buildlogs.pld-linux.org
176 for arch in $archlist; do
177 [ "$arch" ] || continue
178 path=${url#$ftp}
179 echo -n "Checking $p.$arch... "
180 url=$(lftp -c "debug 0;open $ftp; cls --sort=date -r /$dist/$arch/OK/$p,*.bz2 /$dist/$arch/FAIL/$p,*.bz2 | tail -n1")
181 url=$ftp$url
182
183 echo -n "$url... "
184 if wget -q $url -O .$p~; then
185 echo "OK"
186 mv -f .$p~ $p.$arch.bz2
187 else
188 echo "SKIP"
189 rm -f .$p~
190 fi
191 done
192}
193
194fi # no $dist set
195
196alias adif="dif -x '*.m4' -x ltmain.sh -x install-sh -x depcomp -x 'Makefile.in' -x compile -x 'config.*' -x configure -x missing -x mkinstalldirs -x autom4te.cache"
197alias pclean="sed -i~ -e '/^\(?\|=\+$\|unchanged:\|diff\|only\|Only\|Tylko\|Files\|Common\|Index:\|RCS file\|retrieving\)/d'"
198
199# merges two patches
200# requires: patchutils
201pmerge() {
202 combinediff -p1 $1 $2 > m.patch || return
203 pclean m.patch
204 dif $1 m.patch
205}
206
207# downloads sourceforge url from specific mirror
208sfget() {
209 local url="$1"
210 url="${url%?download}"
211 url="${url%?use_mirror=*}"
212 url="${url#http://downloads.}"
213 url="http://dl.${url#http://prdownloads.}"
214 # use mirror
215 local mirror="http://nchc.dl.sourceforge.net"
216 url="$mirror/sourceforge/${url#http://dl.sourceforge.net/}"
217 wget -c "$url"
218}
219
220dif() {
221 if [ -t 1 ]; then
222 diff -ur -x .svn -x .git -x .bzr -x CVS "$@" | diffcol | less -R
223 else
224 diff -ur -x .svn -x .git -x .bzr -x CVS "$@"
225 fi
226}
227
228diffcol() {
229sed -e '
230 s,\e,\e[44m^[\e[49m,g;
231 s,\a,\e[44m^G\e[49m,g;
232 s,^\(Index:\|diff\|---\|+++\) .*$,\e[32m&,;
233 s,^@@ ,\e[33m&,;
234 s,^-,\e[35m&,;
235 s,^+,\e[36m&,;
236 s,\r,\e[44m^M\e[49m,g;
237 s, , ,g;
238 s,\([^[:space:]]\)\([[:space:]]\+\)$,\1\e[41m\2\e[49m,g;
239 s,$,\e[0m,
240' ${1:+"$@"}
241}
242
243# does diff between FILE~ and FILE
244# the diff can be applied with patch -p1
245d() {
246 local file="$1" dir
247 shift
248 if [[ "$file" = /* ]]; then
249 # full path -- no idea where to strip
250 dir=.
251 diff=$file
252 else
253 # relative path -- keep one path component from current dir
254 dir=..
255 diff=${PWD##*/}/${file}
256 fi
257
258 (builtin cd "$dir"; dif $diff{~,} "$@")
259}
260
261# spec name from NVR
262rpm2spec() {
263 sed -re 's,^(.+)-[^-]+-[^-]+$,\1.spec,'
264}
265
266
267#
268# A colorized bash prompt
269# - shows curret branch
270# - shows if branch is up to date/ahead/behind
271# - shows if last command exited with error (red)
272#
273# To use it, set $PROMPT_COMMAND env var:
274# PROMPT_COMMAND=__bash_prompt_command
275#
276__bash_prompt_command() {
277 local previous_return_value=$?
278
279 local RED="\[\033[0;31m\]"
280 local YELLOW="\[\033[0;33m\]"
281 local GREEN="\[\033[0;32m\]"
282 local BLUE="\[\033[0;34m\]"
283 local LIGHT_RED="\[\033[1;31m\]"
284 local LIGHT_GREEN="\[\033[1;32m\]"
285 local WHITE="\[\033[1;37m\]"
286 local LIGHT_GRAY="\[\033[0;37m\]"
287 local COLOR_NONE="\[\e[0m\]"
288
289 # if we are in rpm subdir and have exactly one .spec in the dir, include package version
290 __package_update_rpmversion
291 local rpmver=$(__package_rpmversion)
292
293 local prompt="${BLUE}[${RED}\w${GREEN}${rpmver:+($rpmver)}$(__bash_parse_git_branch)${BLUE}]${COLOR_NONE} "
294 if [ $previous_return_value -eq 0 ]; then
295 PS1="${prompt}➔ "
296 else
297 PS1="${prompt}${RED}➔${COLOR_NONE} "
298 fi
299}
300
301# helper for __bash_prompt_command
302# command line (git) coloring
303# note we use "\" here to avoid any "git" previous alias/func
304__bash_parse_git_branch() {
305 # not in git dir. return early
306 \git rev-parse --git-dir &> /dev/null || return
307
308 local git_status branch_pattern remote_pattern diverge_pattern
309 local state remote branch
310
311 git_status=$(\git -c color.ui=no status 2> /dev/null)
312 branch_pattern="^On branch ([^${IFS}]*)"
313 remote_pattern="Your branch is (behind|ahead) "
314 diverge_pattern="Your branch and (.*) have diverged"
315
316 if [[ ! ${git_status} =~ "working directory clean" ]]; then
317 state="${RED}⚡"
318 fi
319
320 # add an else if or two here if you want to get more specific
321 if [[ ${git_status} =~ ${remote_pattern} ]]; then
322 if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
323 remote="${YELLOW}↑"
324 else
325 remote="${YELLOW}↓"
326 fi
327 fi
328
329 if [[ ${git_status} =~ ${diverge_pattern} ]]; then
330 remote="${YELLOW}↕"
331 fi
332
333 if [[ ${git_status} =~ ${branch_pattern} ]]; then
334 branch=${BASH_REMATCH[1]}
335 echo " (${branch})${remote}${state}"
336 fi
337}
338
339# cache requires bash 4.x
340declare -A __package_update_rpmversion_cache
341__package_update_rpmversion() {
342 # extract vars from cache
343 set -- ${__package_update_rpmversion_cache[$PWD]}
344 local specfile=$1 version=$2 mtime=$3
345
346 # invalidate cache
347 if [ -f "$specfile" ]; then
348 local stat
349 stat=$(stat -c %Y $specfile)
350 if [ $mtime ] && [ $stat -gt $mtime ]; then
351 unset version
352 fi
353 mtime=$stat
354 else
355 # reset cache, .spec may be renamed
356 unset version specfile
357 fi
358
359 # we have cached version
360 test -n "$version" && return
361
362 # needs to be one file
363 specfile=${specfile:-$(\ls *.spec 2>/dev/null)}
364 if [ ! -f "$specfile" ]; then
365 unset __package_update_rpmversion_cache[$PWD]
366 return
367 fi
368
369 mtime=${mtime:-$(stat -c %Y $specfile)}
370
371 # give only first version (ignore subpackages)
372 version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1)
373 __package_update_rpmversion_cache[$PWD]="$specfile $version $mtime"
374}
375
376__package_rpmversion() {
377 # extract vars from cache
378 set -- ${__package_update_rpmversion_cache[$PWD]}
379 # print version
380 echo $2
381}
This page took 0.044174 seconds and 4 git commands to generate.