Home > database >  Get git remote url for webpage tree view at current HEAD hash?
Get git remote url for webpage tree view at current HEAD hash?

Time:10-24

As an example, let's say I check out this small project on GitLab:

git clone https://gitlab.com/gitlab-examples/functions.git
cd functions
git rev-parse --short HEAD
# c1fccc0
git branch -l
# * master

So, by default after cloning we're in master branch, at commit c1fccc0.

Now, I'd like to get the remote URL showing me the git project directory (that is, tree view) at this commit. It turns out, that URL on GitLab is https://gitlab.com/gitlab-examples/functions/-/tree/c1fccc0/ - so, I can in principle generate this URL in bash like so:

echo $(echo $(git config remote.origin.url) | sed 's/\.git//')/-/tree/$(git rev-parse --short HEAD)/
# https://gitlab.com/gitlab-examples/functions/-/tree/c1fccc0/

So far so good - but then, if I look at a GitHub project, the logic is slightly different; here is another example small project:

git clone https://github.com/codepr/llb.git
git branch -l && git rev-parse --short HEAD
# * master
# b4a6a66

Here, the tree view of the files is however in https://github.com/codepr/llb/tree/b4a6a66/ (with or without the last slash) - so the code to obtain this URL is now different:

echo $(echo $(git config remote.origin.url) | sed 's/\.git//')/tree/$(git rev-parse --short HEAD)/
# https://github.com/codepr/llb/tree/b4a6a66/

So, is there a git internal command, which in one way or another would "know" the way the remote URLs are constructed, and could generate you a URL/web link for the current commit at the remote tree view - provided the remote is HTTP(S)?

CodePudding user response:

So, is there a git internal command, which in one way or another would "know" the way the remote URLs are constructed…

No, not at all. git knows Git object database and Git protocol. Web interfaces to the database are properties of Git hostings and they deliberately do things differently from one another to catch users in vendor lock-in.

  • Related