Skip to content

Commit

Permalink
Merge pull request #1 from blendthink/update
Browse files Browse the repository at this point in the history
Support for multiple users of Git
  • Loading branch information
blendthink committed Jul 4, 2023
2 parents cf3f823 + 2e1829f commit c29812a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ sw_vers

```
ProductName: macOS
ProductVersion: 13.2
BuildVersion: 22D49
ProductVersion: 13.4.1
BuildVersion: 22F82
```

```zsh
zsh --version
```

```
zsh 5.8.1 (x86_64-apple-darwin22.0)
zsh 5.9 (x86_64-apple-darwin22.0)
```

## Installation
Expand Down
78 changes: 53 additions & 25 deletions scripts/git.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,68 @@ else
echo "Completed installing pinentry-mac ✅ "
fi

if gpg --list-secret-keys | grep -q "Tatsuya Okayama <admin@blendthink.dev>" &>/dev/null; then
echo 'Key already exists ✅ '
exit 0
fi

mkdir -p "$HOME/.gnupg" && chmod 700 "$HOME/.gnupg"
echo "pinentry-program $(which pinentry-mac)" >"$HOME/.gnupg/gpg-agent.conf"
gpgconf --kill gpg-agent

if [[ "$GITHUB_ACTIONS" == 'true' ]]; then
gpg --no-tty --pinentry-mode loopback --passphrase passwd --quick-gen-key "Tatsuya Okayama <admin@blendthink.dev>" default default 0
else
gpg --quick-gen-key "Tatsuya Okayama <admin@blendthink.dev>" default default 0
fi
function generate_key() {
local CONFIG_NAME=$1
local USER_NAME=$2
local EMAIL=$3
local AUTHOR="$USER_NAME <$EMAIL>"

GPG_KEY_ID=$(gpg --list-secret-keys --with-colons | awk -F: '$1 == "sec" {print $5}' | tail -n 1)
GPG_PUBLIC_KEY=$(gpg --armor --export "$GPG_KEY_ID")

if [[ "$GITHUB_ACTIONS" != 'true' ]]; then
yes | gh auth login -h github.com -s admin:gpg_key -p https -w
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/user/gpg_keys \
-f name="blendthink's GPG Key" \
-f armored_public_key="$GPG_PUBLIC_KEY"
fi
if gpg --list-secret-keys | grep -q "$AUTHOR" &>/dev/null; then
echo "Key already exists ✅ : $AUTHOR"
return
fi

if [[ "$GITHUB_ACTIONS" == 'true' ]]; then
gpg --no-tty --pinentry-mode loopback --passphrase passwd --quick-gen-key "$AUTHOR" default default 0
else
gpg --quick-gen-key "$AUTHOR" default default 0
fi

GPG_KEY_ID=$(gpg --list-secret-keys --with-colons | awk -F: '$1 == "sec" {print $5}' | tail -n 1)
GPG_PUBLIC_KEY=$(gpg --armor --export "$GPG_KEY_ID")

if [[ "$GITHUB_ACTIONS" != 'true' ]]; then
yes | gh auth login -h github.com -s admin:gpg_key -p https -w
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
/user/gpg_keys \
-f name="$AUTHOR's GPG Key" \
-f armored_public_key="$GPG_PUBLIC_KEY"
fi

cat <<EOT >>.gitconfig-"$CONFIG_NAME"
[user]
name = "$USER_NAME"
EMAIL = "$EMAIL"
signingkey = "$GPG_KEY_ID"
EOT
}

CONFIG_NAME_PRIVATE="private"
CONFIG_NAME_YUMEMI="yumemi"

USER_NAME="Tatsuya Okayama (blendthink)"

EMAIL_PRIVATE="admin@blendthink.dev"
EMAIL_YUMEMI="t_okayama@yumemi.co.jp"

generate_key "$CONFIG_NAME_PRIVATE" "$USER_NAME" "$EMAIL_PRIVATE"

git config --global init.defaultBranch main
git config --global user.name blendthink
git config --global user.email admin@blendthink.dev
git config --global user.signingkey "$GPG_KEY_ID"
git config --global user.name "$USER_NAME"
git config --global user.EMAIL "$EMAIL_PRIVATE"
git config --global commit.gpgsign true
git config --global gpg.program "$(which gpg)"
git config --global includeIf."gitdir:~/Repos/$CONFIG_NAME_PRIVATE/".path ".gitconfig-$CONFIG_NAME_PRIVATE"
git config --global includeIf."gitdir:~/Repos/$CONFIG_NAME_YUMEMI/".path ".gitconfig-$CONFIG_NAME_YUMEMI"

generate_key "$CONFIG_NAME_YUMEMI" "$USER_NAME" "$EMAIL_YUMEMI"

GIT_CONFIG_DIR="$HOME/.config/git"
mkdir -p "$GIT_CONFIG_DIR"
Expand Down

0 comments on commit c29812a

Please sign in to comment.