├── .gitattributes ├── LICENSE ├── README.md ├── about.png ├── git-clone-init └── post-checkout /.gitattributes: -------------------------------------------------------------------------------- 1 | git-clone-init text=auto eol=lf 2 | post-checkout text=auto eol=lf 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 DrVanScott, https://github.com/DrVanScott 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Automatic setup of user identity on git clone 2 | 3 | ![Screenshot of a git clone](/about.png) 4 | 5 | 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. 6 | 7 | ## Installation 8 | 9 | In case you do not already have a git template directory, create and register one: 10 | 11 | ```bash 12 | mkdir -p ~/.config/git/templates/hooks 13 | git config --global init.templatedir ~/.config/git/templates 14 | ``` 15 | Copy the file "post-checkout" to your registered git template directory: 16 | ```bash 17 | git clone https://github.com/DrVanScott/git-clone-init.git /tmp/git-clone-init 18 | cp /tmp/git-clone-init/post-checkout ~/.config/git/templates/hooks/ 19 | ``` 20 | Copy a configuration template: 21 | ```bash 22 | cp /tmp/git-clone-init/git-clone-init ~/.config/git/templates/ 23 | ``` 24 | ## Configuration 25 | 26 | You can use the file `git-clone-init` as a starting point. Keep in mind to create a pattern for each protocol you are using, normally ssh and https. 27 | 28 | Example: 29 | ```bash 30 | case "$url" in 31 | *@github.com:* ) email="my-public@email"; name="public name";; 32 | *//github.com/* ) email="my-public@email"; name="public name";; 33 | *@corp.com:* ) email="my-corporate@email"; name="real name";; 34 | *//corp.com/* ) email="my-corporate@email"; name="real name";; 35 | esac 36 | ``` 37 | 38 | ## Usage 39 | 40 | Just do a normal "git clone" as usual. 41 | 42 | ## Known issues 43 | 44 | git-clone-init won't work if you clone using "-n" or clone an empty repository. 45 | 46 | -------------------------------------------------------------------------------- /about.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DrVanScott/git-clone-init/ffd01883717986e9a719382266e1a7a8bf43b533/about.png -------------------------------------------------------------------------------- /git-clone-init: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | case "$url" in 4 | *@github.com:* ) email=""; name="";; 5 | *//github.com/* ) email=""; name="";; 6 | esac 7 | -------------------------------------------------------------------------------- /post-checkout: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | #checkout hook to locally set user name and email based on user defined patterns 4 | #The patterns are matched against the clone url. 5 | # 6 | #Based on http://www.dvratil.cz/2015/12/git-trick-628-automatically-set-commit-author-based-on-repo-url/ 7 | 8 | function warn { 9 | echo -e "\n$1 Email and author not initialized in local config!" 10 | } 11 | 12 | email="$(git config --local user.email)" 13 | name="$(git config --local user.name)" 14 | 15 | if [[ $1 != "0000000000000000000000000000000000000000" || -n $email || -n $name ]]; then 16 | exit 0 17 | fi 18 | 19 | #get remote name: 20 | # only one: take it 21 | # more: take "origin", or fail 22 | remote="$([[ $(git remote | wc -l) -eq 1 ]] && git remote || git remote | grep "^origin$")" 23 | 24 | if [[ -z $remote ]]; then 25 | warn "Failed to detect remote." 26 | exit 0 27 | fi 28 | 29 | url="$(git config --local remote.${remote}.url)" 30 | 31 | configpath="${XDG_CONFIG_HOME:-${HOME}/.config}/git/templates/git-clone-init" 32 | if [[ ! -f "$configpath" ]]; then 33 | cat << INPUT > "$configpath" 34 | #!/bin/bash 35 | 36 | case "\$url" in 37 | *@github.com:* ) email=""; name="";; 38 | *//github.com/* ) email=""; name="";; 39 | esac 40 | INPUT 41 | warn "\nMissing file $configpath. Template created..." 42 | exit 0 43 | fi 44 | . "$configpath" 45 | 46 | if [[ -z $name || -z $email ]]; then 47 | warn "Failed to detect identity using $configpath." 48 | exit 0 49 | fi 50 | 51 | git config --local user.email "$email" 52 | git config --local user.name "$name" 53 | 54 | echo -e "\nLocal identity for ${PWD##*/} set to \"$name <$email>\"" 55 | --------------------------------------------------------------------------------