Suppose I have a Git URL + revision and I want to fetch the README.md file, if present.
My current solution is to:
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.
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:
await git.clone({fs, gitdir, url, noCheckout: true})let oid = await git.resolveRef({fs, gitdir, ref}) where ref is the revision, branch or taglet {object} = await git.readObject({fs, gitdir, oid, filepath: 'README.md', encoding: 'utf8'})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.
Most helpful comment
I should add an option to checkout to check out a specific filepath. That would save a lot of disk thrashing.