-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7ff1f5a
Showing
5 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#!/bin/bash | ||
|
||
case "$url" in | ||
*@github.com:* ) email=""; name="";; | ||
*//github.com/* ) email=""; name="";; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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". | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>" |