Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get blame information of a line of code on a repository from github API

Tags:

git

github-api

I searched through all the existing github API s found in here to get blame information of a certain line of code in a source file, but I couldn't find a way to achieve it, such API is not listed in the above site. Can anyone point me a way to get the blame information of a line of code of a source file, which is hosted in github repos without cloning it and running git blame locally.

Thanks in advance

like image 612
Kasun Siyambalapitiya Avatar asked Jan 27 '17 08:01

Kasun Siyambalapitiya


People also ask

How do I git blame on GitHub?

On GitHub.com, navigate to the main page of the repository. Click to open the file whose line history you want to view. In the upper-right corner of the file view, click Blame to open the blame view.

Which command shows the author of each line of text?

Summary. The git blame command is used to examine the contents of a file line by line and see when each line was last modified and who the author of the modifications was.

What is the fastest way to find out who committed a particular segment of code when using git?

You can use git blame <filename> . You will be shown the name of the author, timetag and commit of each code fragment of the file.


2 Answers

The GitHub API v4 has a working blame API. Here's an example of the proper query:

{
  # repository name/owner
  repository(name: "MidiPlayerJS", owner: "TimMensch") {
    # branch name
    ref(qualifiedName:"tim") {      
      target {
        # cast Target to a Commit
        ... on Commit {
          # full repo-relative path to blame file
          blame(path:"package.json") {
            ranges {
              commit {
                author {
                  name
                }
              }
              startingLine
              endingLine
              age
            }
          }
        }
      }
    }
  }
}

This works in the explorer for me.

like image 115
SomeCallMeTim Avatar answered Nov 14 '22 17:11

SomeCallMeTim


There's no Blame API in the GitHub REST API http://developer.github.com/v3/

But you can fetch blame information through the new GraphQL API which is in early access mode. see this doc https://developer.github.com/early-access/graphql/

like image 2
Kalanka Avatar answered Nov 14 '22 16:11

Kalanka