Skip to content

Commit

Permalink
blobモデルの実装 (#3)
Browse files Browse the repository at this point in the history
* git logコマンドの追加

* messageの取得方法の修正

* コマンドの修正

* log.tsの修正

* commitをクラス化

* コマンド入力時の待ち時間短縮

* blobモデルの作成

* compress-buffer.tsの削除

* addコマンドの追加

* eslintの修正

* package.json & eslintの修正

* package.json & readmeの修正

* レビュー後修正

* ハッシュからパスを生成する処理を共通化

* contentをバイナリデータのまま圧縮するように修正
  • Loading branch information
hyphen-o authored Oct 2, 2024
1 parent b5dd205 commit 7f923b2
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 8 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ npm run lint:fix
# run the formatter
npm run format:check
npm run format:fix

# run all check or fix
npm run check
npm run fix

```

## publish the package
Expand Down
11 changes: 10 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ export default [
allowExpressions: true,
},
],
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-unused-vars": [
"warn",
{
argsIgnorePattern: "^_",
},
],
"@typescript-eslint/array-type": [
"error",
{
Expand All @@ -61,6 +66,10 @@ export default [
{
files: ["**/*.js", "**/*.mjs"],
...eslint.configs.recommended,
//追加でルールを指定
rules: {
"no-unused-vars": ["error", { argsIgnorePattern: "^_.*$" }],
},
languageOptions: {
sourceType: "module",
globals: {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"lint:fix": "eslint --fix .",
"format:check": "prettier --check .",
"format:fix": "prettier --write .",
"check": "npm run format:check && npm run lint:check",
"fix": "npm run format:fix && npm run lint:fix",
"build": "tsc",
"watch": "tsc --watch"
},
Expand Down
23 changes: 23 additions & 0 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readFileSync } from "node:fs";

import { coloredLog } from "../functions/colored-log.js";
import { BlobObject } from "../models/blob-object.js";

export const add = (options: Array<string>): void => {
const filePath = options[0];

//引数にファイルパスが含まれていなかった場合の処理
if (!filePath) {
console.log("Nothing specified, nothing added.");
coloredLog({
text: "hint: Maybe you wanted to say 'git add XXX'?",
color: "yellow",
});
return;
}

const content = readFileSync(filePath);

const blobObject = new BlobObject(content);
blobObject.dumpBlobObject();
};
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { add } from "./add.js";
import { log } from "./log.js";

export const validCommand = {
add: add,
log: log,
help: () => {
console.log("Available commands:");
Expand Down
2 changes: 1 addition & 1 deletion src/commands/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const displayCommitHistory = (
});
};

export const log = (): void => {
export const log = (_options?: Array<string>): void => {
const headHash = extractHeadHash();

if (!headHash) {
Expand Down
12 changes: 7 additions & 5 deletions src/functions/colored-log.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
const colors = {
//https://qiita.com/shuhei/items/a61b4324fd5dbc1af79b
yellow: "\u001b[33m",
red: "\u001b[31m",
};

export const coloredLog = ({
text,
color,
}: {
text: string;
color?: "yellow";
color?: keyof typeof colors;
}): void => {
const RESET = "\u001b[0m";
const colors = {
//https://qiita.com/shuhei/items/a61b4324fd5dbc1af79b
yellow: "\u001b[33m",
};

if (color && color in colors) {
console.log(colors[color] + text + RESET);
Expand Down
17 changes: 17 additions & 0 deletions src/functions/generate-object-path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { join } from "node:path";

import { GIT_OBJECTS } from "../constants.js";

export const generateObjectPath = (
hash: string,
): {
dirPath: string;
filePath: string;
} => {
const dirPath = join(GIT_OBJECTS, hash.slice(0, 2));
const filePath = join(GIT_OBJECTS, hash.slice(0, 2), hash.slice(2));
return {
dirPath,
filePath,
};
};
31 changes: 31 additions & 0 deletions src/models/blob-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import { deflateSync } from "node:zlib";

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

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

public dumpBlobObject = (): void => {
const header = Buffer.from(`blob ${this.content.length.toString()}\x00`);
const store = Buffer.concat([
Uint8Array.from(header),
Uint8Array.from(this.content),
]);

//16進数表示のため,hexに変換
const hash = createHash("sha1")
.update(Uint8Array.from(store))
.digest("hex");

const { dirPath, filePath } = generateObjectPath(hash);
const compressedBlobObject = deflateSync(Uint8Array.from(store));

if (existsSync(filePath)) return;

if (!existsSync(dirPath)) mkdirSync(dirPath);

writeFileSync(filePath, Uint8Array.from(compressedBlobObject));
};
}
4 changes: 3 additions & 1 deletion src/mygit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const mygit = async (argv: Array<string>): Promise<void> => {
: undefined;

if (runCommand) {
runCommand();
const options = argv.slice(3);

runCommand(options);
} else {
console.log(`mygit: '${command}' is not a valid mygit command.\n`);
validCommand.help();
Expand Down

0 comments on commit 7f923b2

Please sign in to comment.