Can I configure git so it does not guess user.email configuration settings?

后端 未结 5 1897
余生分开走
余生分开走 2021-02-14 14:39

On my system I don\'t have the user.email git configuration value set at the global level, on purpose. Instead, I configure it individually in each sandbox. This is

5条回答
  •  鱼传尺愫
    2021-02-14 15:25

    The pre-commit solutions proposed here work well and I will accept one of them. I ended up using this:

    if [ -z $(git config user.email) ]; then
        echo "You need to set your user.email configuration value for this project"
        echo "git config user.email foo@example.com"
        exit 1
    fi
    

    It gets activated system-wide in my ~/.gitconfig file with the init.templatedir config value, which I saw mentioned in another Stack Overflow post. I put my template dir on github as part of my git-tools collection.

    Additionally, I realized that I can also just perform a check in my shell prompt logic, which already adds git status to the prompt, to print a big, fat warning when I cd into a sandbox that does not have the user.email value configured.

    In my .bashrc:

    PS1='\u@\h:\W$(parse_git_branch) \$ '
    
    parse_git_branch() {
        local DIRTY STATUS EMAIL_NOT_CONFIGURED_WARNING
        STATUS=$(git status 2>/dev/null)
        [ $? -eq 128 -o $? -eq 127 ] && return
        [ -z $(git config user.email) ] && EMAIL_NOT_CONFIGURED_WARNING=$' \x1b[31m(user.email not configured)\x1b[m'
        [[ "$STATUS" == *'working directory clean'* ]] || DIRTY=' *'
        echo "($(git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* //')$DIRTY)$EMAIL_NOT_CONFIGURED_WARNING"
    }
    

    This produces an easily visible warning:

    enter image description here

提交回复
热议问题