Generating a link to the compare diff result of a single file

I understand that there is a compare API where you can provide a commits or branches for comparison.

gitLabApi.repositoryApi.compare

This returns the diffs fine but what I’d like to generate next is a link to a GitLab compare page to a given file. Say I have a commit change that’s modified 3 files, I want to provide a URL link to the diff of the last file say.

I see on the Compare web page that there is a dropdown like ‘Showing 2 changed files’ which provides links to the differing files. I’d like to reference these links too but I can’t see where the last hash in the URL’s are coming from to derive it in my code.

e.g.

https://…-/compare/master…caa803cae0b8dc2f7e8307d1248b7c72fe6463db#829fe3af42f5d5ec5d31291014745ed756b4a7ba

I thought that the sha after the # would be just be the blob Id but it doesn’t seem to be. Any suggestions?

I was researching the same problem. It took a totally unreasonable amount of time and finally I found the solution in a comment in one of those 40,932 open gitlab issues. It’s the sha1sum of the file path.
echo -n src/myfile.c | sha1sum

1 Like

This works unless there are a lot of files and there is pagination and your file is not on the first page of pagination. In that case, the above sadly does not work.