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

时间:2017-07-28 20:30:22

标签: github git-submodules octokit octokit.net

有没有办法通过Octokit [.net]查看GitHub存储库子模块的当前目标SHA(没有在本地克隆它)?

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

之前的尝试

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

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

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

  

System.Collections.Generic.IReadOnlyList.this [int] .get名称:   mono-tools路径:path / to / submodule类型:子模块   Octokit.RepositoryContent

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

当您导航到子模块的父目录时,GitHub Web界面肯定会显示此信息,因此我认为我刚尝试了错误的方法。

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

在Octokit API中是否还有其他一些神奇的方法让我错过了这个子模块目标哈希?

1 个答案:

答案 0 :(得分:1)

TL; DR

如问题中所述,如果您获取 .gitmodules 中的子模块路径中的内容,您将获得一段“Submodule”类型的内容。这个内容为空。

但是,如果您获得同一子模块路径的父目录的内容,您将获得一个类型为“文件”的内容,其中包含子模块的路径(例如,上面的path/to/submodule) 。此文件哈希是子模块自己的存储库中的目标SHA。

详细

要获取子模块的SHA,您需要获取其父目录的内容,然后索引到表示子模块的文件。直接进入路径会获得“Submodule”类型的东西,获取父内容将获得一堆“文件”类型的东西(和“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

该哈希符合网络用户界面。

enter image description here