Is there a mapping between a workspace and a depot in Perforce? For example if I have a workspace created on D:/myWorkspace can I run a Perforce command to find the depot from where it is created?
1 Answers
If you have a connection to the Perforce server this is very simple; run a command like p4 where //... or p4 client -o to see the depot(s) associated with the current client workspace.
If you're connected to the Perforce server but those connection settings don't include the name of the workspace that might be associated with the current directory, you'll need to use the p4 clients command and match the Host value to the client hostname, and then look for Root values that match the directory. Note that it's possible to have multiple matches -- people will sometimes do things like create a client with no Host value (allowing it to be used from any host) and/or a null Root value (allowing it to map any directory).
If you don't even know the address of the Perforce server and it's not set in the environment you might be out of luck; Perforce commands will automatically pick up the P4PORT setting from the system environment, the registry, P4CONFIG files, etc, but there isn't a guarantee that any given Perforce client machine will have a connection set up via one of these mechanisms (in the most perverse case, someone might specify the P4PORT on every command via the -p global flag).
When scripting Perforce commands it is generally reasonable to assume a correctly configured environment that includes valid P4PORT/P4USER/P4CLIENT settings, and error out if the user hasn't provided those. A script run from within a shell where the user is using the P4 CLI will already have a correctly configured environment, as will a script run from P4V as a "Custom Tool".