简体   繁体   English

C# Azure DevOps Git:无法推送新目录

[英]C# Azure DevOps Git: Unable to push new directories

I'm trying to work with Azure DevOps Git API for C#.Net, and for the most part I've figured it out.我正在尝试使用 Azure DevOps Git API for C#.Net,大部分我已经弄明白了。 However I'm having issues with pushing a new directory to the repository.但是,我在将新目录推送到存储库时遇到问题。

Below is the relevant code snippets I have so far;以下是我到目前为止的相关代码片段;

Create the Commit创建提交

        GitCommitRef commit = new GitCommitRef()
        {
            Comment = "Add a sample file",
            Changes = new GitChange[]
            {
                new GitChange()
                {
                    ChangeType = VersionControlChangeType.Add,
                    Item = new GitItem() { Path = "/TESTFOLDER", GitObjectType = GitObjectType.Tree, IsFolder = true },
                    NewContent = null
                    //NewContent = new ItemContent()
                    //{
                    //    Content = Utilities.ReadFile(fileNamePath),
                    //    ContentType = ItemContentType.RawText
                    //}
                 },

                new GitChange()
                {
                    ChangeType = VersionControlChangeType.Add,
                    Item = new GitItem() {Path = "/TESTFOLDER/" + fileName, GitObjectType = GitObjectType.Blob, IsFolder = false },
                    NewContent = new ItemContent()
                    {
                        Content = Utilities.ReadFile(fileNamePath),
                        ContentType = ItemContentType.RawText
                    }

                }

Create the Push创建推送

        GitPush toPush = new GitPush()
        {
            RefUpdates = new GitRefUpdate[] { newBranch },
            Commits = new GitCommitRef[] { commit }
        };

        // Create the push with the new branch and commit
        GitPush push = gitClient.CreatePushAsync(toPush, repo.Id).Result;

WHen it executes the Push with that commit, it errors saying "The parameters supplied are not valid. Parameter name: newPush".当它使用该提交执行 Push 时,它会错误提示“提供的参数无效。参数名称:newPush”。

I have had trouble finding anything in the documentation that could help me figure out how to place the "fileName" file, inside a directory created at the same time?我在文档中找不到任何可以帮助我弄清楚如何将“fileName”文件放置在同时创建的目录中的内容?

The error seems to be caused by the GitChange object for the TESTFOLDER directory in GitChanges .该错误似乎是由GitChangeTESTFOLDER目录的GitChanges object 引起的。

You donot need to specify a new GitChange object for the TESTFOLDER directory.您不需要为TESTFOLDER目录指定新的GitChange object。 As @StriplingWarrior commented empty directory is not allowed in git.正如@StriplingWarrior 评论的那样,git 中不允许使用空目录。

You can just specify a new GitChange object for the file only.您可以只为该文件指定一个新的GitChange object。 If the directory which the file resides doesnot exist.如果文件所在的目录不存在。 It will be automatically created.它将自动创建。

See below:见下文:

GitCommitRef commit = new GitCommitRef()
        {
            Comment = "Add a sample file",
            Changes = new GitChange[]
            {
                new GitChange()
                {
                    ChangeType = VersionControlChangeType.Add,
                    Item = new GitItem() {Path = "/TESTFOLDER/" + fileName, GitObjectType = GitObjectType.Blob, IsFolder = false },
                    NewContent = new ItemContent()
                    {
                        Content = Utilities.ReadFile(fileNamePath),
                        ContentType = ItemContentType.RawText
                    }

                }

See below screen from my test: The directory was automatically created.从我的测试中看到下面的屏幕:目录是自动创建的。

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# Azure Devops API Git:如何使用 C# API 推送文件 - C# Azure Devops API Git: How to push files using the C# API 无法从 C# 连接到 Azure DevOps Repo - Unable to connect to Azure DevOps Repo from C# 需要在 Azure DevOps 存储库中使用 C# 创建一个文件夹(以及其中的一个文件) - 无论是 Git 还是 TFVC - Need to create a folder(and a file inside it) using C# inside Azure DevOps repository - be it Git or TFVC Azure Devops REST API 与 ZD7EFA19FBE7D3972FD5ADB6024223D7 - Azure Devops REST API with C# Authentication 如何使用 .NET 数据工厂 ZF20E3C5E54C0893D36 启用 DeVOPS GIT 存储库在 Azure 数据工厂中发布管道? - How to publish pipeline in Azure Data Factory enabled with DeVOPS GIT repo using .NET Data Factory SDK (C# )? 验证到 Azure Devops Git repo - Authenticating to Azure Devops Git repo 如何通过 C# 客户端在 Azure DevOps 中创建代理池? - How to create Agent Pool in Azure DevOps via C# client? 使用c#在Azure DevOps(VSTS)中使用子节点创建迭代 - create iteration with child nodes in Azure DevOps (VSTS) using c# 如何使用 c# API 授权 Azure Devops? - How to Authorize Azure Devops using c# API? 如何使用 C# 为 azure devops 变量赋值 - How to assign value to azure devops variable using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM