-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrecursive-gitconfig.sh
More file actions
54 lines (50 loc) · 1.89 KB
/
recursive-gitconfig.sh
File metadata and controls
54 lines (50 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
#
# recursive-gitconfig: Alias git to use closest .gitconfig file found in parents directories.
#
# Quick example:
#
# # Make dirs & .gitconfig files
# mkdir -p /tmp/gitenvs/john && mkdir /tmp/gitenvs/jack
# touch /tmp/gitenvs/john/.gitconfig && touch /tmp/gitenvs/jack/.gitconfig
#
# # Set values to config files
# git config -f /tmp/gitenvs/john/.gitconfig --add user.name 'John'
# git config -f /tmp/gitenvs/john/.gitconfig --add user.email 'john@local'
#
# git config -f /tmp/gitenvs/jack/.gitconfig --add user.name 'Jack'
# git config -f /tmp/gitenvs/jack/.gitconfig --add user.email 'jack@local'
#
# # Create git repositories under config files
# mkdir /tmp/gitenvs/john/repo/ && git --git-dir /tmp/gitenvs/john/repo/.git init
# mkdir /tmp/gitenvs/jack/repo/ && git --git-dir /tmp/gitenvs/jack/repo/.git init
#
# # Commit & test behaviour
# cd /tmp/gitenvs/john/repo && git commit --allow-empty --message "John's" && git log
# cd /tmp/gitenvs/jack/repo && git commit --allow-empty --message "Jacks's" && git log
#
# Docs:
# Please refere to project git repository for more infos. (https://github.com/arount/recursive-gitconfig)
#
# Look for closest .gitconfig file in parent directories
# This file will be used as main .gitconfig file.
function __recursive_gitconfig_git {
gitconfig_file=$(__recursive_gitconfig_closest)
if [ "$gitconfig_file" != '' ]; then
home="$(dirname $gitconfig_file)/"
HOME=$home "$(which git)" "$@"
else
"$(which git)" "$@"
fi
}
# Look for closest .gitconfig file in parents directories
function __recursive_gitconfig_closest {
slashes=${PWD//[^\/]/}
directory="$PWD"
for (( n=${#slashes}; n>0; --n ))
do
test -e "$directory/.gitconfig" && echo "$directory/.gitconfig" && return
directory="$directory/.."
done
}
alias git='__recursive_gitconfig_git'