#!/usr/bin/env bash# Configure gh as the git credential helper and set the git identity from the GitHub login.set-e# Exit on errorconfigure_identity(){# configure_identity — Configure gh as the git credential helper and set# git user.name / user.email from the gh logged-in user.## Logs in via `gh auth login` if not yet authenticated (interactive), runs# `gh auth setup-git`, then reads the authenticated user's profile via# `gh api user`:# - user.name ← profile name, falling back to the login# - user.email ← public profile email, falling back to the GitHub# noreply address <id>+<login>@users.noreply.github.com# The identity is written to the local (repository) git config.localloginnameemailuser_idghauthstatus>/dev/null2>&1||ghauthloginlogin=$(ghapiuser--jq.login)if[[-z"${login}"]];thenecho"Error: could not get GitHub authenticated username">&2return1finame=$(ghapiuser--jq.name)if[[-z"${name}"||"${name}"=="null"]];thenname="${login}"fiemail=$(ghapiuser--jq.email)if[[-z"${email}"||"${email}"=="null"]];thenuser_id=$(ghapiuser--jq.id)email="${user_id}+${login}@users.noreply.github.com"fighauthsetup-gitgitconfiguser.name"${name}"gitconfiguser.email"${email}"gitconfigsetadvice.skippedCherryPicksfalseprintf"Git identity configured from gh user '%s':\n""${login}"printf" user.name: %s\n""${name}"printf" user.email: %s\n""${email}"}if[["${BASH_SOURCE[0]}"=="${0}"]];thenconfigure_identityfi