问题
When querying a symlink via the GitHub API, I get different results if the symlink points to a file as opposed to a directory. The latter is more well behaved in that it returns "type": "symlink"
as part of its JSON, whereas the former returns "type": "file"
. Example file symlink, example directory symlink.
It's very confusing when a symlink advertises itself as a file, as GET-ing its download URL will just get you the target of the symlink and not the file contents.
How do I tell if a file is actually a symlink, as opposed to a real file?
Also, is the behaviour of returning type "file" for file symlinks a downright bug? It just doesn't seem right.
回答1:
A symlink in Git doesn't care whether the target is a file or a directory. (Or even if the target exists.)
The API is not returning "file" for symlink The file in question is not a symbolic link. It's a regular file. After cloning your repository:
% ls -Flas muzhack/files/littleBitsMidiNotes.ino
4 -rw-r--r-- 1 user group 3247 Jun 7 12:00 muzhack/files/littleBitsMidiNotes.ino
% git ls-files --stage muzhack/files/littleBitsMidiNotes.ino
100644 08918243048ae4a4f57e69a34776e9a0bd1ec7af 0 muzhack/files/littleBitsMidiNotes.ino
A mode of 100644
(that first field returned by ls-files
) indicates that this is a regular file. In contrast, the entry that GitHub is reporting as a symlink is, in fact, a symbolic link:
% ls -Flas muzhack/files/adapter-board-files
4 lrwxr-xr-x 1 user group 25 Jun 7 12:00 muzhack/files/adapter-board-files@ -> ../../adapter-board-files
% git ls-files --stage muzhack/files/adapter-board-files
120000 ef17a5e7b4bef4e51f19dc6b4c360c95cbb223c8 0 muzhack/files/adapter-board-files
So the GitHub API appears to be reporting this information correctly.
回答2:
The answer is unfortunately "you don't". There is no way (with the API in its current state) to differentiate between a file request and a symlink. From the documentation:
If the requested :path points to a symlink, and the symlink's target is a normal file in the repository, then the API responds with the content of the file [...]
Otherwise, the API responds with an object describing the symlink itself:
I raised this with GitHub support and they confirmed there's no way to do this. They offered to raise it as a request with the internal teams, but I would imagine it's unlikely to get picked up.
One work around (that isn't suitable in all scenarios) is to request the file via https://raw.githubusercontent.com/ which will return either the file contents if it's a real file, or just the file path if it's a symlink.
来源:https://stackoverflow.com/questions/37685250/github-api-how-do-i-find-out-if-a-file-is-actually-a-symlink