refactor(populateCache): support onProgress; don’t read remote URL from Git config

This commit is contained in:
Anton Strogonoff 2021-11-29 03:06:19 +01:00
parent 6d43595383
commit d22df35cfc

View file

@ -1,7 +1,7 @@
import fs from 'fs'; import fs from 'fs';
import git from 'isomorphic-git'; import git from 'isomorphic-git';
import http from 'isomorphic-git/http/node'; import http, { GitProgressEvent } from 'isomorphic-git/http/node';
import { isVacantAndWriteable, pointsToLFS } from './util'; import { isVacantAndWriteable, pointsToLFS } from './util';
import downloadBlobFromPointer from './download'; import downloadBlobFromPointer from './download';
@ -11,6 +11,9 @@ import { readPointer } from "./pointers";
const SYMLINK_MODE = 40960; const SYMLINK_MODE = 40960;
type ProgressHandler = (progress: GitProgressEvent) => void
/** /**
* Populates LFS cache for each repository object that is an LFS pointer. * Populates LFS cache for each repository object that is an LFS pointer.
* *
@ -18,26 +21,30 @@ const SYMLINK_MODE = 40960;
* *
* NOTE: If LFS cache path, as extracted from the pointer, * NOTE: If LFS cache path, as extracted from the pointer,
* is not writeable at the time of download start, * is not writeable at the time of download start,
* the object will be silently skipped; * the object will be silently skipped.
* if LFS cache path is not writeable at the time download completes, *
* an error will be thrown. * NOTE: This function skips objects silently in case of errors.
*
* NOTE: onProgress currently doesnt report loaded/total values accurately.
*/ */
export default async function populateCache(workDir: string, ref: string = 'HEAD') { export default async function populateCache(
const remoteURL = await git.getConfig({ workDir: string,
fs, remoteURL: string,
dir: workDir, ref: string = 'HEAD',
path: 'remote.origin.url', onProgress?: ProgressHandler,
}); ) {
if (remoteURL) {
await git.walk({ await git.walk({
fs, fs,
dir: workDir, dir: workDir,
trees: [git.TREE({ ref })], trees: [git.TREE({ ref })],
map: async function lfsDownloadingWalker(filepath, entries) { map: async function lfsDownloadingWalker(filepath, entries) {
if (entries === null || entries[0] === null) { if (entries === null || entries[0] === null) {
return null; return null;
} }
onProgress?.({ phase: `skimming: ${filepath}`, loaded: 5, total: 10 });
const [entry] = entries; const [entry] = entries;
const entryType = await entry.type(); const entryType = await entry.type();
@ -52,6 +59,7 @@ export default async function populateCache(workDir: string, ref: string = 'HEAD
const buff = Buffer.from(content.buffer); const buff = Buffer.from(content.buffer);
if (pointsToLFS(buff)) { if (pointsToLFS(buff)) {
const pointer = readPointer({ dir: workDir, content: buff }); const pointer = readPointer({ dir: workDir, content: buff });
// Dont even start the download if LFS cache path is not accessible, // Dont even start the download if LFS cache path is not accessible,
@ -59,6 +67,7 @@ export default async function populateCache(workDir: string, ref: string = 'HEAD
if (await isVacantAndWriteable(pointer.objectPath) === false) if (await isVacantAndWriteable(pointer.objectPath) === false)
return; return;
onProgress?.({ phase: `downloading: ${filepath}`, loaded: 5, total: 10 });
await downloadBlobFromPointer({ http, url: remoteURL }, pointer); await downloadBlobFromPointer({ http, url: remoteURL }, pointer);
@ -68,5 +77,4 @@ export default async function populateCache(workDir: string, ref: string = 'HEAD
return; return;
} }
}); });
}
} }