dotfiles-git/scripts/identity.sh

63 lines
1.8 KiB
Bash
Raw Normal View History

2023-12-21 22:49:17 +01:00
#!/bin/sh
2023-12-22 01:54:52 +01:00
identities="$HOME/.local/share/git/identities";
function import_identity() {
local gpg_key_id="$1"
filepath="$identities/$2";
2023-12-22 01:54:52 +01:00
local uid_regex="([^\(]* )\s*(\((.*)\))?\s*<(.*)>";
local uid=$(gpg --with-colons -K $gpg_key_id | awk -F: '$1=="uid" {print $10; exit}');
2023-12-22 01:54:52 +01:00
if [ -z "$uid" ]; then
echo "ERROR: found no gpg key matching this";
exit 1;
fi
local name=$(printf "$uid" | sed -Ee "s/$uid_regex/\1/" | xargs);
local email=$(printf "$uid" | sed -Ee "s/$uid_regex/\4/");
2023-12-22 01:54:52 +01:00
git config -f "$filepath" user.name "$name"
git config -f "$filepath" user.email "$email"
2023-12-22 01:54:52 +01:00
local keyid=$(gpg -K --with-colon $1 | awk -F: '$12~/.*s.*/ {print $5; exit}');
2023-12-22 01:54:52 +01:00
# TODO: if multiple found, bring up a dialog to select the right key
if [ -z "$keyid" ]; then
echo "WARNING: found no subkey with signing capabilities. No signing key will be set";
git config -f "$filepath" --unset user.signingKey;
2023-12-22 01:54:52 +01:00
else
git config -f "$filepath" user.signingKey "$keyid";
2023-12-22 01:54:52 +01:00
fi
}
case $1 in
import)
if [ -z "$2" ]; then
echo "USAGE:";
echo " git identity import <gpg_key_id> [identity_file_name]"
echo;
echo "Imports an identity from a gpg key. If no filename is provided, it will by default to the identifier you provided for your gpg key"
exit 1;
fi
if [ -z "$3" ]; then
import_identity "$2" "$2";
else
import_identity "$2" "$3";
fi
echo "imported into $filepath:"
cat "$filepath";
2023-12-22 01:54:52 +01:00
;;
*)
echo "USAGE:";
echo " git identity <command>";
echo;
echo "COMMANDS:"
echo " import: import an identity from a gpg key"
2023-12-22 01:54:52 +01:00
exit 1;
;;
esac