diff --git a/lib/revs.js b/lib/revs.js index ca14837..ebcc848 100644 --- a/lib/revs.js +++ b/lib/revs.js @@ -1,14 +1,12 @@ -const pinflight = require('promise-inflight') const spawn = require('./spawn.js') const { LRUCache } = require('lru-cache') +const linesToRevs = require('./lines-to-revs.js') const revsCache = new LRUCache({ max: 100, ttl: 5 * 60 * 1000, }) -const linesToRevs = require('./lines-to-revs.js') - module.exports = async (repo, opts = {}) => { if (!opts.noGitRevCache) { const cached = revsCache.get(repo) @@ -17,12 +15,8 @@ module.exports = async (repo, opts = {}) => { } } - return pinflight(`ls-remote:${repo}`, () => - spawn(['ls-remote', repo], opts) - .then(({ stdout }) => linesToRevs(stdout.trim().split('\n'))) - .then(revs => { - revsCache.set(repo, revs) - return revs - }) - ) + const { stdout } = await spawn(['ls-remote', repo], opts) + const revs = linesToRevs(stdout.trim().split('\n')) + revsCache.set(repo, revs) + return revs } diff --git a/test/revs.js b/test/revs.js index ba6e5da..7f61595 100644 --- a/test/revs.js +++ b/test/revs.js @@ -120,3 +120,28 @@ t.test('check the revs', async t => { Object.keys(r.shas).forEach(sha => r.shas[sha].forEach(ref => t.equal(r.refs[ref].sha, sha, `shas list is consistent ${ref}`))) }) + +t.test('cache prevents repeated git ls-remote calls', async t => { + let callCount = 0 + const originalSpawn = require('../lib/spawn.js') + + // Mock `spawn` to track calls + require.cache[require.resolve('../lib/spawn.js')].exports = (...args) => { + callCount++ + return originalSpawn(...args) + } + + // Force reloading revs.js after modifying spawn + delete require.cache[require.resolve('../lib/revs.js')] + const revsModule = require('../lib/revs.js') + + await revsModule(repo) // First call should hit `git ls-remote` + await revsModule(repo) // Second call should use cache + + t.equal(callCount, 1, 'git ls-remote should be called only once due to caching') + + // Restore original spawn.js + t.teardown(() => { + require.cache[require.resolve('../lib/spawn.js')].exports = originalSpawn + }) +})