-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprune.sh
executable file
·78 lines (67 loc) · 1.51 KB
/
prune.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
#!/usr/bin/env bash
dry_run=0
max_threads=5
active_threads=0
remote='github'
lockfile='threads'
if [[ "$#" -gt 1 ]]; then
echo 'Usage prune [--dry-run]'
exit 1
fi
if [[ -f $lockfile ]]; then
echo "lockfile $(pwd)/$lockfile already exists"
exit 1
fi
echo $active_threads > $lockfile
declare -a branches
function in_array {
local key="$1"
local exists=0
for ((i=0; i<n; i++)); do
branch="${branches[$i]}"
if [[ $branch == "$key" ]]; then
exists=1
break
fi
done
echo $exists
}
function delete_in_thread {
local branch="$1"
git push "$remote" --delete "$branch"
# this is needed because it runs in a background process
active_threads=$(< $lockfile)
((active_threads--))
if [[ active_threads -eq 0 ]]; then
rm $lockfile
else
echo "$active_threads" > $lockfile
fi
}
if [[ $1 == "--dry-run" ]]; then
dry_run=1
fi
n=0
for branch in $(git branch | sed 's/\*//g' ); do
branches[$n]="$branch"
((n++))
done
remote_branches="$(git ls-remote --head github | grep -Eo "([a-zA-Z0-9>_\-]*)$" )"
for branch in $remote_branches; do
sleep 0.1
exists=$(in_array "$branch")
if [[ $exists -eq 1 ]]; then
echo "skipping $remote/$branch"
else
echo "deleting $remote/$branch"
if [[ $dry_run -eq 0 ]]; then
active_threads=$(< $lockfile)
((active_threads++))
echo "$active_threads" > $lockfile
while [[ $active_threads -gt $max_threads ]]; do
active_threads=$(< $lockfile)
done
delete_in_thread "$branch" &
fi
fi
done