跳至内容

Git

git 助手提供了一个轻量级封装,用于调用 git 可执行文件进行仓库操作。

方法

defaultBranch

解析仓库中的默认分支,通常是mainmaster

const df = await git.defaultBranch()

lastTag

获取仓库中的最后一个标签。

const tag = await git.lastTag()

分支

获取当前代码仓库的分支。

const branchName = await git.branch()

执行

在代码仓库中执行git命令并返回标准输出。

const output = await git.exec(["status"])

listBranches

列出git仓库中的分支。

const branches = await git.listBranches()

listFiles

在git仓库中查找特定文件。

const files = await git.listFiles("modified")

差异

获取当前仓库状态的差异。

const diffOutput = await git.diff({ staged: true })

日志

列出git仓库中的提交记录。

const commits = await git.log()

配置忽略规则

由于GenAIScript使用git,它已经支持.gitignore指令。您还可以通过工作区根目录下的.gitignore.genai文件提供额外的仓库级忽略规则。

.gitignore.genai
**/genaiscript.d.ts

浅克隆

您可以创建仓库的缓存浅克隆来同时处理多个仓库。 shallowClone 方法会返回一个 git 客户端实例。

克隆的仓库会被创建在.genaiscript/git/...目录下,并根据repository/branch/commit信息进行缓存。

const clone = await git.shallowClone("microsoft/genaiscript")

您可以提供选项来强制克隆 和/或在克隆后运行install命令。

const clone = await git.shallowClone("microsoft/genaiscript", {
force: true,
install: true,
})

其他仓库中的Git

使用 git.client 在不同的工作目录下打开 git 客户端。这允许您在不同的代码库上运行 git 命令。

const other = git.client("/path/to/other/repo")
const branch = await other.branch()