Skip to content

Commit

Permalink
fix(@pvm/core): revParse optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
SkReD committed Nov 14, 2022
1 parent bdfad68 commit f1e4430
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 0 additions & 18 deletions benches/clone.js

This file was deleted.

20 changes: 19 additions & 1 deletion packages/pvm-core/lib/git/rev-parse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
import { wdShell } from '../shell'
import path from 'path'
import fs from 'fs'

const revListCache = new Map<string, string>()

export default function revParse(ref: string, cwd: string): string {
// converting HEAD to commit sha without starting separate process which might be slow and up to 50ms
// https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefsymrefasymref
// https://git-scm.com/docs/gitrepository-layout#Documentation/gitrepository-layout.txt-HEAD
const headPath = path.join(cwd, '.git', 'HEAD')
if (ref === 'HEAD' && fs.existsSync(headPath)) {
const headContent = fs.readFileSync(headPath, 'utf-8').trim()
if (headContent) {
// symref
if (headContent.startsWith('ref: ')) {
return fs.readFileSync(path.join(cwd, '.git', headContent.split('ref: ')[1]), 'utf-8')
// plain commit sha
} else {
return headContent
}
}
}

const cacheKey = `${cwd}_${ref}`
// HEAD не кешируем
const cachedResult = revListCache.get(cacheKey)
if (ref.startsWith('HEAD') || !cachedResult) {
const result = wdShell(cwd, `git rev-list -1 ${ref}`)
Expand Down

0 comments on commit f1e4430

Please sign in to comment.