Is there a Git URI scheme that lets me reference a file in a repo at a specific commit?

Viewed 529

The git Resource Identifier Scheme lets me reference a specific git repository.

However, I'm looking for a URI scheme that lets me reference a specific file/folder, at a specific commit, in a specific git repo. For example, the figurative URI git://github.com/torvalds/linux.git:usr/Makefile@a30d8a39f0571425a459816ed8680e987a2ff279 could be used to reference the file usr/Makefile in the repository git://github.com/torvalds/linux.git at commit a30d8a39f0571425a459816ed8680e987a2ff279. Is there a standard URI scheme that achieves this?

To be clear, I'm interested in a URI scheme that works for all git servers, not e.g. a HTTP URL that works for e.g. GitHub or GitLab only.

2 Answers

I am not aware of any widely accepted way to do this, but here is a scheme that I proposed years ago: http://www.nuke24.net/docs/2014/GitObjectURIScheme.html.

Generally, the form is x-git-object:ef967a26bcd39ca62dd687e929ba4eba0ae66e81, which references the object identified by that Git hash (which in this case is the content of Linux's Makefile, but could otherwise be a commit or tree, which are not directly representable as strings/byte sequences). That document also specifies ways to suggest a repository and to reference the latest on a named branch or a path within a tree or commit, so you could say x-git-object:latest?branch=master&repository=git://github.com/torvalds/linux.git#Makefile to reference the Makefile in the latest commit on the master branch, whatever it may be.

Git itself isn't about files; it's about commits. So the main Git commands are about talking to another repository and getting commits. The URLs that Git understands are meant to achieve this, and they do, and that's where they stop.

There is the git archive command, which—with --remote url—can extract an archive of some or all files from one specific commit, provided the server allows it. This is not a specific file URL/URI, though: this is a Git command that reaches out to a Git server (which, again, normally provides commits) and then asks that Git server if it is willing to prepare a single commit, or subset thereof, as a tar or zip archive. By providing the path of a single file as the subset, you may be able to extract a single file from a particular commit on a particular Git server. But that's as far as Git goes towards achieving your goal—and the arguments you would provide to git archive are again not a URL/URI.

Related