I have created a repository and with octokit.net and I want to create a new commit with the 'full commit' method. I have successfully created a 'one file / one line' commit following this tutorial. But the 'full commit' section is not working. I'm using Octokit version 0.24.1-alpha0001.
<PackageReference Include="Octokit" Version="0.24.1-alpha0001"/>
My code is here :
var Client = new GitHubClient(new ProductHeaderValue("my-cool-app"));
var Owner = "Owner";
var RepositoryName = "RepositoryName";
var Password = "Password";
Client.Credentials = new Credentials(Owner, Password);
After, I create a repository with this.
var context = Client.Repository.Create(repository);
And here is my 'full commit' section :
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}");
}
There is no exception, everything seems defined but when I go to the master branch of my repository, no commit is done. There is just the initial commit.
I finally found the problem. I was missing the .Result at the Client.Git.Reference.Update line. This works nice. Thanks Jérémie Bertrand.
I updated
to this code
I discovered it with a breakpoint on the git object (newTree, commit) : urls were here. The tutorial is correct because it uses await :