4
votes

I am trying to find out what the commit hash is for files within my repository. Different files got modified with different commits.

I see this information displayed on GitLab web interface: File's commit hash vs master's commit hash

But how do I get this information using the GitLab API? I tried doing:

GET /projects/:id/repository/files

https://gitlab_server/api/v3/projects/1234/repository/files?file_path=test/file2.tconf&ref=master

But the commit_id from the response matches HEAD's hash 0f05a8e4bf45942b1189fce923184129821845f1, instead of what's seen in the screenshot above.

{
  "file_name": "file2.tconf",
  "file_path": "test/file2.tconf",
  "size"     : 44,
  "encoding" : "base64",
  "content"  : "...",
  "ref"      : "master",
  "blob_id"  : "50450759d2b0694b044ed3d7eaf93c2c98dacbba",
  "commit_id": "0f05a8e4bf45942b1189fce923184129821845f1"
}

Any clues?

2

2 Answers

1
votes

last-commit-id now exists and gives just what you asked for: the last commit that modified that specific file.

see GitLab's get-file-from-repository API

0
votes

get-a-single-commit I've just tested it myself for my project, used it to get information about the last commit, I guess that you can now easily get what you want.

see GitLab's get-a-single-commit

GET /projects/:id/repository/commits/:sha

curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/commits/master"