-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_git.sh
executable file
·77 lines (71 loc) · 1.44 KB
/
clean_git.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
#!/usr/bin/env bash
get_size() {
du -shck . | tail -n 1 | cut -f 1
}
clean() {
silent="no"
if [ "$1" == "--quiet" ] || [ "$2" == "--quiet" ]; then
silent="yes"
fi
if [ "${silent}" == "no" ]; then
if [ "$1" == "." ]; then
echo "$(basename ${PWD})"
else
echo "$1"
fi
fi
before="$(get_size)"
git clean -dfx
git gc --aggressive --prune=now
git submodule foreach clean_git . --quiet
git repack -Ad --depth=4095 --window=5000
after="$(get_size)"
if [ "${silent}" == "no" ]; then
echo "$1: ${before}k -> ${after}k"
echo ""
fi
}
loop() {
builtin cd "$1"
git rev-parse 2>/dev/null
if [ $? -eq 0 ]; then
clean "$@"
else
for d in $(find . -mindepth 1 -maxdepth 1 -type d); do
if [ "$d" != "./.git" ]; then
loop "$d" "$2"
fi
done
fi
if [[ "$1" != "." ]]; then
builtin cd ".."
fi
}
if [[ "$@" == "" ]]; then
if [ "${GITDIR}" == "" ]; then
echo "The GITDIR env variable is empty. Please set it to point somewhere"
exit 1
fi
base_path="$(pwd)"
builtin cd "${GITDIR}"
before_g="$(get_size)"
loop "."
after_g="$(get_size)"
builtin cd "${base_path}"
else
before_g="$(get_size)"
silent="no"
for d in "$@"; do
if [ "$d" == "--quiet" ]; then silent="yes"; fi
done
for d in "$@"; do
if [ "$d" == "--quiet" ]; then continue; fi
if [ "${silent}" == "no" ]; then
loop "$d"
else
loop "$d" --quiet
fi
done
after_g="$(get_size)"
fi
echo "Saved $((${before_g} - ${after_g}))k (${before_g}k -> ${after_g})k"