|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# clean-ci [--dry-run] [--since="relative date"] |
| 4 | +# |
| 5 | +# Cleanup the ceph-ci.git repository branch list by deleting any branch with a |
| 6 | +# HEAD commit date older than (by default) 3 months. |
| 7 | + |
| 8 | +set -e |
| 9 | + |
| 10 | +[email protected]:ceph/ceph-ci.git |
| 11 | +DRY_RUN=0 |
| 12 | +SINCE="3 months ago" |
| 13 | + |
| 14 | +function eprintf { |
| 15 | + formats="$1" |
| 16 | + shift |
| 17 | + printf "$formats"'\n' "$@" >&2 |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +function delete_refs { |
| 22 | + if [[ DRY_RUN -eq 0 ]]; then |
| 23 | + eprintf 'Deleting refs: %s' "$*" |
| 24 | + git push --quiet "$CEPH_CI" --delete "$@" |
| 25 | + else |
| 26 | + eprintf 'Would delete refs: %s' "$*" |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +function iterate_ci { |
| 31 | + cutoff_date=$(date -d "$SINCE" +%s) |
| 32 | + |
| 33 | + REFS=$(git ls-remote --refs --heads "$CEPH_CI") |
| 34 | + |
| 35 | + # Fetch the refs, shallow |
| 36 | + printf '%s' "$REFS" | \ |
| 37 | + cut -f2 | \ |
| 38 | + git fetch --quiet --depth=1 --refetch --filter=blob:none --stdin "$CEPH_CI" |
| 39 | + |
| 40 | + declare -a TO_DELETE |
| 41 | + |
| 42 | + # Examine each ref/commit |
| 43 | + while read commit_ref; do |
| 44 | + commit=$(cut -f1 <<<"$commit_ref") |
| 45 | + ref=$(cut -f2 <<<"$commit_ref") |
| 46 | + |
| 47 | + eprintf 'Examining %s (commit %s)' "$ref" "$commit" |
| 48 | + |
| 49 | + commit_date=$(git log -1 --format="%ct" "$commit") |
| 50 | + |
| 51 | + eprintf '\thas commit date: %s' $(date --iso-8601=sec --date="@$commit_date") |
| 52 | + |
| 53 | + if (( commit_date < cutoff_date )); then |
| 54 | + TO_DELETE+=("$ref") |
| 55 | + if [[ DRY_RUN -eq 0 ]]; then |
| 56 | + eprintf '\twill delete remote ref: %s' "$ref" |
| 57 | + else |
| 58 | + eprintf '\twould delete remote ref: %s' "$ref" |
| 59 | + fi |
| 60 | + if [[ ${#TO_DELETE[@]} -ge 100 ]]; then |
| 61 | + delete_refs "${TO_DELETE[@]}" |
| 62 | + TO_DELETE=() |
| 63 | + fi |
| 64 | + else |
| 65 | + eprintf '\tNOT deleting: %s' "$ref" |
| 66 | + fi |
| 67 | + done <<<"$REFS" |
| 68 | + |
| 69 | + delete_refs "${TO_DELETE[@]}" |
| 70 | +} |
| 71 | + |
| 72 | +function main { |
| 73 | + eval set -- $(getopt --name "$0" --options 'h' --longoptions 'help,dry-run,since:' -- "$@") |
| 74 | + |
| 75 | + while [ "$#" -gt 0 ]; do |
| 76 | + case "$1" in |
| 77 | + -h|--help) |
| 78 | + printf '%s: [--dry-run] [--since=<relative time>]\n' "$0" |
| 79 | + exit 0 |
| 80 | + ;; |
| 81 | + --dry-run) |
| 82 | + DRY_RUN=1 |
| 83 | + shift |
| 84 | + ;; |
| 85 | + --since) |
| 86 | + SINCE="$2" |
| 87 | + shift 2 |
| 88 | + ;; |
| 89 | + --) |
| 90 | + shift |
| 91 | + break |
| 92 | + ;; |
| 93 | + esac |
| 94 | + done |
| 95 | + |
| 96 | + iterate_ci |
| 97 | +} |
| 98 | + |
| 99 | +main "$@" |
0 commit comments