1

我已经使用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}");
}

也不例外,一切似乎都已定义,但是当我转到存储库的主分支时,没有完成任何提交。只有初始提交。

4

1 回答 1

1

我终于找到了问题所在。我在 Client.Git.Reference.Update 行缺少.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));

到这个代码

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));
于 2017-08-12T18:14:36.227 回答