diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-02 18:45:51 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-02 18:45:51 +0200 |
commit | d46d62553cb2f7a300810a2bd9d31c0878b77e8b (patch) | |
tree | 2392f73316bf6fa68729b83b157e30fe7a75bcaf /src/api/git.js | |
parent | b5064a73c9cf7e737fe620eeb38040d683dbf87d (diff) |
Tree data includes last commit, cleaned up the GetTree function
Diffstat (limited to 'src/api/git.js')
-rw-r--r-- | src/api/git.js | 103 |
1 files changed, 75 insertions, 28 deletions
diff --git a/src/api/git.js b/src/api/git.js index f0c2783..35b83cf 100644 --- a/src/api/git.js +++ b/src/api/git.js @@ -315,50 +315,97 @@ function connectToGitHTTPBackend(base_dir, req, reply) git_pack.on("close", () => reply.raw.end()); } -function getTreeEntries(entries) +async function getTree(base_dir, repo_name, tree_path) { - return entries.reduce((acc, entry) => + 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(); + + let entries; + if(tree_path) { + try { + const path_entry = await tree.getEntry(tree_path); + + if(path_entry.isBlob()) { + return { type: "blob", content: (await path_entry.getBlob()).content().toString() }; + } + + entries = await (await path_entry.getTree()).entries(); + } + catch(err) { + if(err.errno === -3) { + return { error: 404 }; + } + return { error: 500 }; + } + } + else { + entries = tree.entries(); + } + + return { type: "tree", tree: await 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 getTreeEntryLastCommit(repo, entry).then((last_commit) => + { + obj[path.parse(entry.path()).base] = { + oid: entry.oid(), + type: entry.isBlob() ? "blob" : "tree", + last_commit: { + id: last_commit.id, + message: last_commit.message, + time: last_commit.time + } + }; return obj; - } + }); + }); + }, Promise.resolve({})) }; +} + +async function getTreeEntryLastCommit(repo, tree_entry) +{ + const walker = git.Revwalk.create(repo); + walker.pushHead(); + + const raw_commits = await walker.getCommitsUntil(() => true); - if(entry.isTree()) { - console.log("tree " + entry.path()); - return entry.getTree().then((tree) => + return raw_commits.reduce((acc, commit) => + { + return acc.then((obj) => + { + if(Object.keys(obj).length == 0) { + return commit.getDiff().then((diffs) => { - console.log("LMAO " + tree.path()); - return getTreeEntries(tree.entries()).then((tree_entries) => + return diffs[0].patches().then((patches) => { - obj[basename] = { oid: entry.oid(), type: "tree", tree: tree_entries }; + let matching_path_patch; + if(tree_entry.isBlob()) { + matching_path_patch = patches.find((patch) => patch.newFile().path() === tree_entry.path()); + } + else { + matching_path_patch = patches.find((patch) => path.parse(patch.newFile().path()).dir.startsWith(tree_entry.path())); + } + + if(matching_path_patch) { + obj.id = commit.sha(); + obj.message = commit.message().replace(/\n/g, ""); + obj.time = commit.date(); + } return obj; }); }); } + + 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; |