PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 21, 2022

[FIXED] How to keep change history while renaming files in Visual Studio using Perforce

 November 21, 2022     c#, perforce, version-control, visual-studio, visual-studio-2010     No comments   

Issue

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?


Solution

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.



Answered By - brgerner
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing