octokit.net:创建一个新的提交

时间:2017-07-23 09:56:22

标签: c# git octokit.net

我已经使用octokit.net创建了一个存储库,我希望使用'完全提交'方法创建新提交。我已在this tutorial之后成功创建了“一个文件/一行”提交。 但是“完全提交”部分无效。我使用的是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}");
}

没有异常,一切似乎已定义但是当我转到我的存储库的主分支时,没有提交。只有初始提交。

1 个答案:

答案 0 :(得分:1)

我终于找到了问题。我错过了Client.Git.Reference.Update行中的 .Result 。这很好用。谢谢JérémieBertrand

我更新了

// 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 github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha));