Reputation: 387
I am doing some large scale analysis of Java projects on Github; this involves cloning the project via git (nothing special here), then doing a static read of the code for analysis, and so on.
My question: is there a way to programmatically recover the Github url for each source file in the cloned repository? I'm trying to get this so I can then link back into Github and see the original source.
For example, the following url (what I want): https://github.com/elastic/elasticsearch/blob/master/libs/elasticsearch-nio/src/main/java/org/elasticsearch/nio/BytesWriteOperation.java#L35 points to a particular function. Of course, the directory structure in the cloned version doesn't match the url name, e.g., the /blob/master/ seems particular to this project and structure of the repository.
Upvotes: 0
Views: 773
Reputation: 9330
So if I'm understanding correctly you want to create a link to a human readable code view with a specific line highlighted.
Essentially the normal GitHub link breaks down into:
https://github.com/elastic/elasticsearch
(aka the remote origin)/blob/
Each of these components are pretty simple to get.
1. Remote Origin
(aka <remote-origin>)
When you clone a repo, the local copy you create stores a reference to the remote origin URL.
$ git config --get remote.origin.url
https://github.com/elastic/elasticsearch
or, for more detail:
$ git remote show origin
* remote origin
Fetch URL: https://github.com/elastic/elasticsearch
Push URL: https://github.com/elastic/elasticsearch
HEAD branch: develop
Remote branches:
1.4 new (next fetch will store in remotes/origin)
2.0 tracked
add-code-of-conduct-1 tracked
build_test tracked
...
and so on
2. Branch
(aka <branch>)
You're no doubt aware of how to get the repo's branches:
git branch
develop
* master
but you can also use git rev-parse --abbrev-ref HEAD
to return just the current branch.
git rev-parse --abbrev-ref HEAD
master
From those pieces of information and the file path within the local copy of the repo you can build the link
<remote-origin>/blob/<branch>/<path-to-file-in-repo>#L<line_number>
Upvotes: 1
Reputation: 1372
As suggested you can use githubusercontent: Syntax should be:
https://raw.githubusercontent.com/<user>/<repo>/<branch>/<file>
Upvotes: 0