2021-11-29 02:03:49 +00:00
|
|
|
|
import fs from 'fs';
|
2021-11-25 12:27:33 +00:00
|
|
|
|
|
|
|
|
|
import git from 'isomorphic-git';
|
|
|
|
|
import http from 'isomorphic-git/http/node';
|
|
|
|
|
|
2021-11-29 02:03:28 +00:00
|
|
|
|
import { isVacantAndWriteable, pointsToLFS } from './util';
|
2021-11-25 12:27:33 +00:00
|
|
|
|
import downloadBlobFromPointer from './download';
|
|
|
|
|
import { readPointer } from "./pointers";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const SYMLINK_MODE = 40960;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Populates LFS cache for each repository object that is an LFS pointer.
|
|
|
|
|
*
|
|
|
|
|
* Does not touch working directory.
|
|
|
|
|
*
|
|
|
|
|
* NOTE: If LFS cache path, as extracted from the pointer,
|
|
|
|
|
* is not writeable at the time of download start,
|
|
|
|
|
* the object will be silently skipped;
|
|
|
|
|
* if LFS cache path is not writeable at the time download completes,
|
|
|
|
|
* an error will be thrown.
|
|
|
|
|
*/
|
|
|
|
|
export default async function populateCache(workDir: string, ref: string = 'HEAD') {
|
|
|
|
|
const remoteURL = await git.getConfig({
|
|
|
|
|
fs,
|
|
|
|
|
dir: workDir,
|
|
|
|
|
path: 'remote.origin.url',
|
|
|
|
|
});
|
|
|
|
|
if (remoteURL) {
|
|
|
|
|
await git.walk({
|
|
|
|
|
fs,
|
|
|
|
|
dir: workDir,
|
|
|
|
|
trees: [git.TREE({ ref })],
|
2021-11-29 02:04:27 +00:00
|
|
|
|
map: async function lfsDownloadingWalker(filepath, entries) {
|
2021-11-25 12:27:33 +00:00
|
|
|
|
if (entries === null || entries[0] === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [entry] = entries;
|
|
|
|
|
const entryType = await entry.type();
|
|
|
|
|
|
|
|
|
|
if (entryType === 'tree') {
|
|
|
|
|
// Walk children
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
} else if (entryType === 'blob' && (await entry.mode()) !== SYMLINK_MODE) {
|
|
|
|
|
const content = await entry.content();
|
|
|
|
|
|
|
|
|
|
if (content) {
|
|
|
|
|
const buff = Buffer.from(content.buffer);
|
|
|
|
|
|
|
|
|
|
if (pointsToLFS(buff)) {
|
|
|
|
|
const pointer = readPointer({ dir: workDir, content: buff });
|
|
|
|
|
|
2021-11-25 13:05:51 +00:00
|
|
|
|
// Don’t even start the download if LFS cache path is not accessible,
|
|
|
|
|
// or if it already exists
|
|
|
|
|
if (await isVacantAndWriteable(pointer.objectPath) === false)
|
2021-11-25 12:27:33 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
2021-11-29 02:04:57 +00:00
|
|
|
|
await downloadBlobFromPointer({ http, url: remoteURL }, pointer);
|
2021-11-25 12:27:33 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|