Search code examples
bashgitversion-controlgit-branch

Retrieving list of names of branches from repository that have been inactive/not committed to in the last 2 months


I'm trying to write a bash script to clean up some stuff and one part of it is to sort through all of the branches I have. I'm trying to get a list of the names of branches in a repo that haven't been commited to in the last 2 months. I'll store this list in a variable and go through each one that I can delete.

So far I have been doing the following:

git clone -q --depth 1 --bare --no-single-branch **REMOTE_URL** tmp/tmp-location
cd tmp/tmp-location
git branch --format='%(color:bold green) %(refname:short) %(color:white) %(committerdate)' --sort=-committerdate

Or I also did the following:

git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - (%(color:green)%(committerdate:relative)%(color:reset))'

This has been great at listing all of the branches and ordering by committer date, but I don't know how to get a list of branches with a committer date older than 2 months.

Or even better Is it possible to check the last commit date of a specific branch name, not just all branches like in the above solutions?


Solution

  • To get just the needed data, fast:

    git clone --filter=tree:0 --depth=1 --bare  --no-single-branch --no-tags \
            u://r/l `mktemp -d`
    cd $_
    

    and now you've got just the minimal needed metadata for just the branch tips. Getting your selection formatted all pretty is

    for rev in `git rev-list --branches --no-walk --before=midnight.2.months.ago`
    do
            git branch --points-at $rev --format='%(refname:short)'
    done
    

    and season that last format to taste of course