2

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.

Sadly, it appears that GitHub branches, unlike local branches, are isolated one from each other.

Let's say I have a repository with three branches (master and other two created from master):

enter image description here

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:

enter image description here

Is there any way to read all the GitHub branches with the Git API, or I'm forced to use the GitHub API?

I hoped to be able to read at least the branches generated from the branch I'm on (master, in this case). I'm starting to guess that GitHub keeps that information for itself, without disclosing it in any canonical Git way...

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • will git branch -r not work for this? https://stackoverflow.com/questions/3471827/how-do-i-list-all-remote-branches-in-git-1-7 – sntrenter Oct 30 '20 at 15:30
  • @sntrenter no, that only prints `origin/master`. While from the outside (your local computer) you can see all the remote branches, from the inside (of one of those remote branches) is apparently impossible to access the other sibling/descendant branches. I suppose I've discovered today that, from the GitHub perspective, those are all unrelated, isolated boxes, connected at a higher, non-Git level (read: at a GitHub level). – Andrea Ligios Oct 30 '20 at 15:59
  • 2
    How are you checking the repository out? The default is a shallow clone when you use `actions/checkout`. – Benjamin W. Oct 30 '20 at 21:55
  • Thank you too, Benjamin – Andrea Ligios Oct 31 '20 at 16:32

1 Answers1

8

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.). This is why git branch -r command output contains all the remote branches.

In GitHub Actions (or mentioned Travis CI) the clone operation behaves slightly different. 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. 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:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags.

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:

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. The documentation is also clear about this (see §Git Clone Depth). You have to adjust your travis.yml to disable --depth flag:

git:
  depth: false
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Marcin Kłopotek
  • 5,271
  • 4
  • 31
  • 51