Skip to content

Commit 7ff1f5a

Browse files
committed
initial release
0 parents  commit 7ff1f5a

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed

.git-clone-init

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
case "$url" in
4+
*@github.com:* ) email=""; name="";;
5+
*//github.com/* ) email=""; name="";;
6+
esac

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 ~/.git-templates/hooks
13+
git config --global init.templatedir ~/.git-templates
14+
```
15+
Copy the file "post-checkout" to your registered git template directory:
16+
```bash
17+
cp post-checkout ~/.git-templates/hooks/
18+
```
19+
20+
## Configuration
21+
22+
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.
23+
24+
Example:
25+
```bash
26+
case "$url" in
27+
*@github.com:* ) email="my-public@email"; name="public name";;
28+
*//github.com/* ) email="my-public@email"; name="public name";;
29+
*@corp.com:* ) email="my-corporate@email"; name="real name";;
30+
*//corp.com/* ) email="my-corporate@email"; name="real name";;
31+
esac
32+
```
33+
34+
## Usage
35+
36+
Just do a normal "git clone" as usual.
37+
38+
## Known issues
39+
40+
git-clone-init won't work if you clone using "-n".
41+

about.png

37.7 KB
Loading

post-checkout

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
if [[ ! -f ~/.git-clone-init ]]; then
32+
cat << INPUT > ~/.git-clone-init
33+
#!/bin/bash
34+
35+
case "\$url" in
36+
*@github.com:* ) email=""; name="";;
37+
*//github.com/* ) email=""; name="";;
38+
esac
39+
INPUT
40+
warn "\nMissing file ~/.git-clone-init. Template created..."
41+
exit 0
42+
fi
43+
. ~/.git-clone-init
44+
45+
if [[ -z $name || -z $email ]]; then
46+
warn "Failed to detect identity using ~/.git-clone-init."
47+
exit 0
48+
fi
49+
50+
git config --local user.email "$email"
51+
git config --local user.name "$name"
52+
53+
echo -e "\nIdentity set to $name <$email>"

0 commit comments

Comments
 (0)