diff options
author | HampusM <hampus@hampusmat.com> | 2021-05-30 12:19:49 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-05-30 12:19:49 +0200 |
commit | b5064a73c9cf7e737fe620eeb38040d683dbf87d (patch) | |
tree | 10ba6608188ba7dba8b6ecac00b9e279057c305a | |
parent | 6edf567693ab885bcbab0a894001e1bcbca939c1 (diff) |
Added a tree api endpoint
-rw-r--r-- | src/api/git.js | 47 | ||||
-rw-r--r-- | src/api/v1.js | 13 |
2 files changed, 58 insertions, 2 deletions
diff --git a/src/api/git.js b/src/api/git.js index 97d38ac..f0c2783 100644 --- a/src/api/git.js +++ b/src/api/git.js @@ -315,9 +315,54 @@ function connectToGitHTTPBackend(base_dir, req, reply) git_pack.on("close", () => reply.raw.end()); } +function getTreeEntries(entries) +{ + return entries.reduce((acc, entry) => + { + return acc.then((obj) => + { + const basename = path.parse(entry.path()).base; + + if(entry.isBlob()) { + console.log("blob " + entry.path()); + obj[basename] = { oid: entry.oid(), type: "blob" }; + return obj; + } + + if(entry.isTree()) { + console.log("tree " + entry.path()); + return entry.getTree().then((tree) => + { + console.log("LMAO " + tree.path()); + return getTreeEntries(tree.entries()).then((tree_entries) => + { + obj[basename] = { oid: entry.oid(), type: "tree", tree: tree_entries }; + return obj; + }); + }); + } + }); + + }, Promise.resolve({})); +} + +async function getTree(base_dir, repo_name) +{ + repo_name = addRepoDirSuffix(repo_name); + + const repo = await git.Repository.openBare(`${base_dir}/${repo_name}`); + const master_commit = await repo.getMasterCommit(); + + const tree = await master_commit.getTree(); + const entries = tree.entries(); + + return await getTreeEntries(entries); +} + module.exports.getLog = getLog; module.exports.getRepos = getRepos; module.exports.getRepoFile = getRepoFile; module.exports.getCommit = getCommit; module.exports.doesCommitExist = doesCommitExist; -module.exports.connectToGitHTTPBackend = connectToGitHTTPBackend;
\ No newline at end of file +module.exports.connectToGitHTTPBackend = connectToGitHTTPBackend; +module.exports.getTree = getTree;
\ No newline at end of file diff --git a/src/api/v1.js b/src/api/v1.js index 3a6f7ea..743be09 100644 --- a/src/api/v1.js +++ b/src/api/v1.js @@ -108,8 +108,19 @@ module.exports = function (fastify, opts, done) } }); + fastify_repo.route({ + method: "GET", + path: "/tree", + handler: async (req, reply) => + { + const tree = await git.getTree(opts.config.settings.base_dir, req.params.repo); + + reply.send({ data: tree }); + } + }); + done_repo(); }, { prefix: "/repos/:repo" }); done(); -}
\ No newline at end of file +};
\ No newline at end of file |