How p4 recognizes a workspace directory? - perforce

In git the git command (unless explicitly specified) goes up the directory tree starting in current working directory looking for .git directory.
How p4 command recognizes in is started in a directory inside workspace?

p4 uses the P4CLIENT environment variable to determine the current workspace, and the workspace definition on the server includes a Root directory that says where on the client machine the workspace lives. If you only have one workspace on your machine, just do p4 set P4CLIENT=my_client.
If you have multiple workspaces on your machine and don't want to p4 set P4CLIENT each time you switch, you can use P4CONFIG, which allows you to set up per-directory config files:
p4 set P4CONFIG=.p4config
echo P4CLIENT=my_workspace1>~/workspace1/.p4config
echo P4CLIENT=my_workspace2>~/workspace2/.p4config
If P4CONFIG is set, p4 goes up the directory tree from the working directory looking for a file with that name (I've called it .p4config here but it can be anything) and will read P4 environment settings (P4CLIENT, P4PORT, P4USER, etc) from that file, having them take precedence over globally-set variables.
If P4CLIENT is not set in any way, the default value is the client hostname (which itself can be overridden with P4HOST).

Related

perforce workspace command line

I'm trying to understand how perorce work and I'm struggling with command line
I have a single depot with 3 project inside
so depot:
projectA
projectB
now I have 2 workspaces, one mapped only on projectA and one on projectB.
Here I have my trouble , using interface everything works
but with command line I have not enough knoledge on perforce.
I use
p4 -c workspacA //to switch workspace
then I run: p4 cstat
and it give me all the information about changelist contained in all depot
not only depot/projectA
But I woul like to enter in a workspace and run my command only on that workspace
Is this normal? I see that I need to specify my depot mapping but this seems strange to me.
Can someone clarify me this?
Thanks
Do:
p4 set P4CLIENT=workspaceA
If you use -c workspaceA it applies only to that one command, whereas p4 set is persistent.
I also recommend looking at documentation on P4CONFIG, which lets you associate Perforce config settings with local directories (so you switch workspaces automatically when you cd to a different workspace root).
https://www.perforce.com/manuals/v16.2/cmdref/P4CONFIG.html

automatic selection of workspace when launching p4v

In the shell, when I am in a directory located as part of a p4 workspace, I can run p4 commands in the shell and it knows what workspace I am in.
However, when I launch p4v, I have to manually find the workspace that I want to load. Since, the context of p4 commands to run is already known in the shell, how I can I pass that to p4v, so it will launch in the same workspace. There's no reason why I should have to select a workspace when I launch the tool, since the p4 toolchain already can self-determine the context.
I should only have to select the workspace if I am wanting to select some other workspace that I am not currently operating under.
As you've found, P4V has its own settings file and ignores the shell's environment. Per this blog post:
https://www.perforce.com/blog/100114/p4v-secrets-calling-p4v-command-line
the way I'd handle this would be to write a script/alias that wraps P4V and passes the current environment to it. Something along these lines:
$_ = `p4 -Ztag -F "%serverAddress%#%clientName%#%userName%" info`;
#env = split /\#/;
exec "p4v -p $env[0] -c $env[1] -u $env[2]";

Perforce refers to a different root while editing

I have the following perforce client perforce.myClient that contains different directories. When I open a file for edit using /perforce.myClient/p4 edit someFile
it fails with the following message /perforce.myClient/someFile is not under clients root '/my/other/perforce/client'
I did the following to ensure that perforce is picking up the right client
setenv P4CLIENT perforce.myClient
checked to see if the root points to the correct location using p4 client perforce.myClient
How else can I enforce the client?
P4CONFIG files are hugely useful for this. Do this:
p4 set P4CONFIG=.p4config
echo P4CLIENT=perforce.myClient>/perforce.myClient/.p4config
echo P4CLIENT=my.other.perforce.client>/my/other/perforce/client/.p4config
Now your P4CLIENT will change automatically based on your working directory.
http://www.perforce.com/perforce/doc.current/manuals/cmdref/P4CONFIG.html
You should be able to use the global options in your command like
p4 -c myclient edit -c mychangelist //...
Global options appear before the command name.

Perforce - Specify target directory path for sync

Is it possible to specify the folder name where depot needs to be synced ?
I tried syncing the depot to my specified path using following command but it does not work.
p4sync -d "c:\my\path" sync //depot//branch/file
Note : My aim is just to copy the file from depot to my specified location.
Looks like sync is not the command I should be using. Is there any other command or way I can do this ?
sync - gets files from depot to your workspace, which must be already customized. To get files without workspace you need to make some hack with command "print":
p4 print -o filename //path/on/depot/filename
This command (above) gets filename and store it in "filename". Also, you can make some another workaround to get all files from some directory, probably with "p4 -x - command" ( -x stands for xargs in unix-world)

P4 - change workspace for client

I want to change the workspace client using the p4 command line but i don't know if its possible.
You can create, delete or edit workspace using the p4 workspace command, but i want to change the current workspace with a .bat method.
Exemple :
p4 -c MyClient changeworkspace myNewWorkspace
If you know in first step if it's possible.
Thanks.
Either set the P4CLIENT environment variable to the name of the workspace you want to be your current workspace, or consistently pass that workspace name as the value of the -c flag on your p4 commands: http://www.perforce.com/perforce/doc.current/manuals/cmdref/env.P4CLIENT.html#1040647
See also these other ways to set the environment variable (many people find P4CONFIG files helpful): http://www.perforce.com/perforce/doc.current/manuals/p4guide/02_config.html#1069873

Resources