]> code.delx.au - monosys/blob - bin/git-cleanup
notes: fix raspi install notes, also @home -> @username
[monosys] / bin / git-cleanup
1 #!/bin/bash
2
3 set -eu
4
5 function usage {
6 echo "Usage: $0 [--remote origin] [--age ndays]"
7 echo
8 echo "This tool will not change your repository, the output is a list of git push commands you can use to delete old branches."
9 echo
10 echo "Note that the arguments must be passed in the order listed above."
11 exit 1
12 }
13
14 if [ "${1:-}" = "--help" ]; then
15 usage
16 fi
17
18 remote="origin"
19 if [ "${1:-}" = "--remote" ]; then
20 remote="$2"
21 shift
22 shift
23 fi
24
25 age_days=30
26 if [ "${1:-}" = "--age" ]; then
27 age_days="$2"
28 shift
29 shift
30 fi
31 age_seconds=$((age_days*24*3600))
32
33 if [ -n "${1:-}" ]; then
34 usage
35 fi
36
37
38 echo "## Fetching latest changes from $remote"
39 git fetch -p "${remote}"
40
41
42 echo "## Constructing list of revisions in master and tags"
43 safe_revs_file="$(mktemp -t gitcleanup.XXXXXX)"
44 git rev-list origin/master --tags > "$safe_revs_file"
45
46
47 echo "## Checking for branches to delete"
48 now="$(date +%s)"
49 git ls-remote --heads "$remote" | while read line; do
50 set $line
51 rev="$1"
52 branch="$2"
53 timestamp="$(git rev-list --format=format:'%ct' --max-count=1 "$rev"|tail -n1)"
54 age=$((now-timestamp))
55
56 if [ "$branch" = "refs/heads/master" ]; then
57 continue;
58 fi
59
60 if grep -q "$rev" "$safe_revs_file"; then
61 echo git push "$remote" ":$branch" "# remove merged into master or tag"
62 continue
63 fi
64
65 if [ "$age" -gt "$age_seconds" ]; then
66 branch_name="${branch##refs/heads/}"
67 echo git tag "archived/$branch_name" "$rev" "# create tag for older than $age_days days branch"
68 echo git push "$remote" tag "archived/$branch_name" "# push tag for older than $age_days days branch"
69 echo git push "$remote" ":$branch" "# remove older than $age_days days"
70 continue
71 fi
72 done
73
74 rm -f "$safe_revs_file"
75