| 1 | -- resty-gitweb@git/list_refs.lua |
| 2 | -- Lists named references/revisions (branches, tags) |
| 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 git2_error = require("git/git2_error") |
| 10 |
|
| 11 | _M = function(repo_obj) |
| 12 | -- List refs |
| 13 | local refs = ffi.new("git_strarray") |
| 14 | local err = git2.git_reference_list(ffi.cast("git_strarray*", refs), repo_obj) |
| 15 |
|
| 16 | local ret = {} |
| 17 | ret.heads = {} |
| 18 | ret.tags = {} |
| 19 |
|
| 20 | for i = 0, tonumber(refs.count)-1 do |
| 21 |
|
| 22 | local name = ffi.string(refs.strings[i]) |
| 23 |
|
| 24 | local dest |
| 25 | local prefix_len |
| 26 | if name:match("^refs/heads/") then |
| 27 | dest = ret.heads |
| 28 | prefix_len = 12 |
| 29 | elseif name:match("^refs/tags/") then |
| 30 | dest = ret.tags |
| 31 | prefix_len = 11 |
| 32 | end |
| 33 |
|
| 34 | if dest then |
| 35 | local oid = ffi.new("git_oid") |
| 36 | local err = git2.git_reference_name_to_id(ffi.cast("git_oid*", oid), repo_obj, refs.strings[i]) |
| 37 |
|
| 38 | -- Format oid as SHA1 hash |
| 39 | local hash = ffi.new("char[41]") -- SHA1 length (40 chars) + \0 |
| 40 | local err = git2.git_oid_fmt(hash, ffi.cast("git_oid*", oid)) |
| 41 | hash = ffi.string(hash) |
| 42 |
|
| 43 | local ref = {} |
| 44 | ref.name = string.sub(name, prefix_len, string.len(name)) |
| 45 | ref.full = name |
| 46 | ref.hash = hash |
| 47 | ref.shorthash = string.sub(hash, 1, 7) |
| 48 | table.insert(dest, ref) |
| 49 | end |
| 50 |
|
| 51 | end |
| 52 |
|
| 53 | if refs then |
| 54 | git2.git_strarray_free(ffi.cast("git_strarray*", refs)) |
| 55 | end |
| 56 |
|
| 57 | return ret |
| 58 | end |
| 59 |
|
| 60 | return _M |
| 61 |
|