【问题标题】:Create Initial Push into Newly Created Repository using VSTS Git API使用 VSTS Git API 创建初始推送到新创建的存储库
【发布时间】: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


    【解决方案1】:

    您可以使用其余的 api 来实现您想要的。 creating an initial commit (create a new branch)的其余api如下:

    POST https://fabrikam.visualstudio.com/_apis/git/repositories/{repositoryId}/pushes?api-version=4.1
    
    {
      "refUpdates": [
        {
          "name": "refs/heads/master",
          "oldObjectId": "0000000000000000000000000000000000000000"
        }
      ],
      "commits": [
        {
          "comment": "Initial commit.",
          "changes": [
            {
              "changeType": "add",
              "item": {
                "path": "/readme.md"
              },
              "newContent": {
                "content": "My first file!",
                "contentType": "rawtext"
              }
            }
          ]
        }
      ]
    }
    

    【讨论】:

    • 谢谢。我错过了将OldObjectId 初始化为0000000000000000000000000000000000000000。
    • 这不能回答问题.... 考虑是否创建了一个新的 repo(空白)。也许它使用“master”,也许是“main”,也许是“trunk”......我们如何确定,因为属性是空白的?!?!?!?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-05
    • 2020-07-08
    • 2016-08-12
    • 2019-09-15
    • 2018-11-13
    • 2017-01-10
    • 2012-02-16
    相关资源
    最近更新 更多