git-arweave-lfs
Version:
A Git extension for versioning large files with Arweave storage
115 lines • 5.04 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanFilter = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const crypto_1 = require("crypto");
const simple_git_1 = __importDefault(require("simple-git"));
const config_1 = require("../config");
const arweave_1 = require("../arweave");
const cleanFilter = async (filePath) => {
try {
if (filePath.endsWith('.gitattributes')) {
const originalContent = (0, fs_1.readFileSync)(filePath, 'utf8');
console.log(originalContent);
return;
}
const git = (0, simple_git_1.default)();
const root = await git.revparse(["--show-toplevel"]);
const gitDir = (0, path_1.join)(root, ".git");
const config = new config_1.GitArweaveConfig(gitDir);
const arweaveManager = await arweave_1.ArweaveManager.create(config, gitDir);
const existingRef = arweaveManager.getFileReference(filePath);
const stats = (0, fs_1.statSync)(filePath);
let txId;
let fileHash;
if (existingRef) {
txId = existingRef.txId;
fileHash = existingRef.hash;
}
else {
const fileBuffer = (0, fs_1.readFileSync)(filePath);
const computedHash = (0, crypto_1.createHash)('sha256').update(fileBuffer).digest('hex');
try {
const gitResult = await git.raw(['ls-files', '-s', filePath]);
if (gitResult.trim()) {
const [, blobHash] = gitResult.trim().split(/\s+/);
const storedPointer = await git.raw(['cat-file', '-p', blobHash]);
const lines = storedPointer.trim().split('\n');
let storedTxId = null;
let storedHash = null;
for (const line of lines) {
if (line.startsWith('arweave-tx-id ')) {
storedTxId = line.substring('arweave-tx-id '.length);
}
else if (line.startsWith('oid sha256:')) {
storedHash = line.substring('oid sha256:'.length);
}
}
if (storedHash === computedHash && storedTxId) {
txId = storedTxId;
fileHash = computedHash;
}
else {
txId = await arweaveManager.uploadFile(filePath);
const updatedRef = arweaveManager.getFileReference(filePath);
fileHash = updatedRef?.hash || computedHash;
}
}
else {
txId = await arweaveManager.uploadFile(filePath);
const updatedRef = arweaveManager.getFileReference(filePath);
fileHash = updatedRef?.hash || computedHash;
}
}
catch (uploadError) {
if (uploadError.message?.includes('Arweave wallet not configured')) {
const gitResult = await git.raw(['ls-files', '-s', filePath]);
if (gitResult.trim()) {
const [, blobHash] = gitResult.trim().split(/\s+/);
const storedPointer = await git.raw(['cat-file', '-p', blobHash]);
const lines = storedPointer.trim().split('\n');
let storedTxId = null;
let storedHash = null;
for (const line of lines) {
if (line.startsWith('arweave-tx-id ')) {
storedTxId = line.substring('arweave-tx-id '.length);
}
else if (line.startsWith('oid sha256:')) {
storedHash = line.substring('oid sha256:'.length);
}
}
if (storedHash === computedHash && storedTxId) {
txId = storedTxId;
fileHash = computedHash;
}
else {
throw uploadError;
}
}
else {
throw uploadError;
}
}
else {
throw uploadError;
}
}
}
const pointerContent = `version git-arweave-lfs:v1
oid sha256:${fileHash}
size ${stats.size}
arweave-tx-id ${txId}
`;
console.log(pointerContent);
}
catch (error) {
console.error('Error in clean filter:', error);
process.exit(1);
}
};
exports.cleanFilter = cleanFilter;
//# sourceMappingURL=clean.js.map
;