1

Is there a way to check if a branch is empty using the github API?

/repos/:owner/:repo/branches/:branch returns the last commit, but how do I check if the commit belongs to the same branch or the parent branch?

Johnny
  • 133
  • 1
  • 2
  • 13

1 Answers1

2

As illustrated in "Find the parent branch of a git branch", a git branch has no "parent branch".

A branch can be empty compared to another, and ahead compared to another:

--x--x (branch1)
      \
       o--o (branch2, branch3)

Here, branch3 could be considered "empty" when compared to branch2, and 2 commits ahead when compared to branch1.

And there is no way to know if branch3 was created from branch2 or branch1.

With the GitHub API, you would need to compare two commits, which can be two branch names:

GET /repos/:owner/:repo/compare/hubot:branchname...octocat:branchname

That would give you fields of interest like:

"status": "behind",
"ahead_by": 1,
"behind_by": 2,

But that status is only relative to "another branch", without easy way to know if that "other branch" is its "parent" or not.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250