/
git-ignore
executable file
·63 lines (54 loc) · 1.14 KB
/
git-ignore
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
55
56
57
58
59
60
61
62
#!/bin/bash
#
# git-ignore
# Appends one or more patterns to a .gitignore file, optionally preceded by
# an explanatory message
#
# Put this script somewhere in your $PATH (perhaps /usr/bin),
# then cd into your working tree and run
# git ignore "pattern1" [pattern2] [pattern3] ... [-m "message"]
# Patterns are added to the .gitignore file without processing
if [ $# == 0 ]; then
echo 'Usage: git-ignore <pattern> [<pattern>...<pattern>] [-m "reason"]'
exit
fi
startdir="$(pwd)"
# Find .gitignore
while [ ! -d ".git" ]; do
local prev="$(pwd)"
cd ..
if [ "$(pwd)" == "$prev" ]
then
echo "Fatal: must be run in a valid working directory"
cd "$startdir"
exit
fi
done
gitignore="$(pwd)/.gitignore"
# Find / echo message
msg_next=0
for arg in "$@"; do
if [ $msg_next != 0 ]; then
message="$arg"
break
elif [ "$arg" == "-m" ]; then
msg_next=1
fi
done
echo "" >> "$gitignore"
if [ ! -z "$message" ]; then
echo "# $message" >> "$gitignore"
fi
# Echo patterns
ignore=0
for arg in "$@"; do
if [ $ignore != 0 ]; then
ignore=0
elif [ "$arg" == '-m' ]; then
ignore=1
else
echo "$arg"
echo "$arg" >> "$gitignore"
fi
done
cd $startdir