-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-gc-all
executable file
·109 lines (75 loc) · 1.41 KB
/
git-gc-all
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
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
# set -o xtrace
declare SRC
main() {
set_src "$1"
pushd "${SRC}" >/dev/null
msg1 'Optimizing repos...'
gc_groups
popd >/dev/null
}
set_src() {
SRC="$1"
[[ -z "${SRC}" ]] && SRC="${PWD}"
return 0
}
gc_groups() {
local group
while read -r group; do
[[ ! -d "${group}" ]] && continue
pushd "${group}" >/dev/null
gc_group
popd >/dev/null
done < <(ls -A1)
}
gc_group() {
local repo
while read -r repo; do
[[ ! -d "${repo}" ]] && continue
pushd "${repo}" >/dev/null
gc_repo "${repo}"
popd >/dev/null
done < <(ls -A1)
}
gc_repo() {
local -r repo="$1"
! git_is_repo && return 0
msg2 "Optimizing '${repo}'..."
if ! git gc --aggressive; then
fail $? "Failed to optimize repo '${repo}' with 'git gc --aggressive'"
fi
return 0
}
git_is_repo() {
git branch &>/dev/null && return 0
return 1
}
fail() {
local -r scode="$1"
local -r msg="$2"
err "${msg}"
exit "${scode}"
}
err() {
local -r msg="$1"
echo "[$(timestamp)]: ${msg}" >&2
}
msg1() {
local -r msg="$1"
echo "[$(timestamp)]: ${msg}"
}
msg2() {
local -r msg="$1"
local -r GREEN="$(tput setaf 2)"
local -r RESET="$(tput sgr0)"
echo "${GREEN}[$(timestamp)]: ${msg}${RESET}"
}
timestamp() {
date +'%Y-%m-%d %H:%M:%S'
# date +'%Y-%m-%dT%H:%M:%S%z'
# date -u +'%Y-%m-%dT%H:%M:%SZ'
}
main "$@"