forked from TacoSpigot/TacoSpigot
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gc.sh
77 lines (75 loc) · 1.88 KB
/
gc.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
#!/bin/bash
printHelp() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -h, --help Print this help message"
echo " -O, --aggressive Run git gc --aggressive"
echo " -p, --prune Prune all loose objects"
echo " -r, --repack Repack all objects in the repository"
echo " -f, --force Imply --aggressive --prune --repack"
echo " -v, --verbose Show status output from git"
}
for arg in "$@"; do
case "$arg" in
-h|--help)
printHelp
;;
-O|--aggressive)
AGGRESSIVE=true
;;
-p|--prune)
PRUNE=true
;;
-r|--repack)
REPACK=true
;;
-f|--force)
AGGRESSIVE=true
PRUNE=true
REPACK=true
;;
-v|--verbose)
VERBOSE=true
;;
*)
echo "Unknown option: $arg" >&2
printHelp >&2
exit 1
;;
esac
done
shopt -s globstar nullglob
REPOSITORIES=()
for git_dir in **/.git; do
repo="$(dirname $git_dir)"
repo="$(realpath --relative-to=. $repo)" # Relativize\
REPOSITORIES+=("$repo")
done;
OPTIONS=()
REPACK_OPTIONS=("-ad")
if [ $AGGRESSIVE ]; then
OPTIONS+=("--aggressive");
fi;
if [ $PRUNE ]; then
OPTIONS+=("--prune=now");
fi
if [ ! $VERBOSE ]; then
OPTIONS+=("-q")
REPACK_OPTIONS+=("-q")
fi
echo "Using git gc" "${OPTIONS[@]}"
if [ $REPACK ]; then
echo "Using git repack" "${REPACK_OPTIONS[@]}"
fi;
for repo in "${REPOSITORIES[@]}"; do
pushd "$repo" > /dev/null
echo "Cleaning $repo"
git gc "${OPTIONS[@]}" || { popd & exit 1; }
if [ $REPACK ]; then
if [ $VERBOSE ]; then
echo "Repacking $repo"
fi
git repack "${REPACK_OPTIONS[@]}" || { popd & exit 1; }
fi;
popd > /dev/null
done;