import { ICreateTreeParams , ITree , ITreeEntry } from "@fluidframework/gitresources" ; import { Router } from "express" ; import * as git from "isomorphic-git" ; import nconf from "nconf" ; import * as utils from "../utils" ; export async function createTree ( store , tenantId , authorization , params ) { const entries = params . tree . map ( ( tree ) => { const entry = { mode : tree . mode , oid : tree . sha , path : tree . path , type : tree . type , } ; return entry ; } ) ; const treeDescription = { entries } ; const sha = await git . writeObject ( { dir : utils . getGitDir ( store , tenantId ) , type : "tree" , object : treeDescription , } ) ; return getTree ( store , tenantId , authorization , sha , false , true ) ; } export async function getTree ( store , tenantId , authorization , sha , recursive , useCache ) { let returnEntries ; if ( recursive ) { returnEntries = await git . walkBeta2 ( { dir : utils . getGitDir ( store , tenantId ) , map : ( async ( path , [ head ] ) => { if ( path === "." ) { return ; } return { path , mode : ( await head . mode ( ) ) . toString ( 8 ) , sha : await head . oid ( ) , size : 0 , type : await head . type ( ) , url : "" , } ; } ) as any , trees : [ git . TREE ( { ref : sha } as any ) ] , } ) ; } else { const treeObject = await git . readObject ( { dir : utils . getGitDir ( store , tenantId ) , oid : sha } ) ; const description = treeObject . object as git . TreeDescription ; returnEntries = description . entries . map ( ( tree ) => { const returnEntry = { path : tree . path , mode : tree . mode , sha : tree . oid , size : 0 , type : tree . type , url : "" , } ; return returnEntry ; } ) ; } return { sha , tree : returnEntries , url : "" , } ; } export function create ( store ) { const router = Router ( ) ; router . post ( "/repos/:ignored?/:tenantId/git/trees" , ( request , response ) => { const treeP = createTree ( store , request . params . tenantId , request . get ( "Authorization" ) , request . body ) ; utils . handleResponse ( treeP , response , false , 201 ) ; } ) ; router . get ( "/repos/:ignored?/:tenantId/git/trees/:sha" , ( request , response ) => { const useCache = ! ( "disableCache" in request . query ) ; const treeP = getTree ( store , request . params . tenantId , request . get ( "Authorization" ) , request . params . sha , request . query . recursive === "1" , useCache ) ; utils . handleResponse ( treeP , response , useCache ) ; } ) ; return router ; }