UNPKG

git-documentdb

Version:

Offline-first database that syncs with Git

170 lines 6.91 kB
"use strict"; /** * GitDocumentDB * Copyright (c) Hidekazu Kubota * * This source code is licensed under the Mozilla Public License Version 2.0 * found in the LICENSE file in the root directory of gitDDB source tree. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readOldBlob = exports.getHistoryImpl = void 0; const isomorphic_git_1 = require("isomorphic-git"); const fs_extra_1 = __importDefault(require("fs-extra")); const error_1 = require("../error"); const blob_1 = require("./blob"); /** * Implementation of getHistory * * @throws {@link Err.DatabaseClosingError} * @throws {@link Err.RepositoryNotOpenError} * * @throws # Errors from blobToJsonDoc * @throws {@link Err.InvalidJsonObjectError} */ // eslint-disable-next-line complexity async function getHistoryImpl(gitDDB, shortName, collectionPath, serializeFormat, historyOptions, options, withMetaData = false) { var _a; if (gitDDB.isClosing) { throw new error_1.Err.DatabaseClosingError(); } if (!gitDDB.isOpened) { return Promise.reject(new error_1.Err.RepositoryNotOpenError()); } options !== null && options !== void 0 ? options : (options = { forceDocType: undefined, }); const fullDocPath = collectionPath + shortName; const docType = (_a = options.forceDocType) !== null && _a !== void 0 ? _a : (serializeFormat.hasObjectExtension(fullDocPath) ? 'json' : 'text'); if (docType === 'text') { // TODO: select binary or text by .gitattribtues } const docArray = []; const commits = await (0, isomorphic_git_1.log)({ fs: fs_extra_1.default, dir: gitDDB.workingDir, ref: 'main', }); let prevOid = ''; for (const commit of commits) { const commitOid = commit.oid; // eslint-disable-next-line no-await-in-loop const readBlobResult = await (0, isomorphic_git_1.readBlob)({ fs: fs_extra_1.default, dir: gitDDB.workingDir, oid: commitOid, filepath: fullDocPath, }).catch(() => undefined); const oid = readBlobResult === undefined ? undefined : readBlobResult.oid; // Skip consecutive same SHAs if (prevOid !== oid) { prevOid = oid; if ((historyOptions === null || historyOptions === void 0 ? void 0 : historyOptions.filter) === undefined || matchHistoryFilter(commit.commit.author, commit.commit.committer, historyOptions.filter)) { if (readBlobResult === undefined) { docArray.push(undefined); } else if (docType === 'json') { const [, extension] = fullDocPath.match(/.+(\..+?)$/); const shortId = serializeFormat.removeExtension(shortName); // eslint-disable-next-line max-depth if (withMetaData) { docArray.push((0, blob_1.blobToJsonDoc)(shortId, readBlobResult, true, serializeFormat, extension)); } else { docArray.push((0, blob_1.blobToJsonDoc)(shortId, readBlobResult, false, serializeFormat, extension)); } } else if (docType === 'text') { // eslint-disable-next-line max-depth if (withMetaData) { docArray.push((0, blob_1.blobToText)(shortName, readBlobResult, true)); } else { docArray.push((0, blob_1.blobToText)(shortName, readBlobResult, false)); } } else if (docType === 'binary') { // eslint-disable-next-line max-depth if (withMetaData) { docArray.push((0, blob_1.blobToBinary)(shortName, readBlobResult, true)); } else { docArray.push((0, blob_1.blobToBinary)(shortName, readBlobResult, false)); } } } } } while (docArray.length > 0 && docArray[docArray.length - 1] === undefined) { docArray.pop(); } return docArray; } exports.getHistoryImpl = getHistoryImpl; /** * readOldBlob */ async function readOldBlob(workingDir, fullDocPath, revision, historyOptions) { let readBlobResult; let prevSHA = ''; let oidCounter = -1; if (revision < 0) { return undefined; } const commits = await (0, isomorphic_git_1.log)({ fs: fs_extra_1.default, dir: workingDir, ref: 'main', }); for (const commit of commits) { const commitOid = commit.oid; // Skip merge commit if (commit.commit.parent.length > 1) { continue; } // Filtering if ((historyOptions === null || historyOptions === void 0 ? void 0 : historyOptions.filter) !== undefined && !matchHistoryFilter(commit.commit.author, commit.commit.committer, historyOptions.filter)) { continue; } // eslint-disable-next-line no-await-in-loop readBlobResult = await (0, isomorphic_git_1.readBlob)({ fs: fs_extra_1.default, dir: workingDir, oid: commitOid, filepath: fullDocPath, }).catch(() => undefined); const oid = readBlobResult === undefined ? undefined : readBlobResult.oid; // Skip consecutive same SHAs if (prevSHA !== oid) { prevSHA = oid; oidCounter++; } if (oidCounter >= revision) { break; } } if (oidCounter >= revision) { return readBlobResult; } return undefined; } exports.readOldBlob = readOldBlob; /** * matchHistoryFilter */ function matchHistoryFilter(author, committer, historyFilter) { var _a, _b, _c, _d; for (const filter of historyFilter) { if ((!((_a = filter.author) === null || _a === void 0 ? void 0 : _a.name) || filter.author.name === author.name) && (!((_b = filter.author) === null || _b === void 0 ? void 0 : _b.email) || filter.author.email === author.email) && (!((_c = filter.committer) === null || _c === void 0 ? void 0 : _c.name) || filter.committer.name === committer.name) && (!((_d = filter.committer) === null || _d === void 0 ? void 0 : _d.email) || filter.committer.email === committer.email)) return true; } return false; } //# sourceMappingURL=history.js.map