github - 通过 Octokit.net 获取 Git repo 子模块的目标哈希/SHA?

标签 github git-submodules octokit octokit.net

有什么方法可以通过 Octokit[.net] 查看 GitHub 存储库子模块的当前目标 SHA(无需在本地克隆)?

我已经能够通过从“父”存储库中检索 .gitmodules 文件来追踪所有子模块,但是该文件不维护子模块在该子模块中指向的位置 repo 。

之前的尝试

在发现有人通过使用 LibGit2Sharp 通过子模块路径索引提交来获取此信息后,我在 Octokit 中进行了类似的尝试。

var submodulePathContents = await repositoriesClient.Content.GetAllContents(parentRepoId, "path/to/submodule");

在调试器中单步执行这段代码时,我在 Locals/Autos 中看到了这一点,所以它肯定知道我将它指向一个子模块路径。

System.Collections.Generic.IReadOnlyList.this[int].get Name: mono-tools Path: path/to/submodule Type:Submodule Octokit.RepositoryContent

不幸的是,当我从该列表中获取实际内容时,submodulePathContents[0].Content 只是 null

当您导航到子模块的父目录时,GitHub 网络界面肯定会显示此信息,所以这让我觉得我刚刚尝试了错误的方法。

GitHub showing a submodule's target hash in the web interface.

我错过了 Octokit API 中获取此子模块目标哈希的其他神奇方法吗?

最佳答案

长话短说

如题中所述,如果获取.gitmodules中子模块路径下的内容,会得到一段“Submodule”类型的内容。这有空内容。

但是,如果您获得同一子模块路径的父目录的内容,您将获得一段"file"类型的内容,以及您的子模块的路径(例如,path/to/上面的子模块)。此 files 哈希是子模块自己的存储库中的目标 SHA。

详情

要获取子模块的 SHA,需要获取其父目录的内容,然后索引到代表子模块的文件。虽然直接转到路径会得到一些“子模块”类型的东西,但获取父内容会得到一堆"file"类型的东西(如果那里有同级子目录,还有“Dir”)。奇怪的是,这个父内容列表不包括您直接指向子模块路径时获得的“子模块”类型。

在上面的示例中,只需在路径中向上一级。

var submodulePathContents = await repositoriesClient.Content.GetAllContents(parentRepoId, "path/to"); // no longer "path/to/submodule"

从那里,首先获取包含您想要的路径的内容项(例如,“path/to/submodule”),它将有一个包含子模块目标 SHA 的 Sha 字段子模块存储库。

using System.Linq;
…
var submoduleTargetSha = submodulePathContents.First(content => content.Path == submodulePath).Sha;

这是来自 monodevelop repo's primary submodule directory 的示例条目.

Name: mono-tools Path: main/external/mono-tools Type:File   Octokit.RepositoryContent
Content null    string
DownloadUrl null    System.Uri
EncodedContent  null    string
Encoding    null    string
GitUrl  {https://api.github.com/repos/mono/mono-tools/git/trees/d858f5f27fa8b10d734ccce7ffba631b995093e5}   System.Uri
HtmlUrl {https://github.com/mono/mono-tools/tree/d858f5f27fa8b10d734ccce7ffba631b995093e5}  System.Uri
Name    "mono-tools"    string
Path    "main/external/mono-tools"  string
Sha "d858f5f27fa8b10d734ccce7ffba631b995093e5"  string
Size    0   int
SubmoduleGitUrl null    System.Uri
Target  null    string
Type    File    Octokit.ContentType
Url {https://api.github.com/repos/mono/monodevelop/contents/main/external/mono-tools?ref=master}    System.Uri

并且该哈希值与 Web UI 对齐。

enter image description here

关于github - 通过 Octokit.net 获取 Git repo 子模块的目标哈希/SHA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45381937/

相关文章:

java - 在 Java 中使用 API 进行翻新

git - `git submodule update --remote` vs `git pull --recurse-submodule` vs `git submodule foreach git pull origin main`

ios - 尝试安装 Octokit.ObjC 时出现几个错误

ruby - 如何使用 octokit.rb 获取提交日期

eclipse - 如何从 Eclipse 将标签推送到 GitHub?

git - 我无法删除 git 上的远程主分支

ruby - 从 github 源安装的 Gem 未在 `gem list` 中显示

git - cloudbuild.yaml 包含不同的云构建器配置

git - 如何在github上上传本地子模块的文件?