apkgrel.in 2.91 KB
Newer Older
1 2
#!/bin/sh

3 4 5 6 7 8
# apkgrel - display or bump pkgrel in APKBUILDs
# Copyright (c) 2012 Natanael Copa <ncopa@alpinelinux.org>
#
# Distributed under GPL-2
#

9
program_version=@VERSION@
10 11 12 13 14 15 16 17
datadir=@datadir@

if ! [ -f "$datadir/functions.sh" ]; then
	echo "$datadir/functions.sh: not found" >&2
	exit 1
fi
. "$datadir/functions.sh"

18

19 20 21 22 23 24 25 26 27
show_plain() {
	# we source the APKBUILD and show last pkgrel that's read
	# if this script is invoked with --force, this needn't pass "do_verify"
	( . "$1" && echo "$pkgrel" )
}

show_pretty() {
	(
	. "$1" || exit 1
28
	[ -n "$pkgname" ] || die "no \$pkgname for $1"
29 30 31 32
	printf '%s: r%s\n' "$pkgname" "${pkgrel:-?}"
	)
}

33
do_show() {
34 35 36
	local f=
	# show_pretty is more informative, but would change the output format of this script
	for f; do show_plain "$f"; done
37 38 39 40 41 42 43 44 45
}

do_set() {
	sed -e "/^pkgrel=/s/=.*/=${setto:-0}/" \
		-i "$@"
}

do_add () {
	local f= old=
46
	for f; do
47
		[ -n "$only_clean_git" ] \
48
			&& [ -n "$(git diff --name-only "${f%/*}")" ] \
49
			&& continue
50
		old=$(show_plain "$f")
51 52 53 54
		case $old in
		[0-9]*) setto=$((old + 1));;
		*) setto=0;;
		esac
55
		do_set "$f" || return 1
56
	done
57 58 59 60
}

do_verify() {
	[ -n "$force" ] && return 0
61
	local f= rc=0
62
	for f; do
63
		if ! grep -q '^pkgrel=[0-9]' "$f"; then
64
			error "no proper \$pkgrel for $f"
65 66 67 68
			rc=1
		fi
	done
	return $rc
69 70 71 72 73 74
}

do_nothing() {
	return 0
}

75
usage() {
76 77 78 79 80 81 82 83 84 85 86 87 88
	cat >&2 <<-__EOF__
		$program $program_version - display or bump pkgrel in APKBUILDs
		Usage: $program [-z|--zero] [-a|--add] [-g|--clean-git] [-s|--set NUM]
				[-t|--test] [-f|--force] DIR or APKBUILD...
		Options:
		  -z, --zero       Set pkgrel to 0
		  -a, --add        Add 1 to current pkgrel
		  -g, --clean-git  Only operate on APKBUILDs with clean git status (implies
		                   --add)
		  -s, --set NUM    Set pkgrel to NUM
		  -t, --test       Only verify that files have a valid pkgrel
		  -f, --force      Operate on files without a valid pkgrel
		  -h, --help       Show this help
89

90
	__EOF__
91 92 93 94
}

cmd=do_show
force=
95 96 97
setto=
only_clean_git=

98 99
args=$(getopt -o zags:tfqh --long zero,add,clean-git,set:,test,force,quiet,help \
	-n "$program" -- "$@")
100 101 102 103 104 105 106 107 108
if [ $? -ne 0 ]; then
	usage
	exit 2
fi
eval set -- "$args"
while true; do
	case $1 in
		-z|--zero) setto=0; cmd=do_set;;
		-a|--add) cmd=do_add;;
109 110 111 112
		-g|--clean-git) # verify that we're in a git tree
				git rev-parse 2>/dev/null || die "not in a git tree"
				cmd=do_add
				only_clean_git=1;;
113 114 115 116 117 118 119
		-s|--set) setto=$2; shift; cmd=do_set;;
		-t|--test) cmd=do_nothing;;
		-f|--force) force=1;;
		-q|--quiet) quiet=1;; # noop
		-h|--help) usage; exit;;
		--) shift; break;;
		*) exit 1;; # getopt error
120
	esac
121
	shift
122 123
done
if [ $# -eq 0 ]; then
124 125
	usage
	exit 2
126 127
fi

128 129 130 131 132 133 134 135 136
# normalize $@ into paths to APKBUILDs
[ "$(echo "$@" | wc -l)" -eq 1 ] || die "can't handle paths with embedded newlines"
args=$(for a; do p=$(any_buildscript "$a") || die "can't find APKBUILD for $a"; echo "$p"; done)
[ $? -eq 0 ] || exit 1
oldifs=$IFS
IFS=$'\n'
set -- $args
IFS=$oldifs

137 138
do_verify "$@" || exit 1
$cmd "$@"