Skip to content

Commit

Permalink
feat(wsl/.gitconfig): include global git config (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
risu729 authored Nov 14, 2024
1 parent 80bd508 commit a241dfc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion win/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $powertoys_backup_dir = "\\wsl.localhost\$distribution\home\$wsl_username\github
# cspell:ignore hkcu
Set-ItemProperty -Path HKCU:Software\Microsoft\PowerToys -Name SettingsBackupAndRestoreDir -Value "$powertoys_backup_dir"

# setup git config after browser is installed
# setup git after browser is installed
# use -i, interactive mode
# need to source .bashrc to update PATH
wsl /usr/bin/env bash -ic "source ~/.bashrc; ~/github/dotfiles/wsl/setup-git.ts"
22 changes: 22 additions & 0 deletions wsl/.gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[init]
defaultBranch = main

[fetch]
prune = true

[commit]
gpgSign = true
[tag]
gpgSign = true

[credential "https://github.com"]
# set helper to empty to avoid using the default helper
# ref: https://github.com/cli/cli/issues/3796#issuecomment-1065150465
helper =
helper = !gh auth git-credential
[credential "https://gist.github.com"]
helper =
helper = !gh auth git-credential

[include]
path = ~/.gitconfig.local
23 changes: 9 additions & 14 deletions wsl/setup-git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { $, env, file, spawn, write } from "bun";

const localGitConfigPath = "~/.gitconfig.local";

// do not use Partial as it sets all properties to optional but doesn't allow undefined
type DeepOptional<T> = {
[P in keyof T]: T[P] extends (infer U)[]
Expand Down Expand Up @@ -161,7 +163,7 @@ const setGitUserConfig = async (): Promise<{
name: string | null;
login: string;
}>("/user");
await $`git config --global user.name ${name ?? login}`.quiet();
await $`git config --file ${localGitConfigPath} user.name ${name ?? login}`.quiet();

const noReplyEmail = await ghApi<{ email: string }[]>("/user/emails").then(
(emails) =>
Expand All @@ -172,7 +174,7 @@ const setGitUserConfig = async (): Promise<{
if (!noReplyEmail) {
throw new Error("Failed to get GitHub-provided no-reply email");
}
await $`git config --global user.email ${noReplyEmail}`.quiet();
await $`git config --file ${localGitConfigPath} user.email ${noReplyEmail}`.quiet();
return { githubId: login, email: noReplyEmail };
};

Expand Down Expand Up @@ -1120,17 +1122,11 @@ const configureGitSign = async (
email: string,
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: too many steps to configure
): Promise<void> => {
// cspell:ignore openpgp
// use the default format, openpgp
await $`git config --global --unset gpg.format`.quiet().nothrow();
// cspell:ignore gpgsign
await $`git config --global commit.gpgSign true`.quiet();
await $`git config --global tag.gpgSign true`.quiet();

// cspell:ignore signingkey
const gitSigningKey = await $`git config --global user.signingkey`
.nothrow()
.text();
const gitSigningKey =
await $`git config --file ${localGitConfigPath} user.signingkey`
.nothrow()
.text();
const keyringSecretKeys = (await getGpgKeyringSecretKeys())
// exclude if the key doesn't contain any secret keys to ignore imported public keys
.filter(
Expand Down Expand Up @@ -1352,11 +1348,10 @@ const configureGitSign = async (
// however, to clarify which subkey is used, specify the subkey id
// don't use fingerprint because it cannot be retrieved from github api
// ! is required to specify subkey as `--local-user` in gpg, which git uses internally
await $`git config --global user.signingkey ${keyringKey.subkey.fingerprint}!`.quiet();
await $`git config --file ${localGitConfigPath} user.signingkey ${keyringKey.subkey.fingerprint}!`.quiet();
};

const main = async (): Promise<void> => {
await $`git config --global init.defaultBranch main`.quiet();
const removeScopes = await ensureGitHubTokenScopes();

try {
Expand Down

0 comments on commit a241dfc

Please sign in to comment.