Git¶
Git
助手是 git
命令的轻量级封装。它可用于不同的目的: - 在 set_version()
方法中获取当前标签,并将其分配给 self.version
- 在第三方或开源软件包配方中的 source()
方法中克隆源代码(通常,优先使用 download()
或 get()
来获取发布 tarball) - 在 export()
方法中捕获您自己的软件包源代码的 “scm” 坐标(url,commit),以便稍后能够从源代码重现构建,并在 source()
方法中检索代码。请参阅 git-scm 捕获示例。
Git()
构造函数接收当前文件夹作为参数,但如果需要,可以更改它,例如,在 source()
中克隆某些仓库的源代码
def source(self):
git = Git(self) # by default, the current folder "."
git.clone(url="<repourl>", target="target") # git clone url target
# we need to cd directory for next command "checkout" to work
git.folder = "target" # cd target
git.checkout(commit="<commit>") # git checkout commit
另一种等效的方法是
def source(self):
git = Git(self, "target")
# Cloning in current dir, not a children folder
git.clone(url="<repourl>", target=".")
git.checkout(commit="<commit>")
- class Git(conanfile, folder='.', excluded=None)¶
Git 是 git 工具常用的几种模式的封装。
- 参数:
conanfile – Conanfile 实例。
folder – 当前目录,默认为
.
,即当前工作目录。excluded – 从 “dirty” 检查中排除的文件。它将与配置
core.scm:excluded
组合(配置具有更高的优先级)。它是一个fnmatch
模式列表。
- run(cmd, hidden_output=None)¶
执行
git <cmd>
- 返回值:
命令的控制台输出。
- get_commit(repository=False)¶
- 参数:
repository – 默认情况下获取定义文件夹的 commit,使用 repo=True 获取仓库的 commit。
- 返回值:
当前 commit,使用
git rev-list HEAD -n 1 -- <folder>
。返回最新的 commit,与本地未提交的更改无关。
- get_remote_url(remote='origin')¶
使用
git remote -v
获取远程 git 仓库的 URL。警告! 请注意,此方法将从
git remote -v
获取输出。如果您在 URL 中向远程仓库添加了令牌或凭据,它们将被暴露。凭据不应添加到 git 远程仓库定义中,而应使用凭据管理器或类似机制。如果您仍然想使用此方法,您有责任从结果中删除凭据。- 参数:
remote – 远程 git 仓库的名称(默认为 ‘origin’)。
- 返回值:
远程 git 仓库的 URL。
- commit_in_remote(commit, remote='origin')¶
使用
branch -r --contains <commit>
检查给定的 commit 是否存在于远程仓库中,并检查该远程仓库中是否存在分支。- 参数:
commit – 要检查的 Commit。
remote – 远程 git 仓库的名称(默认为 ‘origin’)。
- 返回值:
如果给定的 commit 存在于远程仓库中,则为 True,否则为 False。
- is_dirty(repository=False)¶
运行
git status -s
返回当前文件夹是否为 dirty。Git(..., excluded=[])
参数和core.scm:excluded
配置将定义要从此检查中跳过的文件模式。- 参数:
repository – 默认情况下检查当前文件夹是否为 dirty。如果 repository=True,它将检查根仓库文件夹,而不是当前文件夹。
- 返回值:
True,如果当前文件夹为 dirty。否则为 False。
- get_url_and_commit(remote='origin', repository=False)¶
这是一个高级方法,它同时返回当前 commit 和远程仓库 url。此方法旨在捕获包创建的当前远程坐标,以便以后可以再次从同一 commit 的源代码构建。这是行为
如果仓库是 dirty 的,它将引发异常。捕获 dirty 内容的坐标没有意义,因为它不可重现。如果存在本地更改,并且用户想要测试本地 conan create,则应首先提交更改(本地提交,而不是推送更改)。
如果仓库不是 dirty 的,但 commit 在给定的远程仓库中不存在,则该方法将返回该 commit 和本地用户检出的 URL。这样,可以在将某些更改推送到远程仓库之前,本地 conan create 创建包,测试一切正常。
如果仓库不是 dirty 的,并且 commit 存在于指定的远程仓库中,它将返回该 commit 和远程仓库的 url。
警告! 请注意,此方法将从
git remote -v
获取输出。如果您在 URL 中向远程仓库添加了令牌或凭据,它们将被暴露。凭据不应添加到 git 远程仓库定义中,而应使用凭据管理器或类似机制。如果您仍然想使用此方法,您有责任从结果中删除凭据。- 参数:
remote – 远程 git 仓库的名称(默认为 ‘origin’)。
repository – 默认情况下获取定义文件夹的 commit,使用 repo=True 获取仓库的 commit。
- 返回值:
(url, commit) 元组
- get_repo_root()¶
使用
git rev-parse --show-toplevel
获取当前仓库的顶层文件夹- 返回值:
仓库顶层文件夹。
- clone(url, target='', args=None, hide_url=True)¶
执行
git clone <url> <args> <target>
操作,其中 target 是目标目录。- 参数:
url – 远程仓库的 URL。
target – 目标文件夹。
args – 要传递给 git clone 的额外参数列表。
hide_url – 从日志输出中隐藏 URL,以防止意外的凭据泄露。可以通过传递
False
来禁用。
- fetch_commit(url, commit, hide_url=True)¶
实验性功能:执行单次 commit 获取和检出,而不是完整克隆,应该更快。
- 参数:
url – 远程仓库的 URL。
commit – 要检出的 commit ref。
hide_url – 从日志输出中隐藏 URL,以防止意外的凭据泄露。可以通过传递
False
来禁用。
- checkout(commit)¶
使用
git checkout <commit>
检出给定的 commit。- 参数:
commit – 要检出的 Commit。
- included_files()¶
- 运行
git ls-files --full-name --others --cached --exclude-standard
以获取列表 未被
.gitignore
忽略的文件
- 返回值:
文件列表。
- 运行
- coordinates_to_conandata(repository=False)¶
从 Git 仓库捕获 “url” 和 “commit”,调用
get_url_and_commit()
,然后将它们存储在conandata.yml
的 “scm” 键下。此信息稍后可用于克隆和检出用于创建此软件包的确切源代码点,即使配方使用exports_sources
作为嵌入源代码的机制,也可能很有用。- 参数:
repository – 默认情况下获取定义文件夹的 commit,使用 repository=True 获取仓库的 commit。
- checkout_from_conandata_coordinates()¶
从
conandata.yml
中读取 “scm” 字段,该字段必须至少包含 “url” 和 “commit”,然后执行clone(url, target=".")
,然后执行checkout(commit)
。