0
from github import Github

access_token = "MYACCESS_TOKEN"
g=Github(access_token, retry=20)

repo = g.get_repo("pygit/git")
print(repo.name)

branches = repo.get_branches()
for branch in branches:
    print(branch.name)


for file in repo.get_contents(""):
    print(file.name)

Here I can list the branches. [I have 5 branches] I can list all the branches and I am able to view the content present in the root level of the repository.

But I am not able to select particular branch. where my data is present inside a subfolder of particular branch.

Expecting:

  1. List all branches in a repository [Done]
  2. Move inside a particular branch.
  3. List all the contents inside a branch including directories.
  4. Move inside a directory.
  5. List all the content present in that directory.
po.pe
  • 1,047
  • 1
  • 12
  • 27

1 Answers1

0

You can use this example in the PyGithub docs to list contents in a repository. The get_contents method accepts an optional ref parameter to which you can pass a branch's name.

For example:

for branch in branches:
  contents = repo.get_contents("", ref=branch.name)
  while contents:
    file_content = contents.pop(0)
    if file_content.type == "dir":
      contents.extend(repo.get_contents(file_content.path, ref=branch.name))
    else:
      print(file_content)
D Malan
  • 10,272
  • 3
  • 25
  • 50