Skip to content

Instantly share code, notes, and snippets.

@ErnestoCobos
Created February 20, 2024 14:27
Show Gist options
  • Select an option

  • Save ErnestoCobos/b1878522c7faae5143e47a489f06708a to your computer and use it in GitHub Desktop.

Select an option

Save ErnestoCobos/b1878522c7faae5143e47a489f06708a to your computer and use it in GitHub Desktop.

Revisions

  1. ErnestoCobos created this gist Feb 20, 2024.
    28 changes: 28 additions & 0 deletions addGitConfig.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/bin/bash

    # This script updates Git configurations for different projects stored in a JSON file within the user's .config directory

    # Define the path to the JSON configuration file located in the .config directory of the user's home directory
    CONFIG_FILE="$HOME/.config/git-configs.json"

    # Check if the configuration file exists. If it does not, create a new JSON file with empty object initialization
    if [ ! -f "$CONFIG_FILE" ]; then
    echo "{}" > "$CONFIG_FILE"
    fi

    # Capture the current working directory
    DIRECTORY=$(pwd)

    # Prompt the user to enter their Git name and email for configuration
    read -p "Enter your Git name: " NAME
    read -p "Enter your Git email: " EMAIL

    # Use jq to add or update the directory-specific Git configuration in the JSON file
    # jq is a lightweight and flexible command-line JSON processor
    # The script adds or updates an entry for the current directory with the user's Git name, email, and a flag 'applied' set to 'false'
    # This entry signifies that the Git configuration specified has not yet been applied to the directory
    jq --arg dir "$DIRECTORY" --arg name "$NAME" --arg email "$EMAIL" \
    '.[$dir] = {"name": $name, "email": $email, "applied": "false"}' "$CONFIG_FILE" > temp.$$ && mv temp.$$ "$CONFIG_FILE"

    # Inform the user that the Git configuration has been updated for the current directory
    echo "Git configuration updated for $DIRECTORY"