【发布时间】:2018-07-13 13:04:27
【问题描述】:
如何使用 VSTS Git API 在新创建的存储库中创建初始推送?
我创建了一个新的存储库。
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.Core.WebApi
var accountUri = new Uri("https://mysite.visualstudio.com");
var personalAccessToken = "myaccesstoken";
var connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));
// Get a GitHttpClient to talk to the Git endpoints
var gitClient = connection.GetClient<GitHttpClient>();
var teamProject = projectClient.GetProject("MyProject", true, true).Result;
var repo = gitClient.CreateRepositoryAsync(new GitRepository
{
DefaultBranch = "refs/heads/master",
Name = "TestRepo",
ProjectReference = new TeamProjectReference
{
Id = teamProject.Id
}
}).Result;
repo 已成功创建。但是为什么repo.DefaultBranch的值为null呢?
下一步,我想推送我的初始提交。
var newBranch = new GitRefUpdate
{
RepositoryId = repo.Id,
Name = $"refs/heads/master"
};
string newFileName = "README.md";
GitCommitRef newCommit = new GitCommitRef
{
Comment = "Initial commit",
Changes = new GitChange[]
{
new GitChange
{
ChangeType = VersionControlChangeType.Add,
Item = new GitItem { Path = $"/master/{newFileName}" },
NewContent = new ItemContent
{
Content = "# Thank you for using VSTS!",
ContentType = ItemContentType.RawText,
},
}
}
};
GitPush push = gitClient.CreatePushAsync(new GitPush
{
RefUpdates = new GitRefUpdate[] { newBranch },
Commits = new GitCommitRef[] { newCommit },
}, repo.Id).Result;
调用CreatePushAsync时出错:
VssServiceException:参数组合要么无效 或不完整。参数名称:baseCommitId
请帮助如何正确创建初始提交。
【问题讨论】:
标签: c# git azure-devops