Skip to content

Commit 1e7fd25

Browse files
committed
Add a helper script to clean up .gitignore files
Yours truly uses this script to clean up Fiji's .gitignore file. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent afce6bc commit 1e7fd25

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

validate-gitignore.sh

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/bin/sh
2+
3+
# This script can be used to identify unnecessary entries in the .gitignore
4+
# file.
5+
#
6+
# Three modes are available:
7+
#
8+
# --validate
9+
# prints out unnecessary entries
10+
# --clean
11+
# prints out the contents of .gitignore, skipping unnecessary entries
12+
# --fix
13+
# overwrites .gitignore with the output of --clean
14+
15+
die () {
16+
echo "$*" >&2
17+
exit 1
18+
}
19+
20+
while test $# -gt 0
21+
do
22+
case "$1" in
23+
--fix|--validate|--clean) mode=${1#--};;
24+
*) die "Unknown command: $1";;
25+
esac
26+
shift
27+
done
28+
29+
test -f .gitignore ||
30+
die "No .gitignore file?"
31+
32+
handle_line () {
33+
case "$1" in
34+
''|\#*)
35+
printf '%s\n' "$1"
36+
continue
37+
;;
38+
/*)
39+
if eval ls -d ."$1" > /dev/null 2>&1
40+
then
41+
printf "%s\n" "$1"
42+
elif test "validate" = "$mode"
43+
then
44+
echo "Unnecessary: $1" 1>&2
45+
fi
46+
;;
47+
esac
48+
}
49+
50+
cleaned="$(cat .gitignore |
51+
while read line
52+
do
53+
handle_line "$line"
54+
done)"
55+
56+
case "$mode" in
57+
fix)
58+
printf "%s" "$cleaned" > .gitignore
59+
;;
60+
clean)
61+
printf "%s" "$cleaned"
62+
;;
63+
esac

0 commit comments

Comments
 (0)