Skip to content

Commit

Permalink
Merge pull request #3730 from ipfs/feat/branch-archive
Browse files Browse the repository at this point in the history
misc: add branch archiving script
  • Loading branch information
whyrusleeping authored Mar 2, 2017
2 parents 3ff5b74 + c635590 commit 1ef5ecb
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions bin/archive-branches.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -x

auth=""
#auth="-u kubuxu:$GH_TOKEN"
org=ipfs
repo=go-ipfs
arch_repo=go-ipfs-archived
api_repo="repos/$org/$repo"

exclusions=(
'master'
'release'
)

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 $auth -f -sD >(gh_api_next) $link
}

gh_api() {
curl $auth -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("^ipfs:"))' \
| sed 's/^ipfs://'
}

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]:$org/$arch_repo.git" || true

cat <(active_branches) <(pr_branches) <((IFS=$'\n'; echo "${exclusions[*]}")) \
| sort -u | comm - <(origin_refs | sort) -13 |\
while read ref; do
git push archived "origin/$ref:refs/heads/$ref/$(date --rfc-3339=date)"
git push origin --delete "$ref"
done

0 comments on commit 1ef5ecb

Please sign in to comment.