diff --git a/.git-clone-init b/.git-clone-init new file mode 100644 index 0000000..4cae6b7 --- /dev/null +++ b/.git-clone-init @@ -0,0 +1,6 @@ +#!/bin/bash + +case "$url" in + *@github.com:* ) email=""; name="";; + *//github.com/* ) email=""; name="";; +esac diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..17eb83f --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2016 DrVanScott, https://github.com/DrVanScott + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..d6b9b25 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +# Automatic setup of user identity on git clone + +![Screenshot of a git clone](/about.png) + +Whenever a repository is cloned, author information (user.email, user.name) is set according defined patterns. No longer pushing commits with your corporate email address by accident. + +## Installation + +In case you do not already have a git template directory, create and register one: + +```bash +mkdir -p ~/.git-templates/hooks +git config --global init.templatedir ~/.git-templates +``` +Copy the file "post-checkout" to your registered git template directory: +```bash +cp post-checkout ~/.git-templates/hooks/ +``` + +## Configuration + +You can use the file ".git-clone-init" as a starting point. Keep in mind to create a pattern for each protokoll you are using, normally ssh and https. + +Example: +```bash +case "$url" in + *@github.com:* ) email="my-public@email"; name="public name";; + *//github.com/* ) email="my-public@email"; name="public name";; + *@corp.com:* ) email="my-corporate@email"; name="real name";; + *//corp.com/* ) email="my-corporate@email"; name="real name";; +esac +``` + +## Usage + +Just do a normal "git clone" as usual. + +## Known issues + +git-clone-init won't work if you clone using "-n". + diff --git a/about.png b/about.png new file mode 100644 index 0000000..f9f5bdf Binary files /dev/null and b/about.png differ diff --git a/post-checkout b/post-checkout new file mode 100755 index 0000000..4928daa --- /dev/null +++ b/post-checkout @@ -0,0 +1,53 @@ +#!/bin/bash + +#checkout hook to locally set user name and email based on user defined patterns +#The patterns are matched against the clone url. +# +#Based on http://www.dvratil.cz/2015/12/git-trick-628-automatically-set-commit-author-based-on-repo-url/ + +function warn { + echo -e "\n$1 Email and author not initialized in local config!" +} + +email="$(git config --local user.email)" +name="$(git config --local user.name)" + +if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then + exit 0 +fi + +#get remote name: +# only one: take it +# more: take "origin", or fail +remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")" + +if [[ -z $remote ]]; then + warn "Failed to detect remote." + exit 0 +fi + +url="$(git config --local remote.${remote}.url)" + +if [[ ! -f ~/.git-clone-init ]]; then +cat << INPUT > ~/.git-clone-init +#!/bin/bash + +case "\$url" in + *@github.com:* ) email=""; name="";; + *//github.com/* ) email=""; name="";; +esac +INPUT + warn "\nMissing file ~/.git-clone-init. Template created..." + exit 0 +fi +. ~/.git-clone-init + +if [[ -z $name || -z $email ]]; then + warn "Failed to detect identity using ~/.git-clone-init." + exit 0 +fi + +git config --local user.email "$email" +git config --local user.name "$name" + +echo -e "\nIdentity set to $name <$email>"