-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-ignore
executable file
·62 lines (58 loc) · 1.61 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
#!/usr/bin/env bash
_parse_arguments() {
local arg
while (($# > 0)); do
arg="$1"
case "$arg" in
--root | -R)
_gitignore_location="${_repo_path}/.gitignore"
;;
--exclude | -e)
_gitignore_location="${_repo_path}/.git/info/exclude"
_use_git_exclude="yes"
;;
--*)
echo "'$arg' is not a valid option"
_invalid_option="yes"
break
;;
-*)
echo "'$arg' is not a valid option"
_invalid_option="yes"
break
;;
*)
patterns+=("$arg")
;;
esac
shift
done
if [[ $_invalid_option == "yes" ]]; then
return 1
fi
return 0
}
help_message() {
echo "USAGE: git ignore <files> <to> <ignore>"
exit 1
}
main() {
((!$#)) && help_message && exit 1
_repo_path=$(git rev-parse --show-toplevel 2>/dev/null)
_dotgit_folder="${_repo_path:-/non/existing}/.git"
if [[ -z ${_repo_path} || ! -e "${_dotgit_folder}" ]]; then
echo "fatal: not a git repository (or any of the parent directories): .git"
return 1
fi
_parse_arguments "$@" || return 1
_gitignore_location="${_gitignore_location:-${PWD}/.gitignore}"
if [[ ! -f ${_gitignore_location} ]]; then
touch "${_gitignore_location}"
fi
for pattern in ${patterns[*]}; do
echo -n "Ignored '${pattern}'"
[[ ${_use_git_exclude:-no} == "yes" ]] && echo -n " locally only"
echo "${pattern}" | tee -a "${_gitignore_location}" >/dev/null
done
}
main "$@"