简体   繁体   English

如何使用 CI 的 Git API 读取 GitHub 存储库中的所有分支?

[英]How can I read all the branches in a GitHub repo using the Git API from CI?

Using the Git API in a CI system (eg. GitHub Actions or Travis-CI), I want to gather information on all the branches of the repo.在 CI 系统(例如 GitHub Actions 或 Travis-CI)中使用Git API ,我想收集有关 repo 的所有分支的信息。

Sadly, it appears that GitHub branches, unlike local branches, are isolated one from each other.遗憾的是,GitHub 分支似乎与本地分支不同,它们彼此隔离。

Let's say I have a repository with three branches ( master and other two created from master ):假设我有一个包含三个分支的存储库( master和其他两个从master创建的):

在此处输入图片说明

If I run the following script:如果我运行以下脚本:

#!/usr/bin/env bash

printf "\n$ git for-each-ref --format='%(refname)' \n"
printf "$(git for-each-ref)\n"

printf "__________________________________________\n"

printf "\n$ git branch -a\n"
printf "$(git branch -a)\n"

I can only see master , not the other two branches:我只能看到master ,而不是其他两个分支:

在此处输入图片说明

Is there any way to read all the GitHub branches with the Git API, or I'm forced to use the GitHub API ?有没有办法用 Git API 读取所有 GitHub 分支,或者我被迫使用 GitHub API

I hoped to be able to read at least the branches generated from the branch I'm on ( master , in this case).我希望至少能够读取从我所在的分支(在本例中为master )生成的分支。 I'm starting to guess that GitHub keeps that information for itself, without disclosing it in any canonical Git way...我开始猜测 GitHub 会为自己保留这些信息,而不会以任何规范的 Git 方式公开它......

You have to perform a full repository checkout in your workflow job in order to get the full repository data (including all branches).您必须在工作流作业中执行完整的存储库检出才能获得完整的存储库数据(包括所有分支)。 When you do git clone... (without any switches like --depth or --single-branch ) on your local machine you are getting all the repository data (tags, branches, commit history, etc.).当您在本地机器上执行git clone... (没有任何开关,如--depth--single-branch )时,您将获得所有存储库数据(标签、分支、提交历史等)。 This is why git branch -r command output contains all the remote branches.这就是为什么git branch -r命令输出包含所有远程分支的原因。

In GitHub Actions (or mentioned Travis CI) the clone operation behaves slightly different.在 GitHub Actions(或提到的 Travis CI)中,克隆操作的行为略有不同。 By default, to conserve bandwidth and disk storage space the CI system retrieves only the minimal required part of the repository in order to execute the workflow.默认情况下,为了节省带宽和磁盘存储空间,CI 系统仅检索存储库所需的最少部分以执行工作流。 This usually is only the current branch associated with the workflow and limited commit history.这通常只是与工作流和有限提交历史相关联的当前分支。 This is why your commands do not return all the expected branches.这就是您的命令不返回所有预期分支的原因。 However, you can override the default checkout behaviour.但是,您可以覆盖默认的结帐行为。 If you look at the actions/checkout docs :如果您查看actions/checkout文档

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow.对于触发工作流的 ref/SHA,默认情况下仅获取单个提交。 Set fetch-depth: 0 to fetch all history for all branches and tags .设置fetch-depth: 0以获取所有分支和标签的所有历史记录

Taking the above into consideration, to fetch all the branches, you have to adjust the actions/checkout@v2 step in your workflow according to the docs :考虑到上述情况,要获取所有分支,您必须根据文档调整工作流程中的actions/checkout@v2步骤:

Fetch all history for all tags and branches获取所有标签和分支的所有历史记录

- uses: actions/checkout@v2 with: fetch-depth: 0

Now, when you call in the next step sth.现在,当您调用下一步时。 like:喜欢:

- name: List all the remote branches
  run: git branch -r

you should get the expected output.你应该得到预期的输出。

Speaking about Travis CI.说到 Travis CI。 The documentation is also clear about this (see §Git Clone Depth ).文档对此也很清楚(参见§Git Clone Depth )。 You have to adjust your travis.yml to disable --depth flag:您必须调整您的travis.yml以禁用--depth标志:

git:
  depth: false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何不使用Git将GitHub存储库下载为Zip - How to download GitHub repo as Zip without using Git 我如何知道包含特定提交哈希的所有 git 分支的列表 - How can I know the list of all git branches containing specific commit hash 如果没有通过单元测试,如何拒绝来自本地仓库的原始仓库的git推送? - How can I reject git pushes on an origin repo from a local repo if it does not pass unit tests? 如何在shellscript中将所有git repo从目录推送到git - How to push all git repo's from a directory to git in a shellscript 如何使用 API 检查我的 GitHub 存储库中是否存在标签问题? - How do I check if an issue with a tag exists on my GitHub repo using the API? 如何使用 REST API 将文件提交到 github 以进行 CI 集成 - How to commit file to github using REST API for CI integration 如何使用 bash 脚本遍历所有 git 分支 - How to iterate through all git branches using bash script 使用 bash、cURL 和 GitHub API 从命令行分叉 GitHub 存储库 - Forking a GitHub repo using from the command line with bash, cURL, and the GitHub API 为什么 GitHub API 不返回存储库的所有分支? - Why doesn't the GitHub API return all branches for a repository? 是否可以在不使用 HTTPS API 或 `gh` CLI 工具的情况下获取 git 存储库中所有打开的 PR 的列表? - Is it possible to get a list of all open PRs in a git repo without using the HTTPS API or the `gh` CLI tool?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM