TheOperator TheOperator - 1 month ago 6
Git Question

Git config with directory scope, containing multiple repositories

The situation is as follows:


  • I have multiple domains in which I write code, e.g. professional and free time. Those are represented by different directories on my computer.

  • Each of those domains contains multiple Git repositories, in a hierarchical directory structure inside one of the domain directories.



Per domain, I want to use a different email address as part of author/committer information. That is, I want my private address to be listed in my free-time projects and my company address in my professional ones.

git config
knows 3 scopes: repository, global and system-wide. What I basically need is a 4th scope between repository and global, representing a group of repositories (or simply a directory in the file system).

It seems like
git config
doesn't allow that. Of course I could set the email address per repository, but I want to avoid this manual step every time I set up or clone a repository. One option would be to write a script that wraps
git init/clone
and
git config
, are there other ideas?

Answer

The solution I came up with is inspired from Scott Weldon's answer. Since it was not directly applicable for my case, I adapted the hook's bash script and improved several parts*.

Assume the following directory structure from the home directory:

~
   .gitconfig           // [init] templatedir
   .git-template
      hooks
         post-checkout  // our bash script
   MyDomain
      .gitconfig        // [user] name, email

Initially I let Git know where my template directory is. On Windows, you may need to specify the absolute path instead (C:/Users/MyUser/.git-template).

git config --global init.templatedir '~/.git-template'

In ~/MyDomain/.gitconfig I store the configuration for that directory (domain), which should be applied to all repositories inside it and its subdirectories.

cd ~/MyDomain
git config --file .gitconfig user.name "My Name"
git config --file .gitconfig user.email "my@email.com"

The interesting part is the post-checkout bash script, which defines the post-checkout hook. I used a custom user.inferredConfig flag to execute it only once (on git clone), not repeatedly (on git checkout). It would of course also be possible to create a separate file to represent that state.

#!/bin/bash

# Git post-checkout hook for automated use of directory-local git config
# http://stackoverflow.com/a/40450106

# Check for custom git-config flag, to execute hook only once on clone, not repeatedly on every checkout
if grep -q "inferredConfig" .git/config
then
    exit
fi

# Automatically set Git config values from parent folders.
echo "Infer Git configuration from directory..."

# Go upwards in directory hierarchy, examine all .gitconfig files we find
# Allows to have multiple nested .gitconfig files with different scopes
dir=$(pwd)
configFiles=()
while [ "$dir" != "/" ]
do
    # Skip first directory (the newly created Git repo)
    dir=$(dirname "$dir")
    if [ -f "$dir/.gitconfig" ]
    then
        configFiles+=("$dir/.gitconfig")
    fi
done

# Iterate through configFiles array in reverse order, so that more local configurations override parent ones
for (( index=${#configFiles[@]}-1 ; index>=0 ; index-- )) ; do
    gitconfig="${configFiles[index]}"

    echo "* From $gitconfig:"
    # Iterate over each line in found .gitconfig file
    output=$(git config --file "$gitconfig" --list)
    while IFS= read -r line
    do
        # Split line into two parts, separated by '='
        IFS='=' read key localValue <<< "$line"

        # For values that differ from the parent Git configuration, adjust the local one
        parentValue=$(git config $key)
        if [ "$parentValue" != "$localValue" ]
        then
            echo "  * $key: $localValue"
            git config "$key" "$localValue"
        fi
    done <<< "$output"

    # Set custom flag that we have inferred the configuration, so that future checkouts don't need to do it
    git config user.inferredConfig 1
done

*: The changes from the original code include:

  1. Works with spaces in paths (especially interesting on Windows)
  2. Parses key-value pairs from .gitconfig correctly (don't read lines with for, iterate with while read instead)
  3. Checks .gitconfig files from root to local directory, not vice-versa
  4. Invokes hook only at initial clone, not at every checkout
  5. Output the config settings that are applied on git clone