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

查看:129
本文介绍了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:

  • < SccProjectName>
  • < SccLocalPath>
  • < SccAuxPath>
  • < SccProvider>

有时它们都设置为"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的答案 .显然,有

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 文件是标记有 hidden (隐藏)的标记,因此它们将在文件资源管理器中不可见(除非您覆盖隐藏的文件和文件夹"选项).

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文件后,将其更改为< Scc *>.元素无效.

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. 在提交对话框中,单击
  5. 查看"->待签入"->签入"->,而不要单击提交"按钮,请使用取消".
    原因:签入"操作将创建一个新文件"name.vssscc",然后将"name.sln"和"name.vssscc"都添加到Perforce的默认更改列表中;通过取消提交对话框,我们将使添加"操作处于待处理状态,并且能够在将文件提交到P4之前对其进行编辑
  6. 关闭Visual Studio
  7. 在您最喜欢的编辑器中打开name.sln文件(如果真的很急,请使用记事本:-)),并添加两行( SccProjectName0 SccProvider0 )-空白解决方案文件现在应具有一个源代码管理部分,如下所示:

  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:Perforce\u0020SCM
EndGlobalSection

应选择以下值:

  • SccProjectName0 :一个任意字符串,将在更改源代码控制"对话框中显示为服务器绑定".此名称用于确定哪些项目/解决方案文件可以共享同一源代码管理连接.我建议不要使用空格作为该名称,因为解决方案文件和项目文件中的空格转义规则是不同的.
  • SccProvider0 :硬编码值"MSSCCI:Perforce \ u0020SCM".
  • 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:Perforce\u0020SCM".

您现在可以测试绑定了:

You can now test the bindings:

  1. 确保Visual Studio已关闭
  2. 删除"name.sln"(尤其是"name.suo")以外的所有"**"文件
  3. 打开Visual Studio并使用它打开name.sln
  4. 应出现一个连接对话框,使用适当的端口/客户端/用户,然后单击确定"
  5. 解决方案资源管理器现在应显示带有挂锁覆盖图标的解决方案节点:
  6. 您现在可以使用文件"->源代码管理"->更改源代码管理..."来验证解决方案的源代码管理状态: 注意:服务器绑定"列显示了我们为"SccProjectName0"选择的值.
  1. Make sure Visual Studio is closed
  2. Delete **all* files except the name.sln (especially the name.suo)
  3. Open Visual Studio and use it to open name.sln
  4. A connection dialog should appear, use appropriate port/client/user and click OK
  5. Solution explorer should now display the solution 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...": Note: The column "Server Binding" is showing the value we chose for "SccProjectName0".

源代码控制新项目

如果您要创建一个全新的项目,并想立即在Perforce软件仓库中对其进行跟踪,请按照以下步骤操作:

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(即,导入以前未经源代码控制的项目(Internet下载等)或正在使用不同的源代码控制提供程序(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
  1. Copy the project into appropriate location
  2. Clean-up existing source control bindings (if any):
    • Remove existing project-file bindings, i.e. all properties starting with "Scc"
    • Delete file (projectName).vspscc in the same directory as the project file (if any)

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

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将尝试修复任何差异)
  • 我始终将项目文件视为绑定信息的主要来源,而将解决方案文件视为一次性文件,可以通过首先对空白解决方案进行源控制,然后添加所需的项目来轻松地重新创建这些文件.
  • 解决方案文件应始终具有有效的 SccProvider0 SccProjectName0 值(必须与新版本的P4SCC插件一起手动添加)
  • 项目文件应始终具有有效的 SccProjectName (最好与 SccProjectName0 相同), SccLocalPath SccProvider 值(还必须手动编辑,因为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"?

      允许您选择要打开的解决方案.之后,解决方案中包含的所有项目都将自动同步到开头.我发现此功能在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天全站免登陆