2023-12-21 22:49:17 +01:00
#!/bin/sh
2023-12-22 18:51:12 +01:00
no_home_identities = ".local/share/git/identities"
identities = " $HOME / $no_home_identities " ;
2023-12-22 16:34:35 +01:00
mkdir -p " $identities " ;
2023-12-22 01:54:52 +01:00
2023-12-23 18:03:12 +01:00
if [ ! -z " $( git config core.editor) " ] ; then
EDITOR = " $( git config core.editor) " ;
elif [ ! -z " $GIT_EDITOR " ] ; then
EDITOR = " $GIT_EDITOR " ;
fi
2023-12-22 12:37:34 +01:00
function yes_or_no { # courtesy of: https://stackoverflow.com/a/29436423
while true; do
2023-12-22 17:27:23 +01:00
read -p " $* [y/n]: " yn;
2023-12-22 12:37:34 +01:00
case $yn in
2023-12-22 17:27:23 +01:00
[ Yy] *) return 0 ; ;
2023-12-22 12:37:34 +01:00
[ Nn] *) return 1 ; ;
esac
done
}
2023-12-22 12:08:01 +01:00
function get_identity( ) {
2023-12-22 12:37:34 +01:00
filepath = " $identities / $1 " ;
identity = " $1 " ; # TODO: match from partial names and set this to the correct one
2023-12-22 12:22:30 +01:00
sigkey = $( git config -f " $filepath " user.signingKey) ;
2023-12-22 17:27:23 +01:00
name = $( git config -f " $filepath " user.name) ;
2023-12-22 12:22:30 +01:00
email = $( git config -f " $filepath " user.email) ;
}
function display_parts( ) {
local identity = " $1 " ;
local name = " $2 " ;
local email = " $3 " ;
local sigkey = " $4 " ;
2023-12-26 18:09:11 +01:00
if [ ! -z " $identity " ] ; then
printf " [ $identity ] " ;
fi
printf " $name < $email > " ;
if [ ! -z " $sigkey " ] ; then
2023-12-22 12:22:30 +01:00
printf " (signing key: $sigkey ) " ;
fi
printf "\n" ;
}
function display_identity( ) {
2023-12-22 12:37:34 +01:00
local name email sigkey;
2023-12-22 12:22:30 +01:00
get_identity $1 ;
2023-12-22 12:37:34 +01:00
display_parts " $identity " " $name " " $email " " $sigkey " ;
2023-12-22 12:08:01 +01:00
}
2023-12-22 02:23:39 +01:00
function import_identity( ) {
local gpg_key_id = " $1 "
filepath = " $identities / $2 " ;
2023-12-22 01:54:52 +01:00
2023-12-22 02:23:39 +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
2023-12-22 02:23:39 +01:00
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
2023-12-22 17:27:23 +01:00
git config -f " $filepath " user.name " $name " ;
git config -f " $filepath " user.email " $email " ;
2023-12-22 01:54:52 +01:00
2023-12-22 02:23:39 +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" ;
2023-12-22 02:23:39 +01:00
git config -f " $filepath " --unset user.signingKey;
2023-12-22 01:54:52 +01:00
else
2023-12-22 02:23:39 +01:00
git config -f " $filepath " user.signingKey " $keyid " ;
2023-12-22 01:54:52 +01:00
fi
}
2023-12-22 12:08:01 +01:00
function list_identities( ) {
2023-12-22 12:22:30 +01:00
if [ -z " $( ls -A " $identities " ) " ] ; then
2023-12-22 18:55:43 +01:00
echo "No identities to display" ;
2023-12-22 12:22:30 +01:00
exit 0;
fi
2023-12-22 12:08:01 +01:00
local sigkey name email;
for id_file in $identities /*; do
2023-12-22 12:22:30 +01:00
identity = " $( basename " $id_file " ) " ;
get_identity " $identity " ;
display_parts " $identity " " $name " " $email " " $sigkey " ;
2023-12-22 12:08:01 +01:00
done
}
2023-12-22 01:54:52 +01:00
case $1 in
2023-12-22 02:23:39 +01:00
import)
if [ -z " $2 " ] ; then
echo "USAGE:" ;
2023-12-22 18:06:37 +01:00
echo " git identity import <gpg_key_id> [identity]" ;
2023-12-22 02:23:39 +01:00
echo;
2023-12-22 18:06:37 +01:00
echo "Imports an identity from a gpg key. If no name is provided for this identity, it will by default to the identifier you provided for your gpg key" ;
2023-12-22 02:23:39 +01:00
exit 1;
fi
if [ -z " $3 " ] ; then
2023-12-22 12:22:30 +01:00
identity = " $2 " ;
2023-12-22 02:23:39 +01:00
else
2023-12-22 12:22:30 +01:00
identity = " $3 " ;
2023-12-22 02:23:39 +01:00
fi
2023-12-22 12:22:30 +01:00
import_identity " $2 " " $identity " ;
2023-12-22 02:23:39 +01:00
2023-12-22 18:55:43 +01:00
echo " Imported into $filepath : "
2023-12-22 12:22:30 +01:00
display_identity " $identity " ;
2023-12-22 01:54:52 +01:00
; ;
2023-12-22 12:37:34 +01:00
remove)
if [ -z " $2 " ] ; then
echo "USAGE:" ;
echo " git identity remove <identity>" ;
echo;
echo "Removes a saved identity. This is unrecoverable; You'll need to re-import / add it yourself again." ;
exit 1;
fi
get_identity " $2 " ;
display_identity " $identity " ;
yes_or_no "Are you sure ou want to remove this identity ?" && rm " $filepath " ;
; ;
2023-12-22 12:08:01 +01:00
list)
list_identities;
; ;
2023-12-22 17:48:57 +01:00
set )
if [ -z " $2 " ] ; then
echo "USAGE:" ;
echo " git identity set <identity>" ;
echo;
2023-12-22 18:06:37 +01:00
echo "Copies the config from an identity to the local git config." ;
echo "Using set instead of link essentially means that future changes made to the identity will not be synced with the local git config." ;
2023-12-23 18:03:12 +01:00
echo "If you wish for that be the case, consider using the include subcommand instead." ;
2023-12-22 18:51:12 +01:00
echo;
echo "[include]" ;
echo " path = ~/ $no_home_identities /<identity> "
2023-12-22 17:48:57 +01:00
exit 0;
fi
get_identity " $2 " ;
echo "Using this identity:" ;
display_identity " $identity " ;
git config user.name " $name " ;
[ ! -z " $email " ] && git config user.email " $email " ;
[ ! -z " $sigkey " ] && git config user.signingKey " $sigkey " ;
; ;
2023-12-23 18:03:12 +01:00
include)
if [ -z " $2 " ] ; then
echo "USAGE:" ;
echo;
echo " git identity include <identity>" ;
echo;
echo "Takes the path of the file in which the identity is defined and includes it in your local git config." ;
echo "This means that any changes made to the identity will be kept in sync in this local confif, risking a loss of consistency for the identity that's shown in your commits."
exit 1;
fi
get_identity " $2 " ;
echo "Using this identity:" ;
display_identity " $identity " ;
echo;
git_config = " $GIT_DIR /config " ;
echo " Writing to $git_config " ;
echo;
echo -e " [include]\n path = ~/ $no_home_identities / $identity " >> " $git_config " ;
res = 0;
set -m;
#while [ "$res" -eq 0 ]; do # TODOOOOOOOOOOOO
echo "Your new identity is:" ;
display_parts "" " $( git config user.name) " " $( git config user.email) " " $( git config user.signingKey) " ;
yes_or_no "Is this good (otherwise, review the config)" ;
res = $? ;
if [ " $res " -eq 1 ] ; then
$EDITOR " $git_config " ;
fi
#done
; ;
2023-12-22 17:52:36 +01:00
show)
if [ -z " $2 " ] ; then
2023-12-26 18:09:11 +01:00
display_parts "" " $( git config user.name) " " $( git config user.email) " " $( git config user.signingKey) " ;
2023-12-22 17:52:36 +01:00
else
2023-12-22 18:55:43 +01:00
get_identity " $2 " ;
display_identity " $identity " ;
echo " Located in: $filepath "
2023-12-22 17:52:36 +01:00
fi
; ;
2023-12-22 01:54:52 +01:00
*)
echo "USAGE:" ;
echo " git identity <command>" ;
2023-12-22 02:23:39 +01:00
echo;
2023-12-22 12:08:01 +01:00
echo " Saved identities can be found here: $identities " ;
echo;
2023-12-22 17:27:23 +01:00
echo "COMMANDS:" ;
2023-12-22 18:55:43 +01:00
echo " import: Imports an identity from a gpg key" ;
echo " remove: Removes a saved identity" ;
echo " set: Sets the identity of the current git repo" ;
2023-12-23 18:03:12 +01:00
echo " include: Includes the identity file in your local git config"
echo " show: Show a saved identity or the identity that's currently effective" ;
2023-12-22 18:55:43 +01:00
echo " list: List all saved identities" ;
2023-12-22 01:54:52 +01:00
exit 1;
; ;
esac