我已经创建了一个存储库,并且使用 octokit.net,我想使用"完整提交"方法创建一个新提交。按照本教程,我已成功创建了一个"一个文件/一行"提交。 但是"完整提交"部分不起作用。我使用的是Octokit版本0.24.1-alpha0001。
<PackageReference Include="Octokit" Version="0.24.1-alpha0001"/>
我的代码在这里:
var Client = new GitHubClient(new ProductHeaderValue("my-cool-app"));
var Owner = "Owner";
var RepositoryName = "RepositoryName";
var Password = "Password";
Client.Credentials = new Credentials(Owner, Password);
之后,我用这个创建了一个存储库。
var context = Client.Repository.Create(repository);
这是我的"完整提交"部分:
using Octokit;
try {
// 1. Get the SHA of the latest commit of the master branch.
var headMasterRef = "heads/master";
var masterReference = Client.Git.Reference.Get(Owner, RepositoryName, headMasterRef).Result; // Get reference of master branch
var latestCommit = Client.Git.Commit.Get(Owner, RepositoryName,
masterReference.Object.Sha).Result; // Get the laster commit of this branch
var nt = new NewTree { BaseTree = latestCommit.Tree.Sha };
//2. Create the blob(s) corresponding to your file(s)
var textBlob = new NewBlob { Encoding = EncodingType.Utf8, Content = "Hellow World!" };
var textBlobRef = Client.Git.Blob.Create(Owner, RepositoryName, textBlob);
// 3. Create a new tree with:
nt.Tree.Add(new NewTreeItem { Path = fileRel.RelativePath, Mode = "100644", Type = TreeType.Blob, Sha = textBlobRef.Result.Sha });
var newTree = Client.Git.Tree.Create(Owner, RepositoryName, nt).Result;
// 4. Create the commit with the SHAs of the tree and the reference of master branch
// Create Commit
var newCommit = new NewCommit("Commit test with several files", newTree.Sha, masterReference.Object.Sha);
var commit = Client.Git.Commit.Create(Owner, RepositoryName, newCommit).Result;
// 5. Update the reference of master branch with the SHA of the commit
// Update HEAD with the commit
Client.Git.Reference.Update(Owner, RepositoryName, headMasterRef, new ReferenceUpdate(commit.Sha));
} catch (AggregateException e) {
Console.WriteLine($"An exception is detected in the commit step. {e.Message}");
}
没有例外,一切似乎都已定义,但是当我转到存储库的主分支时,没有完成任何提交。只有初始提交。
我终于找到了问题所在。我错过了.Client.Git.Reference.Update 行的结果。这很好用。谢谢热雷米·贝特朗。
我更新了
// 5. Update the reference of master branch with the SHA of the commit
// Update HEAD with the commit
Client.Git.Reference.Update(Owner, RepositoryName, headMasterRef, new
ReferenceUpdate(commit.Sha));
到此代码
Client.Git.Reference.Update(Owner, RepositoryName, headMasterRef, new
ReferenceUpdate(commit.Sha)).Result;
我在 git 对象(newTree,commit(上发现了一个断点:url 在这里。本教程是正确的,因为它使用await:
await github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha));