From b5064a73c9cf7e737fe620eeb38040d683dbf87d Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 30 May 2021 12:19:49 +0200 Subject: Added a tree api endpoint --- src/api/git.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/api/v1.js | 13 ++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) (limited to 'src') 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 -- cgit v1.2.3-18-g5258