Visual Studio 的源代码控制集成如何与 Perforce 配合使用? [英] How does Visual Studio's source control integration work with Perforce?

查看:42
本文介绍了Visual Studio 的源代码控制集成如何与 Perforce 配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Perforce 和 Visual Studio.每当我们创建一个分支时,除非我们使用从源代码管理打开",否则某些项目将不会被绑定到源代码管理,但其他项目无论如何都可以工作.通过我的调查,我知道其中涉及的一些事情:

We're using Perforce and Visual Studio. Whenever we create a branch, some projects will not be bound to source control unless we use "Open from Source Control", but other projects work regardless. From my investigations, I know some of the things involved:

在我们的 .csproj 文件中,有以下设置:

In our .csproj files, there are these settings:

有时它们都设置为SAK",有时则不是.如果这些都说SAK",事情似乎更有可能奏效.

Sometimes they are all set to "SAK", sometimes not. It seems things are more likely to work if these say "SAK".

在我们的 .sln 文件中,有许多项目的设置:

In our .sln file, there are settings for many of the projects:

  • SccLocalPath#
  • SccProjectFilePathRelativizedFromConnection#
  • SccProjectUniqueName#

(# 是标识每个项目的数字.) SccLocalPath 是相对于解决方案文件的路径.经常是.",有时是项目所在的文件夹,有时是.."或....",指向上面的文件夹好像不好解决方案文件夹.相对化的路径是该文件夹到项目文件的路径.如果 SccLocalPath 指向项目的文件夹,它将完全丢失.如果 SccLocalPath 中包含..",则此路径可能包含分支之间不相同的文件夹名称,我认为这会导致问题.

(The # is a number that identifies each project.) SccLocalPath is a path relative to the solution file. Often it is ".", sometimes it is the folder that the project is in, and sometimes it is ".." or "....", and it seems to be bad for it to point to a folder above the solution folder. The relativized one is a path from that folder to the project file. It will be missing entirely if SccLocalPath points to the project's folder. If the SccLocalPath has ".." in it, this path might include folder names that are not the same between branches, which I think causes problems.

所以,为了最终了解我想知道的细节:

So, to finally get to the specifics I'd like to know:

  • 当您执行更改源代码管理"并绑定项目时会发生什么?Visual Studio 如何决定在项目和解决方案文件中放入什么内容?
  • 当您执行从源代码管理打开"时会发生什么?
  • SccLocalPath 和 SccProjectFilePathRelativizedFromConnection 所指的连接"文件夹是什么?Visual Studio/Perforce 如何选择它?
  • 是否有一些推荐的方法可以让源代码控制绑定在您创建解决方案的新分支时继续工作?

2012 年 6 月添加:我不再使用 Perforce,所以我不能保证它,但看看下面的 KCD 的回答.显然有 一个新的 P4 VS 插件 正在开发中.希望它能解决所有这些烂摊子!

Added June 2012: I don't use Perforce any more, so I can't vouch for it, but have a look at KCD's answer below. Apparently there's a new P4 VS plugin under development. Hopefully it should clear up all this mess!

推荐答案

简介

我不同意 Visual Studio 中的 Perforce 集成可怕"的说法.相反,我将其定义为开箱即用的体验不是最佳的":-).以下部分讨论了我对集成的理解以及对项目/解决方案设置的建议.

Introduction

I would disagree with the claim that Perforce integration in Visual Studio is "terrible". Rather, I'd define it as "out of the box experience is less than optimal" :-). The following sections discuss my understanding of the integration and recommendations for project/solution setup.

如果您对源代码控制集成的工作原理不感兴趣,可以跳到本答案的末尾,我总结了 Weeble 问题的答案.

If you're not interested in the details of how the source control integration works you can skip to the end of this answer where I summarize answers to Weeble's question.

免责声明:以下部分只是基于我的经验经验的有根据的猜测,但是我已经在许多项目中使用该技术多年(每个项目都有多个实验/主干/维护/发布分支,有时甚至是多个解决方案文件,没有问题).缺点是您必须手动更新项目文件 - 但 2 分钟的投资在项目的整个生命周期内摊销非常好,恕我直言:-).

Disclaimer: The following sections are just educated guesses based on my empirical experience, however I've used the technique over many years in many projects (each project having multiple experimental/trunk/maintenance/release branches, sometimes even multiple solution files, without issues). The disadvantage is that you have to manually update the project files - but the 2 minute investment is amortized over the lifetime of a project pretty nicely IMHO :-).

Visual Studio 在初始解决方案加载期间使用来自解决方案文件和每个项目文件的源代码管理绑定信息.然后将此绑定信息存储在 name.suo 文件中(假设我们使用 name.sln 作为解决方案) - 请注意 suo 文件是标有隐藏标志,因此它们在文件资源管理器中不可见(除非您覆盖隐藏文件和文件夹"选项).

Visual Studio uses source control binding information from both solution file and each project file during the initial solution loading. This binding information is then stored in name.suo file (assuming we're using name.sln as solution) - note that suo files are marked with hidden flag so they won't be visible in file explorer (unless you override the "Hidden files and folders" option).

如果出现任何问题,重新绑定到源代码管理提供程序的最简单方法是删除相应的 suo 文件并重新打开解决方案.创建 suo 文件后,将更改为 元素无效.

The easiest way to re-bind to source control provider if anything goes wrong is to delete the appropriate suo file and reopen solution. After suo file has been created, changes to <Scc*> elements have no effect.

如果在最初的解决方案打开过程中,解决方案文件中存储的绑定信息与项目文件中存储的信息存在差异,Visual Studio 将尝试修复此问题(有时它甚至会提示您决定是否选择在解决方案文件中存储的信息)解决方案或项目中的信息应作为解决差异的大师"):

If during the initial solution opening there is a discrepancy between the binding information stored in solution file and information stored in project file, Visual Studio will attempt to fix this (sometimes it will even prompt for your decision to choose whether the information in solution or the information in project should be used as a "master" to resolve the discrepancy):

为什么 Visual Studio 违反了 DRY(不要重复自己)原则?我不知道.我认为这有历史原因,并且与称为 Visual Source Safe 的噩梦的需求紧密相关 :-).

Why is Visual Studio violating DRY (Don't Repeat Yourself) principle? I have no idea. I presume this has historic reasons and is tightly coupled to the needs of that nightmare called Visual Source Safe :-).

在向 Perforce 添加新的或现有的解决方案/项目时,我总是首先创建一个空白解决方案(请参阅源代码控制空白解决方案"部分).然后我将项目一个接一个地添加到这个空白解决方案中.根据要添加的项目是否已经存在(请参阅源代码控制现有(未绑定)项目"和源代码控制现有(绑定)项目"部分)或我需要创建一个新项目(请参阅源代码控制新项目"部分).

When adding either new or existing solutions/projects to Perforce, I always start by creating a blank solution (see the "Source-controlling a blank solution" section). I then add projects to this blank solution, one after another. The steps differ slightly based on whether the project being added already exists (see the "Source-controlling existing (unbound) projects" and "Source-controlling existing (bound) projects" sections) or I need to create a new one (see the "Source-controlling new projects" section).

要将新的空白解决方案添加到源代码管理,请执行以下操作:

To add a new blank solution to source control, do the following:

  1. 启动Visual Studio,新建"->项目..."->其他项目类型"->空白解决方案";填写解决方案名称和位置,确定"按钮
  2. 文件"->源代码管理"->将解决方案添加到源代码管理..."
  3. 在连接对话框中输入适当的 P4 服务器端口、客户端和用户(注意所选客户端的视图必须包含您在步骤 1 中选择的位置)
  4. 查看"->待定签入"->签入"-> 在提交对话框中而不是点击提交"按钮,使用取消".
    原因:签入"操作将创建一个新文件name.vssscc",然后将name.sln"和name.vssscc"添加到Perforce的默认更改列表中;通过取消提交对话框,我们将保持添加"操作挂起,并且能够在提交到 P4 之前编辑文件
  5. 关闭 Visual Studio
  6. 在您最喜欢的编辑器(记事本,如果您真的很绝望:-))中打开 name.sln 文件并添加两行新行(SccProjectName0SccProvider0strong>) - 空白解决方案文件现在应该有一个源代码控制部分,如下所示:

  1. Start Visual Studio, "New" -> "Project..." -> "Other project types" -> "Blank solution"; fill in solution name and location, "OK" button
  2. "File" -> "Source Control" -> "Add Solution to Source Control..."
  3. In the connection dialog enter appropriate P4 server port, client and user (note that view of the selected client must include the location that you picked in step 1)
  4. "View" -> "Pending Checkins" -> "Check In" -> in the submit dialog instead of hitting the "Submit" button, use "Cancel".
    Reason: The "Check In" action will create a new file, "name.vssscc", then add both "name.sln" and "name.vssscc" to Perforce's default changelist; by cancelling the submit dialog we will keep the "add" operation pending and will be able to edit the files before submitting to P4
  5. Close Visual Studio
  6. Open the name.sln file in your favourite editor (notepad, if you're really desperate :-) ) and add two new lines (SccProjectName0 and SccProvider0) - the blank solution file should now have a source control section as follows:

GlobalSection(SourceCodeControl) = preSolution
    SccNumberOfProjects = 1
    SccLocalPath0 = .
    SccProjectName0 = Tutorial
    SccProvider0 = MSSCCI:Perforceu0020SCM
EndGlobalSection

应选择如下值:

  • SccProjectName0:将在更改源代码管理"对话框中显示为服务器绑定"的任意字符串.此名称用于确定哪些项目/解决方案文件可以共享相同的源代码控制连接.我建议不要为此名称使用空格,因为解决方案和项目文件中的空格转义规则不同.
  • SccProvider0:硬编码值MSSCCI:Perforceu0020SCM".
  • SccProjectName0: an arbitrary string that will be displayed in "Change Source Control" dialog as "Server Binding". This name is used to determine what projects/solution files can share the same source control connection. I recommend not using space for this name as escaping rules for spaces are different in solution and project files.
  • SccProvider0: hard-coded value "MSSCCI:Perforceu0020SCM".

您现在可以测试绑定:

  1. 确保 Visual Studio 已关闭
  2. 删除除name.sln(尤其是name.suo)之外的**所有*文件
  3. 打开 Visual Studio 并使用它打开 name.sln
  4. 应该会出现一个连接对话框,使用适当的端口/客户端/用户,然后单击确定
  5. 解决方案资源管理器现在应显示带有挂锁覆盖图标的解决方案节点:
  6. 您现在可以通过使用文件"->源代码控制"->更改源代码控制..."来验证解决方案的源代码控制状态:注意:服务器绑定"列显示了我们为SccProjectName0"选择的值.

对新项目进行源代码控制

如果您正在创建一个全新的项目并希望立即开始在 Perforce depot 中对其进行跟踪,请按照以下步骤操作:

Source-controlling new projects

If you're creating a brand-new project and would like to immediately start tracking it in a Perforce depot, follow these steps:

  1. 在 Visual Studio 中打开源代码控制的解决方案
  2. 文件"->添加"->新建项目..."-选择您要添加的项目类型、名称和位置(位置应该是存储解决方案文件的目录的子目录)
  3. 文件"->全部保存"(这会将所有内存中的更改提交到解决方案文件和新创建的项目文件到磁盘)
  4. 使用您选择的编辑器手动编辑您刚刚创建的项目文件(来吧,再次记事本?;-)).将以下属性元素添加到 PropertyGroup(任何属性组)中:

  1. Open the source-controlled solution in Visual Studio
  2. "File" -> "Add" -> "New Project..." - pick the project type you're adding, name and location (location should be a subdirectory of the directory where the solution file is stored)
  3. "File" -> "Save All" (this will commit all in-memory changes to solution file and the newly created project file to the disk)
  4. Manually edit the project file you just created using an editor of your choice (come on, notepad AGAIN? ;-) ). Add the following property elements into a PropertyGroup (any property group):

<PropertyGroup>
    ...
    <SccProjectName>Tutorial</SccProjectName>
    <SccLocalPath>....</SccLocalPath>
    <SccProvider>MSSCCI:Perforce SCM</SccProvider>
    ...
</PropertyGroup>

应选择如下值:

  • SccProjectName - 这是在更改源代码管理"对话框中显示为服务器绑定"的值;应该与您在空白解决方案中用于 SccProjectName0 的值相同;如果不相同,解决方案和此项目将无法共享相同的源代码控制提供程序连接
  • SccLocalPath - 参考目录的相对路径(在更改源代码管理"对话框中显示为本地绑定");因为我建议使用解决方案目录作为参考目录,这实际上是从包含项目文件的目录到包含解决方案文件的目录的相对路径(我的示例是将项目存储在(solutionDir)/Source/ProjectName/projectName.csproj"中,因此相对路径是向上两级")
  • SccProvider - 硬编码值MSSCCI:Perforce SCM";这用于确定哪些 SCCAPI 提供程序是有效的 Scc* 绑定值
  • SccProjectName - this is the value that is displayed in "Change Source Control" dialog as "Server Binding"; should be the same as the value you used for SccProjectName0 in blank solution; if not the same, solution and this project won't be able to share the same source control provider connection
  • SccLocalPath - relative path to the reference directory (displayed in "Change Source Control" dialog as "Local binding"); because I recommend using the solution directory as the reference directory, this is in effect relative path from directory containing project file to directory containing solution file (my example is storing projects in "(solutionDir)/Source/ProjectName/projectName.csproj", thus the relative path is "two levels up")
  • SccProvider - hard-coded value "MSSCCI:Perforce SCM"; this is used to determine what SCCAPI provider are the Scc* binding values valid for

切换回 Visual Studio;它应该自动检测到项目文件已在外部更新并提供重新加载它(如果没有,请手动卸载并重新加载项目)

Switch back to Visual Studio; it should automatically detect that the project file has been updated externally and offer to reload it (if not, unload and reload the project manually)

验证新添加的项目是否绑定正确,可以按照以下步骤操作:

To verify that the newly added project is bound properly, you can follow these steps:

  1. 确保 Visual Studio 已关闭
  2. 删除 (solutionName).suo 文件以及 MSSCCPRJ.SCC(在解决方案目录中)
  3. 打开 Visual Studio 并使用它打开 (solutionName).sln
  4. 应该会出现一个连接对话框,使用适当的端口/客户端/用户,然后单击确定
  5. 解决方案资源管理器现在应该显示带有挂锁覆盖图标的项目节点:
  6. 您现在可以使用文件"->源代码管理"->更改源代码管理..."来验证解决方案的源代码管理状态:

  1. Make sure Visual Studio is closed
  2. Delete (solutionName).suo file as well as MSSCCPRJ.SCC (in solution directory)
  3. Open Visual Studio and use it to open (solutionName).sln
  4. A connection dialog should appear, use appropriate port/client/user and click OK
  5. Solution explorer should now display the project node with a padlock overlay icon:
  6. You can now verify source control status of the solution by using "File" -> "Source Control" -> "Change Source Control...":

关于这个状态截图需要注意的一点是,当我选择解决方案行时,所有剩余的行也被选中"(蓝色突出显示).这是因为所有这些条目都具有相同的服务器绑定"+本地绑定",因此共享相同的源代码控制提供程序 (P4) 连接.

One thing to note about this status screenshot is that when I selected the solution row, all the remaining rows were "selected" as well (blue highlight). This is because all those entries have the same "Server Binding" + "Local Binding" and thus share the same source-control-provider (P4) connection.

另请注意,两个项目的相对路径"有两个级别,并且相对于同一个本地绑定"——解决方案文件所在的目录.

Also note that "Relative Path" for both projects has two levels, and are relative to the same "Local Binding" - the directory where solution file resides.

对现有(未绑定)项目进行源代码控制

如果您有尚未在任何其他 Perforce 解决方案中使用的现有项目,请按照以下步骤将它们添加到 Perforce(即导入之前未进行源代码控制(互联网下载等)或正在使用不同的源代码管理提供程序(Visual Source Safe 等).

Source-controlling existing (unbound) projects

If you have existing projects that have not yet been used in any other Perforce solution, follow these steps to add them to Perforce (i.e. importing projects that have not been source-controlled before (Internet downloads etc.) or were using a different source control provider (Visual Source Safe, etc.).

  1. 将项目复制到合适的位置
  2. 清理现有的源代码控制绑定(如果有):
    • 删除现有的项目文件绑定,即所有以Scc"开头的属性
    • 删除与项目文件(如果有)相同目录中的文件(projectName).vspscc

验证步骤与源代码控制新项目"部分完全相同.

Verification steps are exactly the same as in "Source-controlling new projects" section.

如果您的项目已经使用此处讨论的技术绑定到 Perforce,并且您想在不同的解决方案(新分支、重用项目的替代解决方案等)中使用它们,请使用以下步骤:

If you have projects that have already been bound to Perforce using the technique discussed here and you want to use them in a different solution (new branch, alternative solution reusing the project, etc) use the following steps:

  1. 将项目集成到所需位置
  2. 在 Visual Studio 中打开源代码控制的解决方案
  3. 文件"->添加"->现有项目..."-通过集成浏览到在步骤 1 中创建的项目
  4. 查看"->待签到"->签入"-添加描述并提交

总结

  • 源代码控制绑定信息存储在解决方案和项目中,必须同步(如果不同步,Visual Studio 将尝试修复任何差异)
  • 我总是将项目文件视为绑定信息的主要来源,将解决方案文件视为一次性文件,可以通过首先对空白解决方案进行源代码控制然后添加所需项目来轻松重新创建这些文件
  • 解决方案文件应始终具有有效的 SccProvider0SccProjectName0 值(必须使用新版本的 P4SCC 插件手动添加)
  • 项目文件应始终具有有效的 SccProjectName(最好与 SccProjectName0 相同)、SccLocalPathSccProvider 值(也必须手动编辑,因为 P4SCC 默认设置不好)
  • Summary

    • Source control binding information is stored in both solution and projects, must be in sync (if not, Visual Studio will attempt to fix any discrepancies)
    • I always treat project files as the primary source of binding information and solution files as throwaway files that can be recreated easily by first source-controlling a blank solution and then adding desired projects
    • Solution file should always have valid SccProvider0 and SccProjectName0 values (have to be added manually with new versions of P4SCC plugins)
    • Project files should always have valid SccProjectName (preferrably same as SccProjectName0), SccLocalPath and SccProvider values (also have to be edited manually as the P4SCC defaults are no good)
    • 我还包括对您原始问题的回答:

      I'm also including answers to your original questions:

      当您执行更改源代码管理"并绑定项目时会发生什么?Visual Studio 如何决定在项目和解决方案文件中放入什么内容?

      What happens when you do "Change source control" and bind projects? How does Visual Studio decide what to put in the project and solution files?

      这会更新您重新绑定的项目文件中的Scc*"元素;然后也更新解决方案文件,使其与项目文件绑定同步

      This updates "Scc*" elements in a project file you're rebinding; the solution file is then updated as well so that it is in sync with the project file bindings

      当您执行从源代码管理打开"时会发生什么?

      What happens when you do "Open from source control"?

      允许您选择要打开的解决方案.之后,解决方案中包含的所有项目都会自动同步到 head.我发现这个功能在 Perforce 世界中不是很有用,在那里你必须创建一个客户端,而且你很可能从 P4V/P4Win/P4 同步这个客户端,而不是依赖 Visual Studio.这在没有视图概念的 Visual Source Safe 世界中很有用,您需要定义存储库在检出时的位置.

      Allows you to pick solution that you'd like to open. Afterwards all the projects included in the solution are automatically synced to head. I find this feature not very useful in Perforce world where you have to create a client anyway and the chances are you're syncing this client from a P4V/P4Win/P4 instead of relying on Visual Studio. This was kind-of useful in Visual Source Safe world where there was no concept of views and you were defining where a repository goes on checkout time.

      SccLocalPath 和 SccProjectFilePathRelativizedFromConnection 所指的连接"文件夹是什么?Visual Studio/Perforce 如何选择它?

      What's this "connection" folder that SccLocalPath and SccProjectFilePathRelativizedFromConnection refer to? How does Visual Studio/Perforce pick it?

      这是 Visual Studio 的簿记.它是根据每个项目文件中的绑定确定的(我猜理论上如果项目文件由于某种原因丢失了绑定信息,它可以从解决方案信息中重建...)

      This is Visual Studio's bookkeeping. It is determined based on bindings in each project file (I guess in theory if a project file loses binding information for some reason, it could be reconstructed from the solution information...)

      是否有一些推荐的方法可以让源代码控制绑定在您创建解决方案的新分支时继续工作?

      Is there some recommended way to make the source control bindings continue to work even when you create a new branch of the solution?

      我希望以上部分能让您了解一种对我来说非常有效的方法:-).

      I hope the sections above give you some idea of a way that is working very well for me :-).

      这篇关于Visual Studio 的源代码控制集成如何与 Perforce 配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆