So I have follwoing bash command which is part of a script, that will list just remote releases branches in a repo with no updates in more than 6 months
for b in $(git branch -r --merged | grep release) ; do echo "$(git log -1 --pretty=format:"%Cgreen%as" --before '7 month ago' $b) $b"; done
this will print :
2020-04-27 origin/release/1.1
2020-04-28 origin/release/1.2
2020-04-28 origin/release/1.3
2020-04-29 origin/release/1.4
2020-05-01 origin/release/1.5
The idea is that script will print. "Following merged branches are going to be removed: " and then execute the git push origin --delete {branch} to those branches
Now will like to achieve something similar in python since I´m starting with it...but not quite expert. Was trying with something like this but this doesn´t show the same output than bash:
import subprocess
def release_branches():
result_release = subprocess.getoutput("git branch -r --merged | grep release ")
return [b.strip() for b in result_release.split('\n')]
def branch_stale(branch):
commits_since_seven = subprocess.getoutput(f"git log -1 --pretty=format:"\%Cgreen%as\"{branch} --before '7 month ago'")
if commits_since_seven is not None:
return True
for branch in release_branches():
if branch_stale(branch):
print (branch)
Also is this the best way with subprocess and catching output? or is it better to do it with GitPython instead? thank you!