Isomorphic-git: Most efficient way to fetch a single file from a given revision or branch?

Created on 24 May 2018  路  5Comments  路  Source: isomorphic-git/isomorphic-git

Suppose I have a Git URL + revision and I want to fetch the README.md file, if present.

My current solution is to:

  • Create a temporary folder
  • Git clone from the URL into the folder
  • Git checkout the revision, branch or tag
  • Read
  • Clean-up the folder

This works, but is it the best approach using isomorphic-git?

I believe that the git CLI tool can do this efficiently using git archive.

faq

Most helpful comment

I should add an option to checkout to check out a specific filepath. That would save a lot of disk thrashing.

All 5 comments

Honestly the most efficient solution would probably be to use the Github/Bitbucket/Gitlab API. But if you're stuck using git, then the best you can do is use {depth: 1, singleBranch: true}. Assuming your commit is a branch head so you can clone just that branch.

I should add an option to checkout to check out a specific filepath. That would save a lot of disk thrashing.

OK you can now do this without calling checkout by calling readObject with a filepath parameter:

  • Create a temporary folder
  • await git.clone({fs, gitdir, url, noCheckout: true})
  • let oid = await git.resolveRef({fs, gitdir, ref}) where ref is the revision, branch or tag
  • let {object} = await git.readObject({fs, gitdir, oid, filepath: 'README.md', encoding: 'utf8'})
  • Clean-up the folder

Adding filepath argument to checkout will happen too soon. But readObject is kinda nice for this case because you can get the README data directly as a string without writing it to disk.

Cloning is still slowed down by the fact it checks out the branch at the end, which involves writing all those files. You can get around that by using init, config, and fetch directly, instead of clone... but I bet there's enough common use case for "I want to clone a repo but all I care about is one file" that it might make sense to add an option to skip the checkout step at the end of a normal clone.

it might make sense to add an option to skip the checkout step at the end of a normal clone.

FYI I have added a noCheckout parameter to clone in #240.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

westurner picture westurner  路  7Comments

tyru picture tyru  路  4Comments

juancampa picture juancampa  路  6Comments

wmhilton picture wmhilton  路  7Comments

wmhilton picture wmhilton  路  6Comments