6

I know there are already several questions about renaming files by using a version control system. But I did not found a satisfactory answer to the special version control system Perforce.
My question: Is there a plug-in/solution which keeps the version history of my C# code files when I renaming it within Visual Studio?

Edit: Currently I am using VS2P4 plug-in.

Edit2: I have found a little shorter way to rename a file:

  1. Open containing folder in Explorer (in Solution Explorer right-click Open in Windows Explorer).
  2. Select file and right-click Perforce->Show in P4V.
  3. Do the normal rename/move action with selected file.

That scenario is shorter than navigating in Perforce Depot in P4V until I find the right file. But of course I am looking for a shorter way.

Edit3: Is there a way to directly do "Show in P4V" with file selection within VS?

brgerner
  • 4,287
  • 4
  • 23
  • 38
  • http://www.perforce.com/product/components/visual_studio_plugin looks like they have their own plugin, does this do it? (never used Perforce myself) – akiller Feb 15 '12 at 13:29
  • @aki I have already used P4SCC about 1 year ago with the same lack. – brgerner Feb 15 '12 at 13:36
  • 1
    http://stackoverflow.com/questions/4024807/workflow-for-renaming-a-class-when-using-visual-studio-with-perforce has a few more suggestions for you, doesn't look too straight forward still – akiller Feb 15 '12 at 13:58
  • @a Thank you! I can't believe that the accepted answer is the best possible one. Is it so difficult to write a plug-in which gets file name as parameter and runs the (two?) Perforce commands? Ok if it would be so easy such a plug-in would already exist. But this lack is my main pain while using VS with ReSharper. Normally I would rename classes several times a day. But without the right support I rename types at most half the amount. – brgerner Feb 15 '12 at 14:17
  • @a The difference between my question and the question found by you is that it's enough to me to just rename the file name (not also the class name in one step). That is because I can rename class name by ReSharper without renaming file name. And then I can rename the file name only. This is acceptable. What is _not_ acceptable is that I need to switch to Perforce and navigate to the files to do the renaming. And the second pain is that renaming (file name) and changing (class name within same file) is not possible in one changelist. – brgerner Feb 15 '12 at 14:34

4 Answers4

6

There's a new Visual Studio plugin for Perforce that will be out in beta shortly. It does have support for the built-in Visual Studio rename operation, and also works well with Resharper.

Until it's out I'm afraid the existing solutions are a bit clunky.

randy-wandisco
  • 3,649
  • 16
  • 11
  • You keep me on tenterhooks. How is its name, is there a link? – brgerner Feb 15 '12 at 18:04
  • 1
    When is "shortly"? A week, a month, a year? – brgerner Feb 17 '12 at 15:42
  • The final schedule is still in flux, but we're looking to begin a beta towards the end of Q1. Sorry I can't be more specific. The Engineering team is rolling in support for streams and P4Sandbox so there's quite a lot for a first release. – randy-wandisco Feb 26 '12 at 06:06
  • [Beta announcement as of May 2012](http://forums.perforce.com/index.php?/topic/1585-new-perforce-plugin-for-visual-studio-20121-beta-available-now/) where you can register to download/watch [webinar](http://www.perforce.com/documentation/multimedia-library/webinar/best-tools-2012-increase-productivity-visual-studio-perforce) – KCD Jun 20 '12 at 04:09
1

You can save yourself a step in that process by setting up an external tool in VS (Tools->External Tools) to open perforce to the document you have open. Not ideal, but closer to what you want.

Command: p4v.exe
Arguments: -s $(ItemPath)
Initial Directory: $(ItemDir)
John Koerner
  • 37,428
  • 8
  • 84
  • 134
0

Macro

I have a good solution now. Just handle macro event SolutionItemsEvents_ItemRenamed.
This is done by open Macros IDE by clicking Tools->Macros->Macros IDE. Then add following event handler to your macro project:

    Private Sub SolutionItemsEvents_ItemRenamed(ByVal ProjectItem As EnvDTE.ProjectItem,
                                                ByVal OldName As String)
                Handles SolutionItemsEvents.ItemRenamed

        Dim process As System.Diagnostics.Process

        process.Start("p4", "move -k " & ProjectItem.Properties.Parent.Document.Path &
                      "\\" & OldName & " " & ProjectItem.Document.Path)
    End Sub

See screenshot: enter image description here

As you can see it just calls a "p4 move -k". "-k" option is needed because after a rename the old file is already deleted, so Perforce would throw an error without "-k". "-k" causes Perforce to rename the file on server only.

If you get Perforce connection errors you need to add a P4CONFIG file where connection is defined. This is done by:

  • Add file p4config.txt to the directory of your project (or any of its parent directories) with content:

    P4CLIENT=your workspace
    P4USER=user
    P4PORT=p4server:1234

  • Set environment variable P4CONFIG=p4config.txt

Now you can change your files by any way (Solution Explorer Item->Rename, Solution Explorer Item->F2, ReSharper's Rename file to match type name, ReSharper's class Rename (Ctrl+R,R),...).

But keep in mind: I have problems if I try to edit and rename same file in one chek-in and if I rename a file while another person has checked-out same file.

brgerner
  • 4,287
  • 4
  • 23
  • 38
0

The Visual Studio Plugin provides excellent integration with the Visual Studio. All renaming operations work well and the history is tracked ok.

Dmitry Pavlov
  • 30,789
  • 8
  • 97
  • 121
  • At first moment I was surprised by the hint on this site that P4SCC only supports 32 bit systems. But then I found an answer at http://comments.gmane.org/gmane.comp.version-control.perforce/16459 where is explained that you need to install 32 bit P4SCC on 64 bit systems. – brgerner Feb 23 '12 at 10:20
  • I upgraded to newest P4SCC version (2011.1/389946) and it does not work. When I rename a file the message appears: "Checked out items cannot be renamed by your source control provider. If you continue with the change, FileX.cs will no longer appear to be under source control." – brgerner Feb 23 '12 at 14:30
  • @brgerner consider posting that as a _question_ rather than post issues regarding your system here. please take note of _["Looks like is solved isn't it? As for , that would be a different question, consider posting it separately."](https://meta.stackoverflow.com/a/253829/585968)_ –  Nov 11 '22 at 03:11