-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy patharchive-branches.sh
executable file
·68 lines (51 loc) · 1.56 KB
/
archive-branches.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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
org=filecoin-project
repo=lotus
arch_repo="$org/lotus-archived"
api_repo="repos/$org/$repo"
exclusions=(
'master'
'main'
'releases'
)
gh_api_next() {
links=$(grep '^link:' | sed -e 's/link: //' -e 's/, /\n/g')
echo "$links" | grep '; rel="next"' >/dev/null || return
link=$(echo "$links" | grep '; rel="next"' | sed -e 's/^<//' -e 's/>.*//')
curl -n -f -sD >(gh_api_next) "$link"
}
gh_api() {
curl -n -f -sD >(gh_api_next) "https://api.github.com/$1" | jq -s '[.[] | .[]]'
}
pr_branches() {
gh_api "$api_repo/pulls" | jq -r '.[].head.label | select(test("^'"$org"':"))' \
| sed 's/^'"$org"'://'
}
origin_refs() {
format=${1-'%(refname:short)'}
git for-each-ref --format "$format" refs/remotes/origin | sed 's|^origin/||'
}
active_branches() {
origin_refs '%(refname:short) %(committerdate:unix)' |awk \
' BEGIN { monthAgo = systime() - 31*24*60*60 }
{ if ($2 > monthAgo) print $1 }
'
}
git remote add archived "[email protected]:$arch_repo.git" || true
branches_to_move="$(cat <(active_branches) <(pr_branches) <((IFS=$'\n'; echo "${exclusions[*]}")) | sort -u | comm - <(origin_refs | sort) -13 | grep -v -e '^release/' -e '^ntwk-')"
echo "================"
printf "%s\n" "$branches_to_move"
echo "================"
echo "Please confirm move of above branches [y/N]:"
read -r line
case "$line" in
[Yy]|[Yy][Ee][Ss]) ;;
*) exit 1 ;;
esac
printf "%s\n" "$branches_to_move" | \
while read -r ref; do
git push archived "origin/$ref:refs/heads/$ref/$(date --rfc-3339=date)"
git push origin --delete "$ref"
done