# @git-lazy/clone > Provides async and sync APIs for git clone operations with shallow clone support and customizable options ## Installation ```bash npm install @git-lazy/clone # or pnpm add @git-lazy/clone # or yarn add @git-lazy/clone ``` ## Usage ```typescript import gitClone, { gitCloneSync } from '@git-lazy/clone'; // 非同步克隆(推薦)/ Async clone (recommended) await gitClone('https://github.com/user/repo.git', { targetDir: 'my-project', cloneOptions: { depth: 1 }, }); // 同步克隆 / Sync clone gitCloneSync('https://github.com/user/repo.git', { targetDir: 'my-project', }); ``` ## API ### gitClone(remote, options?) 非同步執行 Git Clone 操作 / Asynchronously execute Git clone operation 透過子進程執行 `git clone` 命令,將遠端倉庫克隆到本地。預設採用淺層克隆(depth: 50)且僅克隆單一分支。 Executes `git clone` command via child process to clone remote repository to local. Defaults to shallow clone (depth: 50) and single branch only. **Parameters:** - `remote` (string) - Git 遠端倉庫 URL / Git remote repository URL - `options` (IOptionsGitClone, optional) - Clone 選項 / Clone options - `cwd` (string) - 工作目錄路徑,預設為 `process.cwd()` / Working directory path, defaults to `process.cwd()` - `targetDir` (string) - 目標目錄名稱或路徑 / Target directory name or path - `cloneOptions` (IOptionsClone) - Clone 命令選項 / Clone command options - `depth` (number) - 克隆深度(提交歷史層數)/ Clone depth (number of commit history layers) - `singleBranch` (boolean) - 是否僅克隆單一分支 / Whether to clone only a single branch - `noTags` (boolean) - 是否不克隆標籤 / Whether to not clone tags - `spawnOptions` (ISpawnGitAsyncOptions) - 子進程產生選項 / Child process spawn options **Returns:** `Promise` - 子進程執行結果 Promise / Child process execution result Promise **Example:** ```typescript import { gitClone } from '@git-lazy/clone'; // 基本克隆 - 使用預設選項(depth: 50, singleBranch: true) // Basic clone - using default options (depth: 50, singleBranch: true) await gitClone('https://github.com/user/repo.git'); // 指定目標目錄 // Specify target directory await gitClone('https://github.com/user/repo.git', { targetDir: 'my-project', }); // 完整選項配置 // Full options configuration await gitClone('https://github.com/user/repo.git', { cwd: '/path/to/parent', targetDir: 'my-repo', cloneOptions: { depth: 1, singleBranch: true, noTags: true, }, spawnOptions: { env: { ...process.env }, }, }); ``` ### gitCloneSync(remote, options?) 同步執行 Git Clone 操作 / Synchronously execute Git clone operation 透過子進程同步執行 `git clone` 命令,阻塞直到克隆完成。 Synchronously executes `git clone` command via child process, blocking until clone completes. **Parameters:** - `remote` (string) - Git 遠端倉庫 URL / Git remote repository URL - `options` (IOptionsGitCloneSync, optional) - Clone 選項 / Clone options - `cwd` (string) - 工作目錄路徑,預設為 `process.cwd()` / Working directory path, defaults to `process.cwd()` - `targetDir` (string) - 目標目錄名稱或路徑 / Target directory name or path - `cloneOptions` (IOptionsClone) - Clone 命令選項 / Clone command options - `depth` (number) - 克隆深度(提交歷史層數)/ Clone depth (number of commit history layers) - `singleBranch` (boolean) - 是否僅克隆單一分支 / Whether to clone only a single branch - `noTags` (boolean) - 是否不克隆標籤 / Whether to not clone tags - `spawnOptions` (ISpawnGitSyncOptions) - 子進程產生選項(同步)/ Child process spawn options (sync) **Returns:** `SpawnSyncReturns` - 子進程同步執行結果 / Child process sync execution result **Example:** ```typescript import { gitCloneSync } from '@git-lazy/clone'; // 基本同步克隆 // Basic sync clone const result = gitCloneSync('https://github.com/user/repo.git'); // 指定選項 // With options const result = gitCloneSync('https://github.com/user/repo.git', { targetDir: 'my-project', cloneOptions: { depth: 1, singleBranch: true, }, }); // 檢查執行結果 // Check execution result if (result.status === 0) { console.log('克隆成功 / Clone successful'); } ``` ## Types ### IOptionsClone Clone 命令選項設定 / Clone command options configuration | Property | Type | Description | |----------|------|-------------| | `depth` | `number` | 克隆深度(提交歷史層數)/ Clone depth | | `singleBranch` | `boolean` | 是否僅克隆單一分支 / Whether to clone only a single branch | | `noTags` | `boolean` | 是否不克隆標籤 / Whether to not clone tags | ## Dependencies - `@git-lazy/spawn` - Git 子進程產生 / Git child process spawning - `@git-lazy/util-args` - 參數處理工具 / Argument handling utilities - `tslib` - TypeScript 輔助函式 / TypeScript helpers ## License ISC