-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-gc.sh
More file actions
executable file
·42 lines (35 loc) · 1.14 KB
/
git-gc.sh
File metadata and controls
executable file
·42 lines (35 loc) · 1.14 KB
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
#!/usr/bin/env bash
for_each_git_dir() {
run_when_success="$1"
prefix="$2"
# Since the cache dir has 3 levels:
# Site level (github, gitlab, ...)
# User name level
# repo name level
for repo in ${prefix}/*/*/*; do
cd "${repo}" && eval "${run_when_success}"
done
}
sleep_until() {
target_hour="$1"
target_min="$2"
# In case of race condition:
# 1. After fetching hour when fetching min, hour and min changed. E.x., 16:59->17:00, the process will get 16:00.
# 2. Vice versa.
# In the first situation, the process may be sleep for an additional hour.
# In the second situation, the process may be sleep for an additional min.
HM=($(date +'%H %M'))
let diff_hour=target_hour-${HM[0]}
let diff_min=target_min-${HM[1]}
[ $diff_min -lt 0 ] && let diff_min=diff_min+60 && let diff_hour=diff_hour-1
[ $diff_hour -lt 0 ] && let diff_hour=diff_hour+24
sleep "${diff_hour}h" "${diff_min}m"
}
hour=1
[ -n "$HOUR" ] && hour=$HOUR
min=30
[ -n "$MIN" ] && min=$MIN
while true; do
sleep_until $hour $min
for_each_git_dir "git gc --aggressive" "/var/cache/git"
done