P4 change ownership through command line

30 views Asked by At

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

2

There are 2 answers

2
Samwise On BEST ANSWER

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'
1
Jase On

If you want to switch your workspace (aka CLIENT in the command line) the easiest way is probably to set the P4CLIENT environment variable, then all your commands will use that new workspace.

Powershell: $env:P4CLIENT = "new_workspace_name"
CMD: set P4CLIENT=new_workspace_name
Bash-type shells: export P4CLIENT=new_workspace_name

The commands you listed above are used to output the actual spec of the workspace or user, not to change which one is being referenced. Hope that helps!