Skip to content

Commit

Permalink
entryBufferの作成機能の実装
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphen-o committed Oct 2, 2024
1 parent 5db8054 commit 6581e06
Showing 1 changed file with 95 additions and 11 deletions.
106 changes: 95 additions & 11 deletions src/models/git-index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,119 @@
import { stat } from "node:fs/promises";

import { exists } from "../functions/exists.js";

interface Entry {
ctimeSec: string;
cTimeSec: number;
cTimeNanoSec: number;
mTimeSec: number;
mTimeNanoSec: number;
device: number,
inode: number,
mode: number,
uId: number,
gId: number,
size: number,
hash: string,
flag: boolean,
length: number,
filePath: string,
}

export class GitIndex {
private Entries: Array<Entry>;
private entries: Array<Entry>;

constructor(private readonly gitIndexPath: string) {
this.Entries = [];
this.entries = [];
}

public initialize = async (): Promise<void> => {
await this.readEntries();
}

public clearEntry = (): void => {
this.Entries = [];
this.entries = [];
};

public pushEntry = async (filePath: string): Promise<void> => {
await this.readEntries();

public pushEntry = async (filePath: string, hash: string): Promise<void> => {
const fileStat = await stat(filePath);

console.log(fileStat);
// https://nodejs.org/api/fs.html#class-fsstats
const entry: Entry = {
cTimeSec: fileStat.ctime.getTime(),
cTimeNanoSec: fileStat.ctimeMs,
mTimeSec: fileStat.mtime.getTime(),
mTimeNanoSec: fileStat.mtimeMs,
device: fileStat.dev,
inode: fileStat.ino,
mode: fileStat.mode,
uId: fileStat.uid,
gId: fileStat.gid,
size: fileStat.size,
hash: hash,
flag: false,
length: filePath.length,
filePath: filePath,
};

this.entries.push(entry)
};

private createEntriesBuffer = (): Buffer => {
const buffers: Array<Buffer> = [];

for (const entry of this.entries) {
const entryBuffer = this.createEntryBuffer(entry);
buffers.push(entryBuffer);
}

return Buffer.concat([Uint8Array.from(buffers)]);
}

private createEntryBuffer = (entry: Entry): Buffer => {
let entryBufferLength = 64 + entry.length;
entryBufferLength += 8 - (entryBufferLength % 8);

const entryBuffer = Buffer.alloc(entryBufferLength);
let offset = 0;

offset = this.writeUInt32BE(entryBuffer, entry.cTimeSec, offset);
offset = this.writeUInt32BE(entryBuffer, entry.cTimeNanoSec, offset);
offset = this.writeUInt32BE(entryBuffer, entry.mTimeSec, offset);
offset = this.writeUInt32BE(entryBuffer, entry.mTimeNanoSec, offset);
offset = this.writeUInt32BE(entryBuffer, entry.device, offset);
offset = this.writeUInt32BE(entryBuffer, entry.inode, offset);
offset = this.writeUInt32BE(entryBuffer, entry.mode, offset);
offset = this.writeUInt32BE(entryBuffer, entry.uId, offset);
offset = this.writeUInt32BE(entryBuffer, entry.gId, offset);
offset = this.writeUInt32BE(entryBuffer, entry.size, offset);

Buffer.from(entry.hash, "hex").copy(Uint8Array.from(entryBuffer), offset);
offset += 20;

offset += 2;

entryBuffer.writeUInt16BE(entry.length, offset);
offset += 2;

Buffer.from(entry.filePath).copy(Uint8Array.from(entryBuffer), offset);

return entryBuffer;
}

private writeUInt32BE = (buffer: Buffer, value: number, offset: number): number => {
buffer.writeUInt32BE(value, offset);
return offset + 4;
}


public dumpIndex = (): void => {
console.log(this.Entries);

const headerBuffer = '';
const entriesBuffer = this.createEntriesBuffer()

console.log(this.entries);
};

private readEntries = async (): Promise<void> => {
private parseEntries = async (): Promise<void> => {
if (await exists(this.gitIndexPath)) return;

console.log(this.gitIndexPath);
Expand Down

0 comments on commit 6581e06

Please sign in to comment.