Skip to content

Commit

Permalink
perf(git): cache git exec results
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchfriedman authored and edvald committed Jul 6, 2020
1 parent 3541284 commit 5dce835
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions garden-service/src/vcs/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ interface Submodule {
@Profile()
export class GitHandler extends VcsHandler {
name = "git"
repoRoots = new Map()

private gitCli(log: LogEntry, cwd: string): GitCli {
return async (...args: string[]) => {
log.silly(`Calling git with args '${args.join(" ")}'`)
log.silly(`Calling git in ${cwd} with args '${args.join(" ")}'`)
const { stdout } = await exec("git", args, { cwd, maxBuffer: 10 * 1024 * 1024 })
return stdout.split("\n").filter((line) => line.length > 0)
}
Expand All @@ -81,10 +82,16 @@ export class GitHandler extends VcsHandler {
}

async getRepoRoot(log: LogEntry, path: string) {
if (this.repoRoots.has(path)) {
return this.repoRoots.get(path)
}

const git = this.gitCli(log, path)

try {
return (await git("rev-parse", "--show-toplevel"))[0]
const repoRoot = (await git("rev-parse", "--show-toplevel"))[0]
this.repoRoots.set(path, repoRoot)
return repoRoot
} catch (err) {
if (err.exitCode === 128) {
// Throw nice error when we detect that we're not in a repo root
Expand Down

0 comments on commit 5dce835

Please sign in to comment.