我想使用 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)
这是一个例子:
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}")