3

We want to get a difference between two GitLab/GitHub branches through REST API. We saw Git supports a command to do that but it seems they don't support for REST API. Is there any API support for this?

git diff --name-status firstbranch..yourBranchName

git diff --name-status origin/develop..origin/master

Showing which files have changed between two revisions

TonyTran
  • 87
  • 2
  • 11

2 Answers2

7

GitHub has dedicated URL (non-REST) for comparing branches.
Example:

https://github.com/octocat/linguist/compare/master...octocat:an-example-comparison-for-docs

Same for GitLab:

https://gitlab.com/gitlab-org/gitlab-foss/compare?from=master&to=master

Although it can be different from a git diff.


The REST API for GitHub would be: "Compare two commits"

GET /repos/:owner/:repo/compare/:base...:head

The response also includes details on the files that were changed between the two commits.
This includes the status of the change (for example, if a file was added, removed, modified, or renamed), and details of the change itself.
For example, files with a renamed status have a previous_filename field showing the previous filename of the file, and files with a modified status have a patch field showing the changes made to the file.

For GitLab: compare branch API

GET /projects/:id/repository/compare?from=master&to=feature
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you for your information. That is what I need. – TonyTran Nov 25 '19 at 19:39
  • Hi VonC, do you know how to compare branch API for GitHub? – TonyTran Nov 25 '19 at 20:14
  • 1
    @TonyTran It is https://developer.github.com/v3/repos/commits/#compare-two-commits: Both `:base` and `:head` must be branch names in `:repo`. In other words, if `:base` and `:head` are *branch* names instead of commits *SHA1*, ... you are comparing branches. – VonC Nov 25 '19 at 22:25
1

As @VonC says

BUT Watch out for a gotcha.

If you are working from a list of all commits in a PR. Your result for each commit in the list will include commit_hash['commit']['url']

It has format https://api.github.com/repos/myorg/myrepo/git/commits/7358c0d4bd18a4b7b6f30a3e3e7b34xxxxxe22e9

If you use this url to call a single commit you won't get the files!

You need to remove /git from the url, or use the commit_hash['url'] which is correct

Jake
  • 4,322
  • 6
  • 39
  • 83