13c22ee30d0e8aa50b1584cdf9293c0cecda908f
[BK-2020-03.git] / user / get_gpgkey_from_gitlog.sh
1 #!/usr/bin/env bash
2 # Desc: A script that outputs a list of GPG signatures from git log output
3 # Usage: get_gpgkey_from_gitlog.sh [dir]
4 # Input: arg1: directory
5 # Output: stdout: newline-delimited list of 40-char GPG fingerprints
6 # Version: 0.0.1
7
8 declare -g buffer;
9
10 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
11 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
12 try() { "$@" || die "cannot $*"; } # runs args as command, reports args if command fails
13 check_args() {
14 # Desc: Check arguments
15 if [[ $# -ne 1 ]]; then die "FATAL:Incorrect number of arguments:$#"; fi;
16 if [[ ! -d $1 ]]; then die "Not a directory:$1"; fi;
17 return 0;
18 }; # check args
19 fill_buffer() {
20 # Input: arg1: git dir
21 # Output: var: $buffer: newline-delimited list of gpg fingerprints
22 pushd "$1" 1>/dev/random || die "FATAL:Failed to change directory:$1";
23 buffer="$(git log --show-signature 2>&1)";
24 yell "DEBUG:Buffer has $(wc -l < <(printf "%s" "$buffer")) lines.";
25 buffer="$(grep -E -- "^gpg:" < <( printf "%s" "$buffer" ) )";
26 yell "DEBUG:Buffer has $(wc -l < <(printf "%s" "$buffer")) lines.";
27 buffer="$(grep -Eo "([0-9A-F]){40}" < <( printf "%s" "$buffer" ) )";
28 yell "DEBUG:Buffer has $(wc -l < <(printf "%s" "$buffer")) lines.";
29 buffer="$(sort -u < <( printf "%s" "$buffer" ))";
30 yell "DEBUG:Buffer has $(wc -l < <(printf "%s" "$buffer")) lines.";
31 popd 1>/dev/random || die "FATAL:Failed to popd.";
32 }; # fill buffer with git gpg lines
33 main() {
34 check_args "$@";
35 fill_buffer "$1";
36 printf "%s\n" "$buffer";
37 return 0;
38 };
39
40 main "$@";
41
42 # git log --show-signature 2>&1 | grep -E -- "^gpg:" | grep -Eo "([0-9A-F]){40}" | sort -u