[英]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
.该错误似乎是由
GitChange
中TESTFOLDER
目录的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.