Search code examples
version-controlperforcep4v

P4 change ownership through command line


I have a specific use case where I need to create a button (Custom Tools) within Perforce in which it enables the user to right-click a pending changelist with checked out files and it will shelve the files, revert the checked out files, then change the user to a single user and assign the changelist to a specific workspace(The same as the change ownership button)

So far I have set the custom tools to run p4 with the following arguments:

shelve -f -Af -c %P revert -c %P //... change -U **user.name** %P

But this only gets me to shelving the changes, reverting and assigning to a user, I'm missing the workspace change but can't seem to figure this bit out from the docs.

I ran perforce with full logging, which suggested I could run:

p4 user -o **user.name**
p4 spec -o user
p4 client -o **workspace.name**
p4 change -i

But trying to run that locally in a cmd/powershell just outputs the information of the user and workspace.

I am trying to do this to streamline a process as an alternate for manually shelving/unshelving


Solution

  • Use the p4 change -o command to output the current spec, then modify it (you can do this with sed but the --field global option on p4 makes it much easier IMO), and then use p4 change -i to save the modified spec. This is the general flow that you can use to programmatically modify any Perforce spec (clients, streams, etc).

    In this case since you're changing the user you do also need the -U and changelist# arguments on the p4 change -i command.

    C:\Perforce\test>p4 changes -m1
    Change 512 on 2024/03/28 by Samwise@Samwise-dvcs-1509687817 *pending* 'test change'
    
    C:\Perforce\test>p4 --field User=bob --field Client=CLIENT change -o 512 | p4 change -i -U 512
    Change 512 updated.
    
    C:\Perforce\test>p4 changes -m1
    Change 512 on 2024/03/28 by bob@CLIENT *pending* 'test change'