gpt4 book ai didi

java - TFS JAVA SDK14.0.3 - 如何从服务器获取远程文件并进行更改并再次 checkin

转载 作者:行者123 更新时间:2023-12-02 11:20:20 24 4
gpt4 key购买 nike

我在 Team Foundation 服务器中有一个名为 master-builds 的项目。 master-build 文件夹有一个名为 build-main.xml 的 xml 文件我想从服务器获取此文件并对其进行一些更改(更改版本号)并再次 checkin ,如何使用 TFS JAVA SDK 14.0.3 实现它。

我尝试了文件夹中提供的示例,但无法实现。

这是创建工作区的代码片段

public static Workspace createAndMapWorkspace(final TFSTeamProjectCollection tpc) {
final String workspaceName = "SampleVCWorkspace" + System.currentTimeMillis(); //$NON-NLS-1$
Workspace workspace = null;

// Get the workspace
workspace = tpc.getVersionControlClient().tryGetWorkspace(ConsoleSettings.MAPPING_LOCAL_PATH);

// Create and map the workspace if it does not exist
if (workspace == null) {
workspace = tpc.getVersionControlClient().createWorkspace(
null,
workspaceName,
"Sample workspace comment", //$NON-NLS-1$
WorkspaceLocation.SERVER,
null,
WorkspacePermissionProfile.getPrivateProfile());

// Map the workspace
final WorkingFolder workingFolder = new WorkingFolder(
ConsoleSettings.MAPPING_SERVER_PATH,
LocalPath.canonicalize(ConsoleSettings.MAPPING_LOCAL_PATH));
workspace.createWorkingFolder(workingFolder);
}

System.out.println("Workspace '" + workspaceName + "' now exists and is mapped"); //$NON-NLS-1$ //$NON-NLS-2$

return workspace;
}

这是添加/编辑文件的代码段

public static void getLatest(final Workspace workspace) {
final ItemSpec spec = new ItemSpec(ConsoleSettings.MAPPING_LOCAL_PATH, RecursionType.FULL);
final GetRequest request = new GetRequest(spec, LatestVersionSpec.INSTANCE);
workspace.get(request, GetOptions.NONE);
}

public static File addFile(final Workspace workspace) {

// Create the file locally
final File newFile = new File(ConsoleSettings.MAPPING_LOCAL_PATH, "SampleAppFile"); //$NON-NLS-1$
writeFileContents(newFile, "", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

// Pend an add
// The encoding is passed as null and the add will detect the encoding
// of the file
workspace.pendAdd(new String[] {
newFile.getAbsolutePath()
}, false, ENCODING, LockLevel.UNCHANGED, GetOptions.NONE, PendChangesOptions.NONE);

// Checkin the pending change
final int cs = checkinPendingChanges(workspace, "Adding a sample file"); //$NON-NLS-1$

System.out.println("Added file " + newFile.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$

return newFile;
}

public static void editFile(final Workspace workspace, final File file) {
// Pend edit
final ItemSpec fileSpec = new ItemSpec(file.getAbsolutePath(), RecursionType.NONE);
workspace.pendEdit(
new ItemSpec[] {
fileSpec
},
LockLevel.UNCHANGED,
ENCODING,

GetOptions.NONE,
PendChangesOptions.NONE);

// Edit the file
writeFileContents(file, "Edited this file at " + new Date().toString(), "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$

// Checkin the pending change
final int cs = checkinPendingChanges(workspace, "Editing the sample file"); //$NON-NLS-1$

System.out.println("Edited file " + file.getAbsolutePath() + " in CS# " + cs); //$NON-NLS-1$ //$NON-NLS-2$
}

这些是我的问题

我能够成功地将新文件添加到远程服务器,但无法编辑现有的远程文件。

如果我调用 editFile,然后调用 addFile 方法,它将得到更新,但我不想每次都添加 enw 文件,我想要的是从服务器获取最新版本的 buils-main.xml 文件并进行更改,办理入住手续。

此外,在本地路径中创建工作区后,我在该路径中看不到它,这是正确的行为吗?

有人可以帮我解决这个问题吗?

最佳答案

当您想要编辑受TFS版本控制的文件时,您需要在工作区中获取该文件并在本地进行编辑,然后将修改后的文件 checkin 到TFS。我对 Java 代码不熟悉,我会添加 C# 代码供您引用。

  1. 为您要编辑的文件夹创建工作区:

-

using Microsoft.TeamFoundation.Client;
using System;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace TestCaseProject
{
class Program
{
static void Main(string[] args)
{
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(new Uri("http://tfs:8080/tfs/DefaultCollection"));
var versioncontrols = tfs.GetService<VersionControlServer>();

var workspace = versioncontrols.CreateWorkspace("test", "Cece Dong");

String ServerFolder = @"$/ScrumProject/TestCaseProject";
String LocalFolder = @"E:\test";

WorkingFolder workfolder = new WorkingFolder(ServerFolder, LocalFolder);
workspace.CreateMapping(workfolder);

workspace.Get();


}
}
}
  • 使用Workspace.PendEdit Method编辑从 TFS 到工作区的文件。

  • 使用Workspace.CheckIn Method检查待处理的更改。

  • 查看下面的博客了解更多详细信息:

    https://blogs.msdn.microsoft.com/buckh/2012/03/10/team-foundation-version-control-client-api-example-for-tfs-2010-and-newer/

    或者您可以引用此案例使用TFS Rest api更新文件:

    Updating a file using REST Api Visual Studio Team Services

    关于java - TFS JAVA SDK14.0.3 - 如何从服务器获取远程文件并进行更改并再次 checkin ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49966629/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com