Sai Astro
Sai Astro

Reputation: 123

Python GitLab How to get all file names in a project?

I want to get all files ( python files esp ) in a git project. While I am able to access one file at a time, based on file name input. I am unsure how to get all file names under a directory.

My present code below

gl=gitlab.Gitlab('https://git.sys.xxx.com', private_token=pwd, api_version=4)
gl.auth()
project=gl.projects.get('hadoop-d/xyz')
a='src/main/python/xyz_inbound/baseline/*.*'
b='file_name'
c='.py'
d=a+b+c
f=project.files.get(file_path=d, ref='master')

Upvotes: 3

Views: 9793

Answers (2)

evan54
evan54

Reputation: 3733

You can use this command from here

# list the content of the root directory for the default branch
items = project.repository_tree()

# list the content of a subdirectory on a specific branch
items = project.repository_tree(path='docs', ref='branch1')

I think for your usecase you'll need something like the following:

items = project.repository_tree(path='src/main/python/xyz_inbound/baseline',
                                ref='master', recursive=True, all=True)
filenames = [f.path for f in items if f.path[-3:] == '.py']

Upvotes: 6

Arty-chan
Arty-chan

Reputation: 2998

If all you need is a list of the files, then you can use the repositories API to get a list of files and filter for the files in a particular repository.

As explained in the docs, the repository tree endpoint is essentially the same as running git ls-tree on the repo.

Upvotes: 1

Related Questions