1
1
#! /usr/bin/env bash
2
2
3
- : ${ GIT_DIR: = $(git rev-parse --git-dir)}
3
+ GIT_DIR=$( git rev-parse --git-dir 2> /dev/null )
4
4
5
5
function show_contents {
6
6
local file=" ${2/# ~/ $HOME } "
@@ -11,18 +11,41 @@ function show_contents {
11
11
fi
12
12
}
13
13
14
+ function cd_to_git_root {
15
+ local error_level=" $1 "
16
+
17
+ if ! git rev-parse --git-dir & > /dev/null; then
18
+ if [ " $error_level " = ' --warn' ]; then
19
+ echo " Warning: Not currently in a Git repository" >&2
20
+ elif [ " $error_level " = ' --error' ]; then
21
+ echo " Error: Not currently in a Git repository" >&2
22
+ exit 1
23
+ fi
24
+ fi
25
+
26
+ local result=
27
+ if result=$( git rev-parse --show-toplevel 2> /dev/null) ; then
28
+ cd " $result " || exit
29
+ fi
30
+ }
31
+
14
32
function global_ignore() {
15
- git config --global core.excludesFile || \
16
- ([ -n " $XDG_CONFIG_HOME " ] && echo " $XDG_CONFIG_HOME /git/ignore" ) || \
17
- echo " $HOME /.config/git/ignore"
33
+ if ! git config --global core.excludesFile 2> /dev/null; then
34
+ if [ -f " $HOME /.gitignore" ]; then
35
+ echo " $HOME /.gitignore"
36
+ else
37
+ echo " ${XDG_CONFIG_HOME:- $HOME / .config} /git/ignore"
38
+ fi
39
+ fi
18
40
}
19
41
20
42
function show_global {
21
43
show_contents Global " $( global_ignore) "
22
44
}
23
45
24
46
function add_global {
25
- local global_gitignore=" $( global_ignore) "
47
+ local global_gitignore
48
+ global_gitignore=" $( global_ignore) "
26
49
if [ -z " $global_gitignore " ]; then
27
50
echo " Can't find global .gitignore."
28
51
echo " "
@@ -34,22 +57,22 @@ function add_global {
34
57
}
35
58
36
59
function show_local {
37
- cd " $( git root ) "
60
+ cd_to_git_root --warn
38
61
show_contents Local .gitignore
39
62
}
40
63
41
64
function add_local {
42
- cd " $( git root ) "
65
+ cd_to_git_root --warn
43
66
add_patterns .gitignore " $@ "
44
67
}
45
68
46
69
function show_private {
47
- cd " $( git root ) "
70
+ cd_to_git_root --error
48
71
show_contents Private " ${GIT_DIR} /info/exclude"
49
72
}
50
73
51
74
function add_private {
52
- cd " $( git root ) "
75
+ cd_to_git_root --error
53
76
test -d " ${GIT_DIR} /info" || mkdir -p " ${GIT_DIR} /info"
54
77
add_patterns " ${GIT_DIR} /info/exclude" " $@ "
55
78
}
0 commit comments