repo_description: A pure JavaScript implementation of git for node and browsers!
repo_topics: browser git hacktoberfest isomorphic-javascript javascript nodejs vcs
code: // @ts-check
  import '../typedefs.js'
  
  import { _indexPack } from '../commands/indexPack.js'
  import { FileSystem } from '../models/FileSystem.js'
  import { assertParameter } from '../utils/assertParameter.js'
  import { discoverGitdir } from '../utils/discoverGitdir.js'
  import { join } from '../utils/join.js'
  
  /**
   * Create the .idx file for a given .pack file
   *
   * @param {object} args
   * @param {FsClient} args.fs - a file system client
   * @param {ProgressCallback} [args.onProgress] - optional progress event callback
   * @param {string} args.dir - The [working tree](dir-vs-gitdir.md) directory path
   * @param {string} [args.gitdir=join(dir,'.git')] - [required] The [git directory](dir-vs-gitdir.md) path
   * @param {string} args.filepath - The path to the .pack file to index
   * @param {object} [args.cache] - a [cache](cache.md) object
   *
   * @returns {Promise<{oids: string[]}>} Resolves with a list of the SHA-1 object ids contained in the packfile
   *
   * @example
   * let packfiles = await fs.promises.readdir('/tutorial/.git/objects/pack')
   * packfiles = packfiles.filter(name => name.endsWith('.pack'))
   * console.log('packfiles', packfiles)
   *
   * const { oids } = await git.indexPack({
   *   fs,
   *   dir: '/tutorial',
   *   filepath: `.git/objects/pack/${packfiles[0]}`,
   *   async onProgress (evt) {
   *     console.log(`${evt.phase}: ${evt.loaded} / ${evt.total}`)
   *   }
   * })
   * console.log(oids)
   *
   */
  export async function indexPack({
    fs,
    onProgress,
    dir,
    gitdir = join(dir, '.git'),
    filepath,
    cache = {},
  }) {
    try {
      assertParameter('fs', fs)
      assertParameter('dir', dir)
      assertParameter('gitdir', dir)
      assertParameter('filepath', filepath)
  
      const fsp = new FileSystem(fs)
      const updatedGitdir = await discoverGitdir({ fsp, dotgit: gitdir })
      return await _indexPack({
        fs: fsp,
        cache,
        onProgress,
        dir,
        gitdir: updatedGitdir,
        filepath,
      })
    } catch (err) {
      err.caller = 'git.indexPack'
      throw err
    }
  }
  