如何使用 Microsoft.TeamFoundationServer.Client NuGet 包在 TFS 源代码管理中创建分支?
How can I use the Microsoft.TeamFoundationServer.Client NuGet package to create a branch in TFS source control?
我正在使用 Microsoft.TeamFoundationServer.Client NuGet 包来编写一些自定义脚本。我想在 VSTS 中的 TFS 源代码管理中创建分支,但我似乎无法在此包中找到任何功能来创建任何类型的分支,无论是在 TFVC 还是 Git 中。我是不是漏了什么东西或者是包裹?
假设您正在使用 .NET client libraries for Visual Studio Team Services (and TFS)。对于 TFVC,您需要使用 "Microsoft.TeamFoundation.VersionControl.Client"。以下是代码示例:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace ConsoleApplica
{
class Program
{
static void Main(string[] args)
{
string URL = "https://xxxxx.visualstudio.com/";
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(URL));
VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
string sourcepath = "$/ProjectNmae/SourceBranch";
string targetpath = "$/ProjectNmae/TargetBranch";
vcs.CreateBranch(sourcepath,targetpath,VersionSpec.Latest);
}
}
}
更新:对于 "Microsoft.TeamFoundationServer.Client",它通过 Rest API 访问 VSTS。但是TFVC的RestAPI和Git暂时只能获取分支,不能创建分支。所以你也看不到"Microsoft.TeamFoundationServer.Client"中的方法。
Integrate with Team Foundation Server 2015 and Visual Studio Team
Services from desktop-based, ASP.NET, and other Windows applications.
Provides access to version control, work item tracking, build, and
more via public REST APIs.
对于 VSTS GIT 存储库,您可以使用 Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient
和 2 个 Refs 方法创建一个分支。
如果您在 VSTS visualstudio.com 上检查浏览器开发工具中的 XHR 请求,同时创建分支,您可以看到它们是如何做到的。
- 首先他们从源分支获得最新的提交 ID
- 然后他们发送一个参考修改
首先使用 GetRefsAsync
.https://www.visualstudio.com/en-us/docs/integrate/api/git/refs#just-branches
找到您想要作为新的基础的源分支
我的示例我想从 master.
分支
(此示例是在控制台应用程序中编写的,因此如果您的代码在异步方法中,请忽略 GetAwaiter().GetResult())
var client = new GitHttpClient(uri, creds);
var masterRefs = client.GetRefsAsync(repo.Id, filter: "heads/master").GetAwaiter().GetResult();
var masterRef = masterRefs.FirstOrDefault(x => x.Name == "refs/heads/master");
这将为您提供最新的提交 ID,以作为分支的基础。
然后使用 masterRef 中的 ObjectId 作为新 Objectid 发送一个 refs 修改
https://www.visualstudio.com/en-us/docs/integrate/api/git/refs#modify-one-or-more-refs
GitRefUpdate newRef = new GitRefUpdate
{
Name = "refs/heads/your-new-branch",
NewObjectId = masterRef.ObjectId,
OldObjectId = "0000000000000000000000000000000000000000",
};
// create branch
var updateResult = client.UpdateRefsAsync(new[] { newRef }, repo.Id).GetAwaiter().GetResult();
我正在使用 Microsoft.TeamFoundationServer.Client NuGet 包来编写一些自定义脚本。我想在 VSTS 中的 TFS 源代码管理中创建分支,但我似乎无法在此包中找到任何功能来创建任何类型的分支,无论是在 TFVC 还是 Git 中。我是不是漏了什么东西或者是包裹?
假设您正在使用 .NET client libraries for Visual Studio Team Services (and TFS)。对于 TFVC,您需要使用 "Microsoft.TeamFoundation.VersionControl.Client"。以下是代码示例:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
namespace ConsoleApplica
{
class Program
{
static void Main(string[] args)
{
string URL = "https://xxxxx.visualstudio.com/";
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(URL));
VersionControlServer vcs = ttpc.GetService<VersionControlServer>();
string sourcepath = "$/ProjectNmae/SourceBranch";
string targetpath = "$/ProjectNmae/TargetBranch";
vcs.CreateBranch(sourcepath,targetpath,VersionSpec.Latest);
}
}
}
更新:对于 "Microsoft.TeamFoundationServer.Client",它通过 Rest API 访问 VSTS。但是TFVC的RestAPI和Git暂时只能获取分支,不能创建分支。所以你也看不到"Microsoft.TeamFoundationServer.Client"中的方法。
Integrate with Team Foundation Server 2015 and Visual Studio Team Services from desktop-based, ASP.NET, and other Windows applications. Provides access to version control, work item tracking, build, and more via public REST APIs.
对于 VSTS GIT 存储库,您可以使用 Microsoft.TeamFoundation.SourceControl.WebApi.GitHttpClient
和 2 个 Refs 方法创建一个分支。
如果您在 VSTS visualstudio.com 上检查浏览器开发工具中的 XHR 请求,同时创建分支,您可以看到它们是如何做到的。
- 首先他们从源分支获得最新的提交 ID
- 然后他们发送一个参考修改
首先使用 GetRefsAsync
.https://www.visualstudio.com/en-us/docs/integrate/api/git/refs#just-branches
我的示例我想从 master.
分支(此示例是在控制台应用程序中编写的,因此如果您的代码在异步方法中,请忽略 GetAwaiter().GetResult())
var client = new GitHttpClient(uri, creds);
var masterRefs = client.GetRefsAsync(repo.Id, filter: "heads/master").GetAwaiter().GetResult();
var masterRef = masterRefs.FirstOrDefault(x => x.Name == "refs/heads/master");
这将为您提供最新的提交 ID,以作为分支的基础。
然后使用 masterRef 中的 ObjectId 作为新 Objectid 发送一个 refs 修改 https://www.visualstudio.com/en-us/docs/integrate/api/git/refs#modify-one-or-more-refs
GitRefUpdate newRef = new GitRefUpdate
{
Name = "refs/heads/your-new-branch",
NewObjectId = masterRef.ObjectId,
OldObjectId = "0000000000000000000000000000000000000000",
};
// create branch
var updateResult = client.UpdateRefsAsync(new[] { newRef }, repo.Id).GetAwaiter().GetResult();