gists/pre-commit

113 lines
2.0 KiB
Plaintext
Raw Normal View History

2018-06-08 17:16:26 +00:00
#!/bin/bash
#
# Author: Julian Ospald <hasufell@posteo.de>
# Version: 0.0.1
# Date: 2018-06-08
# Reason: automate digest generation
#
# This pre-commit hook generates digests for all packages
# that have been changed and possibly adds the Manifests to the index.
#
# For non-interactive mode set "DIGEST_NONINTERACTIVE=y".
#
die() {
echo "$@" 1>&2
exit 1
}
edo() {
echo "$@" 1>&2
"$@" || exit 1
}
# all staged files
file_names=( )
# all touched packages (${CATEGORY}/${PN})
pkgs=( )
# all packages that we generated digest for
pkg_digs=( )
# set file_names
while IFS= read -r -d '' f; do
file_names+=( "${f}" )
done < <(git diff --cached --name-only -z HEAD)
unset f IFS
# exit early
if [[ -z ${file_names[@]} ]] ; then
exit 0
fi
# name of the repository
repo_name="$(cat $(git rev-parse --show-toplevel)/profiles/repo_name)"
if [[ -z ${repo_name} ]] ; then
die "no repository name!"
fi
# set pkgs
for f in "${file_names[@]}" ; do
ext="${f##*.}"
if [[ ${ext} =~ exheres-[0-9]+ ]] ; then
pkg=${f#packages/}
pkg=${pkg%/*}
pkgs+=( "${pkg}" )
fi
done
unset ext pkg f
# sort array
IFS=$'\n' pkgs=( $(sort -u <<<"${pkgs[*]}") )
unset IFS
# Allows us to read user input below, assigns stdin to keyboard
if [[ -z ${DIGEST_NONINTERACTIVE} ]] ; then
exec < /dev/tty
fi
# call digest
for pkg in "${pkgs[@]}" ; do
if [[ -n ${DIGEST_NONINTERACTIVE} ]] ; then
edo ${CAVE:-cave} digest ${pkg} ${repo_name}
pkg_digs+=( "${pkg}" )
else
read -p "Generate digest for ${pkg}? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
edo ${CAVE:-cave} digest ${pkg} ${repo_name}
pkg_digs+=( "${pkg}" )
fi
fi
done
unset pkg
# stage Manifests
for pkg_dig in "${pkg_digs[@]}" ; do
if [[ -n ${DIGEST_NONINTERACTIVE} ]] ; then
edo git add -- packages/${pkg_dig}/Manifest
else
read -p "Add packages/${pkg_dig}/Manifest to commit? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
edo git add -- packages/${pkg_dig}/Manifest
fi
fi
done
unset pkg_dig
# close STDIN
if [[ -z ${DIGEST_NONINTERACTIVE} ]] ; then
exec <&-
fi