]>
code.delx.au - monosys/blob - git-cleanup
6 echo "Usage: $0 [--remote origin] [--age ndays]"
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."
10 echo "Note that the arguments must be passed in the order listed above."
14 if [ "${1:-}" = "--help" ]; then
19 if [ "${1:-}" = "--remote" ]; then
26 if [ "${1:-}" = "--age" ]; then
31 age_seconds
=$
((age_days
*24*3600))
33 if [ -n "${1:-}" ]; then
38 echo "## Fetching latest changes from $remote"
39 git fetch
-p "${remote}"
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"
47 echo "## Checking for branches to delete"
49 git ls-remote
--heads "$remote" |
while read line
; do
53 timestamp
="$(git rev-list --format=format:'%ct' --max-count=1 "$rev"|tail -n1)"
54 age
=$
((now-timestamp
))
56 if [ "$branch" = "refs/heads/master" ]; then
60 if grep -q "$rev" "$safe_revs_file"; then
61 echo git push
"$remote" ":$branch" "# remove merged into master or tag"
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"
74 rm -f "$safe_revs_file"