コンテンツにスキップ

Git操作

主要なインターフェース

TypeGit

各ランタイム向けに事前設定された便利クラス:

import { TypeGit } from 'type-git/node';
const git = new TypeGit();
// Node.jsアダプターで自動設定

使用場面: ほとんどのアプリケーション。最もシンプルな開始方法。

Git

リポジトリ非依存の操作のためのコアインターフェース:

interface Git {
open(path: string): Promise<WorktreeRepo | BareRepo>;
clone(url: string, path: string, opts?: CloneOpts): Promise<WorktreeRepo | BareRepo>;
init(path: string, opts?: InitOpts): Promise<WorktreeRepo | BareRepo>;
lsRemote(url: string, opts?: LsRemoteOpts): Promise<LsRemoteResult>;
version(): Promise<string>;
raw(argv: string[]): Promise<RawResult>;
}

使用場面: 基礎となるインターフェースが必要な場合、またはカスタムアダプターでcreateGit()を使用する場合。

WorktreeRepo

作業ディレクトリを持つ標準リポジトリ:

interface WorktreeRepo {
readonly workdir: string;
// コア操作
status(opts?: StatusOpts): Promise<StatusPorcelain>;
log(opts?: LogOpts): Promise<Commit[]>;
add(paths: string[], opts?: AddOpts): Promise<void>;
commit(opts?: CommitOpts): Promise<CommitResult>;
// リモート操作
fetch(opts?: FetchOpts): Promise<void>;
push(opts?: PushOpts): Promise<void>;
pull(opts?: PullOpts): Promise<void>;
// ネストされた操作
branch: BranchOperations;
stash: StashOperations;
tag: TagOperations;
lfs: LfsOperations;
lfsExtra: LfsExtraOperations;
worktree: WorktreeOperations;
// rawアクセス
raw(argv: string[]): Promise<RawResult>;
}

BareRepo

作業ディレクトリのないリポジトリ(ミラー、サーバー):

interface BareRepo {
readonly gitDir: string;
fetch(opts?: FetchOpts): Promise<void>;
push(opts?: PushOpts): Promise<void>;
raw(argv: string[]): Promise<RawResult>;
}

注意: ベアリポジトリは作業ディレクトリがないため、操作が制限されます。

CliRunner

低レベルのコマンド実行エンジン:

class CliRunner {
constructor(adapters: RuntimeAdapters, options?: CliRunnerOptions);
run(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
runOrThrow(context: ExecutionContext, args: string[], opts?: ExecOpts): Promise<RawResult>;
}

使用場面: カスタムgitラッパーの構築やtype-gitの拡張。

GitError

セマンティック情報を持つ構造化エラー:

class GitError extends Error {
readonly kind: 'NonZeroExit' | 'Aborted' | 'SpawnFailed';
readonly category: GitErrorCategory;
readonly exitCode: number;
readonly stdout: string;
readonly stderr: string;
readonly argv: string[];
}

ネストされた操作

いくつかの操作は名前空間プロパティの下にグループ化されています:

branch

await repo.branch.list(); // ブランチ一覧
await repo.branch.current(); // 現在のブランチ名を取得
await repo.branch.create('feature-x'); // ブランチ作成
await repo.branch.delete('old-branch'); // ブランチ削除
await repo.branch.rename('old', 'new'); // ブランチ名変更

stash

await repo.stash.list(); // スタッシュ一覧
await repo.stash.push({ message: 'WIP' }); // 変更をスタッシュ
await repo.stash.pop(); // 最新のスタッシュを適用
await repo.stash.apply({ index: 1 }); // 特定のスタッシュを適用
await repo.stash.drop(0); // スタッシュエントリを削除
await repo.stash.clear(); // 全スタッシュをクリア

tag

await repo.tag.list(); // タグ一覧
await repo.tag.create('v1.0.0'); // 軽量タグを作成
await repo.tag.create('v1.0.0', { // 注釈付きタグを作成
message: 'Release 1.0'
});
await repo.tag.delete('old-tag'); // タグ削除
await repo.tag.show('v1.0.0'); // タグ情報を取得

lfs / lfsExtra

// 基本LFS
await repo.lfs.pull();
await repo.lfs.push();
await repo.lfs.status();
// 高度なLFS(2フェーズパターン)
await repo.lfsExtra.preUpload();
await repo.lfsExtra.preDownload({ ref: 'main' });

worktree

await repo.worktree.list(); // ワークツリー一覧
await repo.worktree.add('./wt', { // ワークツリー追加
branch: 'feature-x'
});
await repo.worktree.remove('./wt'); // ワークツリー削除
await repo.worktree.lock('./wt'); // ワークツリーをロック
await repo.worktree.unlock('./wt'); // ワークツリーのロック解除