使用Python获取git repo中的目录和文件列表

问题描述 投票:0回答:1

我想使用 Python 获取 git 存储库中存在的所有文件和目录,然后解析每个目录以获取其下存在的文件的详细信息。

下面是我一直在尝试但没有返回任何内容的代码。我可以获得分行详细信息。

import git
import os
from git import Repo

Repo.clone_from(repo_url,local_path)
repo = git.Repo(local_path)
remote = repo.remote("origin")

for branches in remote.refs:
 //code to get a specific branch, say abc, using if condition

repo.git.checkout(abc)
os.listdir(local_path)
python git gitpython
1个回答
0
投票

这是一个例子:

import git
import os

# Clone the repository
repo_url = "https://github.com/your_username/your_repository.git"
local_path = "path_to_local_clone"
git.Repo.clone_from(repo_url, local_path)

# Switch to a specific branch
repo = git.Repo(local_path)
repo.git.checkout("master")

# List files and directories in the repository
contents = os.listdir(local_path)

for item in contents:
    item_path = os.path.join(local_path, item)

    if os.path.isfile(item_path):
        print(f"File: {item}")
    elif os.path.isdir(item_path):
        print(f"Directory: {item}")
© www.soinside.com 2019 - 2024. All rights reserved.