-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* git logコマンドの追加 * messageの取得方法の修正 * コマンドの修正 * log.tsの修正 * commitをクラス化 * コマンド入力時の待ち時間短縮 * blobモデルの作成 * compress-buffer.tsの削除 * addコマンドの追加 * eslintの修正 * package.json & eslintの修正 * package.json & readmeの修正 * レビュー後修正 * ハッシュからパスを生成する処理を共通化 * contentをバイナリデータのまま圧縮するように修正
- Loading branch information
Showing
10 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters