Skip to content

Instantly share code, notes, and snippets.

@hasufell hasufell/pre-commit.sh
Last active Jun 8, 2018

Embed
What would you like to do?
automate digest generation
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.