246 lines
6.3 KiB
Bash
Executable file
246 lines
6.3 KiB
Bash
Executable file
#!/bin/bash
|
|
|
|
# Get the current git config list
|
|
gc_ls="$(git config --list)"
|
|
|
|
# ANSI color codes
|
|
RED='\033[0;31m'
|
|
CYAN='\033[0;36m'
|
|
YELLOW='\033[0;33m'
|
|
GREEN='\033[0;32m'
|
|
BLUE='\033[0;34m'
|
|
LIGHT_GREEN='\033[0;92m'
|
|
BOLD='\033[1m'
|
|
NC='\033[0m' # No Color
|
|
|
|
echo_error() {
|
|
printf "${BOLD}${RED}ERROR: ${NC}${RED}%s${NC}\n" "$1" >&2
|
|
}
|
|
|
|
echo_warning() {
|
|
printf "${BOLD}${YELLOW}WARNING: ${NC}${YELLOW}%s${NC}\n" "$1"
|
|
}
|
|
|
|
echo_gls() {
|
|
printf "${BOLD}${YELLOW}%s${NC}\n" "$1"
|
|
}
|
|
|
|
echo_note() {
|
|
printf "${BOLD}${LIGHT_GREEN}NOTE: ${NC}${LIGHT_GREEN}%s${NC}\n" "$1"
|
|
}
|
|
|
|
# Function to print colored text
|
|
print_color() {
|
|
printf "%b%s%b\n" "$1" "$2" "$NC"
|
|
}
|
|
|
|
# Check if the given command exists silently
|
|
command_exists() {
|
|
command -v "$@" >/dev/null 2>&1
|
|
}
|
|
|
|
# Function to install a package if `curl` is available
|
|
if command_exists curl; then
|
|
install_pkg() {
|
|
sh -c "$(curl -sSL https://git.k4li.de/pika/scripts/raw/branch/main/bash/snippets/install_pkg.sh)" -- "$@"
|
|
}
|
|
else
|
|
echo_error "curl is not installed, universal install disabled!"
|
|
fi
|
|
|
|
# Function for barebones Git setup
|
|
gitBareBonesSetup() {
|
|
echo_gls "Please enter your username: "
|
|
read -r g_username
|
|
echo_gls "Please enter your email: "
|
|
read -r g_mail
|
|
echo_gls "Please enter your default branch name <e.g. main>"
|
|
read -r g_branch
|
|
git config --global user.name "$g_username"
|
|
git config --global user.email "$g_mail"
|
|
git config --global init.defaultBranch "$g_branch"
|
|
echo_note "Barebones Git setup complete!"
|
|
read </dev/tty
|
|
}
|
|
|
|
# Function for extended Git configuration
|
|
gitExtendedSetup() {
|
|
echo_gls "Please enter the default branch name (e.g., main): "
|
|
read -r g_branch
|
|
echo_gls "Please enter your preferred text editor (e.g., vim, nano, code): "
|
|
read -r g_editor
|
|
|
|
# Set default branch name
|
|
git config --global init.defaultBranch "$g_branch"
|
|
|
|
# Set preferred text editor
|
|
git config --global core.editor "$g_editor"
|
|
|
|
# Set some common aliases
|
|
git config --global alias.co "checkout"
|
|
git config --global alias.br "branch"
|
|
git config --global alias.ci "commit"
|
|
git config --global alias.st "status"
|
|
|
|
# Set default push behavior to simple (to avoid issues with push)
|
|
git config --global push.default simple
|
|
|
|
# Enable credential caching for 15 minutes (900 seconds)
|
|
git config --global credential.helper "cache --timeout=900"
|
|
|
|
# Add a global .gitignore file (optional)
|
|
echo_gls "Do you have a global .gitignore file? If so, please provide the path (or leave blank to skip): "
|
|
read -r g_gitignore
|
|
if [ -n "$g_gitignore" ] && [ -f "$g_gitignore" ]; then
|
|
git config --global core.excludesfile "$g_gitignore"
|
|
echo_note "Global .gitignore file set to $g_gitignore"
|
|
else
|
|
echo_warning "Skipping global .gitignore configuration!"
|
|
fi
|
|
|
|
echo_note "Extended Git setup complete!"
|
|
read </dev/tty
|
|
}
|
|
|
|
gitChangeMail() {
|
|
echo_gls "Please enter the new e-Mail: "
|
|
read -e -r new_mail
|
|
git config --global user.email "$new_mail"
|
|
echo_note "Email updated to $new_mail"
|
|
read </dev/tty
|
|
}
|
|
|
|
gitChangeUsername() {
|
|
currentName="$(git config --get user.name)"
|
|
echo_warning "Do you really want to change your username? (current username is $currentName) (y/N): "
|
|
read -e -r confirm
|
|
if [[ "$currentName" -eq ^"yY" ]]; then
|
|
echo_gls "Please enter the new Username: "
|
|
read -r new_username
|
|
git config --global user.name "$new_username"
|
|
echo_note "Username updated to $new_username"
|
|
read </dev/tty
|
|
fi
|
|
}
|
|
|
|
gitCredentialsAddAuthToken() {
|
|
local CREDENTIALS_FILE="$HOME/.git-credentials"
|
|
echo_gls "Enter the domain/server (e.g. git.k4li.de):"
|
|
read -r g_domain
|
|
|
|
echo_gls "Enter your username:"
|
|
read -r g_user
|
|
|
|
echo_gls "Enter your authentication token:"
|
|
read -r g_token # The '-s' flag hides input for privacy
|
|
|
|
# Append the new credentials to the file
|
|
echo "https://$g_user:$g_token@$g_domain" >>"$CREDENTIALS_FILE"
|
|
|
|
echo "Credentials added for $g_domain in $CREDENTIALS_FILE"
|
|
|
|
git config --global credential.helper store
|
|
|
|
chmod 600 "$HOME/.git-credentials"
|
|
|
|
read </dev/tty
|
|
}
|
|
|
|
# Display the current Git configuration
|
|
showGitConfig() {
|
|
echo_gls "This is your current git-config:"
|
|
echo_gls "─────────────────────────────────────────────────"
|
|
|
|
# Loop through each line of the git config list and format it nicely
|
|
printf "%s\n" "$gc_ls" | while IFS='=' read -r key value; do
|
|
# Extract section/category name (before the first period)
|
|
category="${key%%.*}"
|
|
# Extract setting name (after the first period)
|
|
setting="${key#*.}"
|
|
|
|
# Use different colors and styles for category and setting names
|
|
printf "${BOLD}${CYAN}%-20s${NC}: ${LIGHT_GREEN}%-30s${NC} ${YELLOW}%s${NC}\n" "$category" "$setting" "$value"
|
|
done
|
|
|
|
echo_gls "─────────────────────────────────────────────────"
|
|
|
|
read </dev/tty
|
|
}
|
|
|
|
# Main menu loop
|
|
main_menu() {
|
|
local selected=0
|
|
local key=""
|
|
|
|
while true; do
|
|
display_menu
|
|
|
|
# Read a single character
|
|
read -rsn1 key
|
|
|
|
case $key in
|
|
A | k) # Up arrow or k
|
|
((selected--))
|
|
if [[ $selected -lt 0 ]]; then
|
|
selected=$((${#options[@]} - 1))
|
|
fi
|
|
;;
|
|
B | j) # Down arrow or j
|
|
((selected++))
|
|
if [[ $selected -ge ${#options[@]} ]]; then
|
|
selected=0
|
|
fi
|
|
;;
|
|
q) # q for quit
|
|
exit 0
|
|
;;
|
|
"") # Enter key
|
|
case $selected in
|
|
0) gitBareBonesSetup ;;
|
|
1) gitExtendedSetup ;;
|
|
2) gitChangeMail ;;
|
|
3) gitChangeUsername ;;
|
|
4) gitCredentialsAddAuthToken ;;
|
|
5) showGitConfig ;;
|
|
6) exit 0 ;;
|
|
esac
|
|
;;
|
|
esac
|
|
done
|
|
}
|
|
|
|
# Menu options
|
|
declare -a options=(
|
|
"BareBones setup"
|
|
"Extended setup"
|
|
"Change e-Mail"
|
|
"Change username"
|
|
"Activate git-credentials and add Authentication token for any git server"
|
|
"Show the current git config"
|
|
"Exit"
|
|
)
|
|
|
|
# Function to display the menu
|
|
display_menu() {
|
|
clear
|
|
print_color "$BLUE" "=== Git setup tui ==="
|
|
echo_note "(navigate with j/k or up/down arrows - q to quit)"
|
|
for i in "${!options[@]}"; do
|
|
if [[ $i -eq $selected ]]; then
|
|
print_color "$GREEN" "> ${options[$i]}"
|
|
else
|
|
echo " ${options[$i]}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# Main function
|
|
main() {
|
|
if command_exists git; then
|
|
main_menu </dev/tty
|
|
else
|
|
install_pkg git && main
|
|
fi
|
|
}
|
|
|
|
main
|