Git¶
Git 辅助工具是 git 命令的一个轻量级封装。它可以用于不同的目的:
在
set_version()方法中获取当前标签,并将其分配给self.version。在第三方或开源包配方(recipes)的
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 – 要从“脏”检查中排除的文件。它将与配置
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')¶
检查给定的 commit 是否存在于远程,使用
branch -r --contains <commit>并检查在该远程中是否存在一个分支。- 参数:
commit – 要检查的 commit。
remote – 远程 git 仓库的名称(默认为 'origin')。
- 返回:
如果给定的 commit 存在于远程,则为 True,否则为 False。
- is_dirty(repository=False)¶
返回当前文件夹是否为“脏”状态,运行
git status -s。Git(..., excluded=[])参数和core.scm:excluded配置将定义要跳过此检查的文件模式。- 参数:
repository – 默认检查当前文件夹是否为“脏”。如果 repository=True,它将检查根仓库文件夹而不是当前文件夹。
- 返回:
如果当前文件夹为“脏”,则为 True。否则为 False。
- get_url_and_commit(remote='origin', repository=False)¶
这是一个高级方法,它返回当前的 commit 和远程仓库的 URL。此方法旨在捕获包创建的当前远程坐标,以便以后可以从同一 commit 的源代码重新构建。这就是它的行为方式。
如果仓库是“脏”的,它将引发异常。捕获“脏”的内容的坐标没有意义,因为它将无法重现。如果存在本地更改,并且用户想要测试本地 conan create,则应首先提交更改(本地提交,不要推送更改)。
如果仓库不是“脏”的,但 commit 不存在于指定的远程,该方法将返回该 commit 和本地用户签出的 URL。这样,可以在本地进行包的 conan create,测试一切正常,然后再将一些更改推送到远程。
如果仓库不是“脏”的,并且 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 引用。
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="."),fetch <commit>,接着执行checkout(commit)。