1 | -- resty-gitweb@git/read_blob.lua |
2 | -- Opens and reads blob by filename and rev |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://git.joshstock.in/resty-gitweb> |
6 | -- This software is licensed under the MIT License. |
7 |
|
8 | local ffi = require("ffi") |
9 | local bit = require("bit") |
10 | local git2_error = require("git/git2_error") |
11 |
|
12 | local _M = function(repo_obj, rev, file_path) |
13 | local err = 0 |
14 |
|
15 | -- Get tree root |
16 | local tree = ffi.new("git_object*[1]") |
17 | err = git2.git_revparse_single(ffi.cast("git_object**", tree), repo_obj, rev) |
18 | git2_error(err, "Failed to look up tree from rev name") |
19 | tree = tree[0] |
20 |
|
21 | -- Get tree entry object (blob) |
22 | local blob = ffi.new("git_object*[1]") |
23 | err = git2.git_object_lookup_bypath(ffi.cast("git_object**", blob), tree, file_path, git2.GIT_OBJECT_BLOB) |
24 | git2_error(err, "Failed to look up blob") |
25 | blob = ffi.cast("git_blob*", blob[0]) |
26 |
|
27 | -- Get blob content |
28 | local buf = ffi.new("git_buf") |
29 | err = git2.git_blob_filtered_content(buf, blob, file_path, 0) |
30 | git2_error(err, "Failed to filter blob") |
31 | local raw = ffi.string(buf.ptr, buf.size) |
32 | local is_binary = git2.git_blob_is_binary(blob) == 1 |
33 |
|
34 | -- Free everything |
35 | git2.git_buf_free(buf) |
36 | git2.git_blob_free(blob) |
37 | git2.git_object_free(tree) |
38 |
|
39 | return raw, is_binary |
40 | end |
41 |
|
42 | return _M |
43 |
|