Skip to content

appendIndex 関数の実装#14

Merged
massan02 merged 4 commits intomainfrom
ulxsth/append-index
Mar 27, 2025
Merged

appendIndex 関数の実装#14
massan02 merged 4 commits intomainfrom
ulxsth/append-index

Conversation

@ulxsth
Copy link
Contributor

@ulxsth ulxsth commented Mar 27, 2025

Details

loadIndex で入手した Index オブジェクトに対し、新しい blob オブジェクトとファイル名を渡してエントリに登録できる appendIndex 関数を実装しました。

// src/gitIndex.ts
/**
 * Blob オブジェクトを保存し、GitIndex にエントリを追加する。
 * @param blob GitBlobObject
 * @param index GitIndex
 * @param fileName ファイル名
 * @returns 更新された GitIndex
 */
const appendIndex = (index: GitIndex, blob: GitBlobObject, fileName: string): void 

appendIndex はあくまで引数として渡された GitIndexObject の変更のみを行い、実際の .git/index を書き換えるわけではないことに留意してください。これは saveIndex の管轄です。

懸念点

エントリの一部情報は固定値で埋めており、実際のファイルと異なるため、予期せぬエラーを引き起こす可能性があります。

const entry: GitIndexEntry = {
    ctime: { seconds: now, nanoseconds: 0 },  // TODO: 値の取得
    mtime: { seconds: now, nanoseconds: 0 },  // TODO: 値の取得
    dev: 0, // TODO: 値の取得
    ino: 0, // TODO: 値の取得

...

Debug

example フォルダを作成し、適当なファイルを追加してコミットしたのち、以下のコマンドを実行してください。

npx tsx ../src/gitIndex.ts

テストでは、loadIndex でインデックスオブジェクトが読み込まれたのち、appendIndex でテストデータが入れられ、 saveIndex で更新されています。

const index = loadIndex();

const blob: GitBlobObject = { type: "blob", size: 5, content: "test\n" };
const updatedIndex = appendIndex(index, blob, "hoge.txt");
console.log(updatedIndex.entries);

saveIndex(updatedIndex);

実行すると、以下のようにエントリ一覧が表示されます。一番下にテストデータと同じエントリが追加されていることを確認してください。

$ nlx tsx ../src/gitIndex.ts
[
  {
    ctime: { seconds: 1743051843, nanoseconds: 327102829 },
    mtime: { seconds: 1743051843, nanoseconds: 327102829 },
    dev: 2080,
    ino: 199153,
    mode: 33188,
    uid: 1000,
    gid: 1000,
    fileSize: 2,
    sha1: '78981922613b2afb6025042ff6bd878ac1994e85',
    flags: { assumeValid: false, extended: false, stage: 0, nameLength: 5 },
    pathName: 'a.txt'
  },
  {
    ctime: { seconds: 1743052459, nanoseconds: 0 },
    mtime: { seconds: 1743052459, nanoseconds: 0 },
    dev: 0,
    ino: 0,
    mode: 33188,
    uid: 1000,
    gid: 1000,
    fileSize: 5,
    sha1: '9daeafb9864cf43055ae93beb0afd6c7d144bfa4',
    flags: { assumeValid: false, extended: false, stage: 0, nameLength: 8 },
    pathName: 'hoge.txt'
  }
]

念のため、git status で本元のgitから変更が検知できるかを確認します。
(テストデータに相当する実ファイルを作成していない場合 deleted 変更が追加されますが、気にしなくていいと思います

$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   hoge.txt

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        deleted:    hoge.txt

@massan02
Copy link
Contributor

動作確認済み

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants