-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclean_git.sh
executable file
·84 lines (76 loc) · 1.56 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
78
79
80
81
82
83
84
#!/usr/bin/env bash
get_size() {
to_do="$@"
if [ "${to_do}" == "" ]; then
to_do="."
fi
du -shck ${to_do} | tail -n 1 | cut -f 1
}
clean() {
if [ "$2" != "--quiet" ]; 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 [ "$2" != "--quiet" ]; 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
}
dir_to_do=""
silent="not_quiet"
for d in "$@"; do
if [ "$d" == "--quiet" ]; then
silent="quiet"
elif [ "$d" == "." ]; then
dir_to_do+=" ${PWD}"
else
dir_to_do+=" ${d}"
fi
done
base_path="${PWD}"
if [[ "${dir_to_do}" == "" ]]; then
if [ "${GITDIR}" == "" ]; then
echo "The GITDIR env variable is empty. Please set it to point somewhere"
exit 1
fi
dir_to_do="${GITDIR}"
else
dir_to_do="${dir_to_do:1}"
fi
if [ "${silent}" == "not_quiet" ]; then
before_g=$(get_size "${dir_to_do}")
fi
for d in "${dir_to_do}"; do
if [ "$d" == "--quiet" ]; then continue; fi
loop "$d" "--${silent}"
done
if [ "${silent}" == "not_quiet" ]; then
after_g=$(get_size "${dir_to_do}")
echo "Saved $((${before_g} - ${after_g}))k (${before_g}k -> ${after_g})k"
fi
builtin cd "${base_path}"