How to get a list of a repository branches using Python

MagicaNexus

I am trying to get a list of all the branches available on my repository using Python with this code :

import subprocess

branches = ["All"]
command = "git branch -r"
branch_list = subprocess.check_output(command)

for branch in branch_list:
   print branch
   branches.append[branch]

What I want is to have something like :

print branches[0] # is "All"
print branches[1] # is "branch1"
print branches[2] # is "branch2"
etc etc

but instead of that I have

print branches[0] # is "All"
print branches[1] # is "b"
print branches[2] # is "r"
print branches[3] # is "a"
print branches[4] # is "n"
print branches[5] # is "c"
print branches[6] # is "h"
etc etc

Thank you for your time and your help

wbadart

Taking a peek at the check_output documentation, it looks like we're getting a blob of bytes back. To make it easier to work with, we can decode it. Then, since git branch -r outputs one branch per line, split the string on newlines:

branches = subprocess.check_output(command).decode().split('\n')

BUT I think there's an even easier way to do it. Every single object in git corresponds to some file under the .git directory. In this case, you can find your list of branches in .git/refs/heads:

import os
branches = os.listdir('.git/refs/heads')

EDIT (2020/10/13): I've spent some more time with subprocess since writing this response and wanted to point out the text option (via subprocess.run):

If encoding or errors are specified, or text is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the io.TextIOWrapper default.

This means you could write the check_output expression as:

branches = subprocess.check_output(command, text=True).split('\n')

leaving encoding and decoding to the system. Whichever you prefer!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

BitBucket API: How to get a list of branches from a repository UUID

How to list the names of all the branches from a repository using GitHttpClient (API)

Not able to get the list of azure repository using python

how to minimal clone repository to be able list branches and checkout single file

How to list branches that contain a given commit on a specific repository?

How to get list files from a github repository folder using R

How to get all branches from git remote repository for a specific user

How to get list of branches on a atlassian-stash project using REST call in JAVA

How to get specific file version from git repository using python

How to maintain multiple branches of a repository?

How to calculate sum of tree branches using Python?

How to get list of all branches created off a GIT branch (get all sub branches of a branch)

List of branches of a Python tree

How to get list using sql query in python?

Git: How to get a list of pushed branches to remote master?

How to get the list of branches that made the difference between 2 git revisions

How to get a list of different commits between two git branches?

How to get a list of all branches containing specific revisoin in Mercurial?

How to get a list with all branches and the latest tag that includes a number in Git?

Subversion: How to get a list of all active, unmerged branches

How to get list of latest branches that auto exit (no need to press q)?

How to select using List<String> as input at Repository?

How to update list of remote branches using IntelliJ IDEA?

Using the Nexus3 API how do I get a list of artifacts in a repository

How to list `git` branches not merged to given branches?

How to split branches on a mercurial repository to make other repositories using HG commands?

How do I get code coverage for all branches using Pester?

How to get all branches with someone’s commits using git?

How can I get all the branches of a project using Gitlab API?