This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
/
gitignore-in-submodules.sh
executable file
·98 lines (85 loc) · 1.8 KB
/
gitignore-in-submodules.sh
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/bin/bash
# This script requires bash because of the <() construct
FIJI_ROOT="$(cd "$(dirname "$0")"/.. && pwd)"
EXCLUDE=.git/info/exclude
all_submodules () {
git --git-dir="$FIJI_ROOT/.git" ls-files --stage |
sed -n 's/^160000 .\{40\} 0.//p'
}
submodule_exists () {
test -d "$FIJI_ROOT/$1/.git"
}
exclude_exists () {
test -d "$FIJI_ROOT/$1/$EXCLUDE"
}
get_gitignore_from_submodule () {
(cd "$FIJI_ROOT/$1/" &&
if test -f $EXCLUDE
then
grep -v '^#' < $EXCLUDE || :
fi &&
git ls-files --exclude-standard --others --directory) |
sed "s|^|/$1/|"
}
get_gitignore_for_submodule () {
sed -n "s|^/$1/||p" < "$FIJI_ROOT/.gitignore"
}
update_submodules_exclude () {
if test ! -f "$FIJI_ROOT/$1/$EXCLUDE"
then
: > "$FIJI_ROOT/$1/$EXCLUDE"
fi &&
NEW="$(comm -13 <(sort < "$FIJI_ROOT/$1/$EXCLUDE" | uniq) \
<(get_gitignore_for_submodule "$1" | sort))" &&
if test -n "$NEW"
then
echo "$NEW" >> "$FIJI_ROOT/$1/$EXCLUDE"
fi
}
update_gitignore () {
NEW="$(comm -13 <(sort < "$FIJI_ROOT/.gitignore" | uniq) \
<(get_gitignore_from_submodule "$1" | sort))" &&
if test -n "$NEW"
then
echo "$NEW" >> "$FIJI_ROOT/.gitignore"
fi
}
die () {
echo "$*" >&2
exit 1
}
test $# = 0 && die "Usage: $0 (gitignore | submodule) [<submodule>...]"
case "$1" in
gitignore|submodule|ignoredirty)
mode="$1" &&
shift
;;
*)
die "Need to know what to update: 'gitignore' or 'submodule'"
;;
esac
if test $# = 0
then
set $(all_submodules)
fi &&
for submodule in "$@"
do
submodule=${submodule%/}
if $(submodule_exists "$submodule")
then
case "$mode" in
gitignore)
update_gitignore "$submodule"
;;
submodule)
update_submodules_exclude "$submodule"
;;
ignoredirty)
git config submodule."$submodule".ignore dirty
;;
*)
false
;;
esac || die "Could not handle submodule $submodule"
fi
done