Skip to content

Commit

Permalink
重複しないように修正
Browse files Browse the repository at this point in the history
  • Loading branch information
hyphen-o committed Oct 3, 2024
1 parent f96769f commit f602807
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ export const add = async (options: Array<string>): Promise<void> => {
const blobObject = new BlobObject(content);
const hash = await blobObject.dumpBlobObject();

if (!hash) {
const gitIndex = new GitIndex(GIT_INDEX);
await gitIndex.initialize();

if (gitIndex.checkDuplicate(filePath, hash)) {
console.log("Nothing has changed.");
return;
}

const gitIndex = new GitIndex(GIT_INDEX);
await gitIndex.initialize();
await gitIndex.pushEntry(filePath, hash);
await gitIndex.dumpIndex();
};
File renamed without changes.
6 changes: 3 additions & 3 deletions src/models/blob-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { mkdir, writeFile } from "node:fs/promises";
import { deflateSync } from "node:zlib";

import { exists } from "../functions/exists.js";
import { generateObjectPath } from "../functions/generate-object-path.js";
import { generateObjectPath } from "../functions/path.js";

export class BlobObject {
constructor(private readonly content: Buffer) {}

public dumpBlobObject = async (): Promise<string | undefined> => {
public dumpBlobObject = async (): Promise<string> => {
const header = Buffer.from(`blob ${this.content.length.toString()}\x00`);
const store = Buffer.concat([
Uint8Array.from(header),
Expand All @@ -23,7 +23,7 @@ export class BlobObject {
const { dirPath, filePath } = generateObjectPath(hash);
const compressedBlobObject = deflateSync(Uint8Array.from(store));

if (await exists(filePath)) return;
if (await exists(filePath)) return hash;

if (!(await exists(dirPath))) await mkdir(dirPath);

Expand Down
16 changes: 16 additions & 0 deletions src/models/git-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,25 @@ export class GitIndex {
return this.entries.map((entry) => entry.filePath);
};

public checkDuplicate = (filePath: string, hash: string): boolean => {
return this.entries.some(
(entry) => entry.filePath === filePath && entry.hash === hash,
);
};

private deleteDuplicate = (filePath: string): void => {
const index = this.entries.findIndex(
(entry) => entry.filePath === filePath,
);
if (index !== -1) this.entries.splice(index, 1);
};

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

//同じファイルパスのentryは重複を防ぐために削除しておく
this.deleteDuplicate(filePath);

// https://nodejs.org/api/fs.html#class-fsstats
const entry: Entry = {
cTimeSec: Math.floor(fileStat.ctime.getTime() / 1000),
Expand Down

0 comments on commit f602807

Please sign in to comment.