Finding Old Branches

List bug or feature branches that have not been modified in the past 90 days and that have been merged into other branches since they were last modified:

$ hg -q branches | \
    egrep '(bug_|feature_)' | \
    while read b; do \
      oldmerges=`hg log --template '{rev}\n' -r "children(last(branch('"$b"')) and not date('-90'))"`; \
      [ -n "$oldmerges" ] && echo $b ; \
    done

List branches as above, then close each branch with a commit dated 24 hours after the branch was last modified:

$ hg -q branches | \
    egrep '(bug_|feature_)' | \
    while read b; do \
      oldmerges=`hg log --template '{rev}\n' -r "children(last(branch('"$b"')) and not date('-90'))"`; \
      [ -n "$oldmerges" ] && echo $b ; \
    done | \
    while read b; do \
      d=`hg log --template '{date|hgdate}' -r "last(branch('"$b"'))"`; \
      closeat="$((${d%% *} + 86400)) ${d##* }"; \
      hg update "$b" && hg commit -m "Close obsolete branch $b" -d "$closeat" --close-branch; \
    done

Needless to say, do this in a spare clone and check the result carefully before pushing!