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