git bash commands with python or Gitpyhon

0 votes

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!

Jun 21, 2022 in Linux Administration by Korak
• 5,820 points
1,732 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Linux Administration

0 votes
0 answers

Run text file as commands in Bash

If I have a text file with ...READ MORE

Apr 13, 2022 in Linux Administration by Aditya
• 7,680 points
691 views
0 votes
1 answer

bash: /home/XXX/.rvm/scripts/rvm: No such file or directory

Install RVM  gpg --keyserver hkp:// keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 ​Now ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
3,570 views
0 votes
1 answer

Adding Git-Bash to the new Windows Terminal

Open settings with Ctrl + , You'll want ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
1,029 views
0 votes
1 answer

Windows system, after heroku login from git bash, there is no command line show

You can login via Windows command prompt ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
1,574 views
0 votes
1 answer

Persistant "bash: /home/XXX/.rvm/scripts/rvm: No such file or directory" in terminal

Introduce RVM: gpg - - keyserver hkp://keys.gnupg.net - ...READ MORE

answered May 27, 2022 in Linux Administration by Korak
• 5,820 points
617 views
0 votes
0 answers
0 votes
0 answers

Display an ASCII table of characters 32 to 112 with bash

I want to know how to display ...READ MORE

Jun 3, 2022 in Linux Administration by Rahul
• 3,380 points
455 views
0 votes
0 answers

How to add man and zip to "git bash" installation on Windows

I am using git bash on Windows - that ...READ MORE

Jun 7, 2022 in Linux Administration by Rahul
• 3,380 points
1,040 views
0 votes
0 answers

How to add man and zip to "git bash" installation on Windows

I am using git bash on Windows - that ...READ MORE

Jun 9, 2022 in Linux Administration by Rahul
• 3,380 points
1,716 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP