How to keep change history while renaming files in

2019-01-19 22:12发布

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?

4条回答
一夜七次
2楼-- · 2019-01-19 22:39

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.

查看更多
狗以群分
3楼-- · 2019-01-19 22:44

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.

查看更多
不美不萌又怎样
4楼-- · 2019-01-19 22:47

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)
查看更多
Lonely孤独者°
5楼-- · 2019-01-19 22:50

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

查看更多
登录 后发表回答