C#Azure DevOps Git:无法推动新目录[英] C# Azure DevOps Git: Unable to push new directories

本文是小编为大家收集整理的关于C#Azure DevOps Git:无法推动新目录的处理方法,想解了C#Azure DevOps Git:无法推动新目录的问题怎么解决?C#Azure DevOps Git:无法推动新目录问题的解决办法?C#Azure DevOps Git:无法推动新目录问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我正在尝试使用C#.NET的Azure DevOps Git API合作,在大多数情况下,我已经弄清楚了.但是,我在将新目录推向存储库时遇到问题.

以下是我到目前为止的相关代码片段;

创建提交

        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
                    }

                }

创建推送

        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;

当它通过该提交执行推动时,它错误地说"提供的参数无效.参数名称:newpush".

我在文档中找到任何可以帮助我弄清楚如何将"文件名"文件放置在同时创建的目录内的文件?

中时遇到了麻烦?

推荐答案

该错误似乎是由TESTFOLDER目录GitChanges中的GitChange对象引起的.

.

您需要为TESTFOLDER目录指定一个新的GitChange对象.因为@StriplingWarrior评论了git中不允许空目录.

您只能为文件指定新的GitChange对象.如果文件属于的目录不存在.它将自动创建.

请参阅下文:

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
                    }

                }

请参阅我的测试中的下面屏幕:目录是自动创建的.

在此处输入图像说明

本文地址:https://www.itbaoku.cn/post/2318626.html

问题描述

I'm trying to work with Azure DevOps Git API for C# .Net, and for the most part I've figured it out. 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".

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?

推荐答案

The error seems to be caused by the GitChange object for the TESTFOLDER directory in GitChanges.

You donot need to specify a new GitChange object for the TESTFOLDER directory. As @StriplingWarrior commented empty directory is not allowed in git.

You can just specify a new GitChange object for the file only. 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.

enter image description here

查看更多