revs.js 640 B

12345678910111213141516171819202122232425262728
  1. const pinflight = require('promise-inflight')
  2. const spawn = require('./spawn.js')
  3. const LRU = require('lru-cache')
  4. const revsCache = new LRU({
  5. max: 100,
  6. maxAge: 5 * 60 * 1000
  7. })
  8. const linesToRevs = require('./lines-to-revs.js')
  9. module.exports = async (repo, opts = {}) => {
  10. if (!opts.noGitRevCache) {
  11. const cached = revsCache.get(repo)
  12. if (cached) {
  13. return cached
  14. }
  15. }
  16. return pinflight(`ls-remote:${repo}`, () =>
  17. spawn(['ls-remote', repo], opts)
  18. .then(({ stdout }) => linesToRevs(stdout.trim().split('\n')))
  19. .then(revs => {
  20. revsCache.set(repo, revs)
  21. return revs
  22. })
  23. )
  24. }