1

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?

John Doe
  • 1,058
  • 8
  • 30

1 Answers1

2

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".

Samwise
  • 68,105
  • 3
  • 30
  • 44
  • The question is related to my [previous question](https://stackoverflow.com/questions/46156741/how-to-detect-if-a-perforce-session-is-opened?noredirect=1#comment79302379_46156741). I know that the executed script is two folders lower to the main folder. So I know the workspace root path, but now we want to collect the version of all files from the root path. That's why I wanted to find the depot of the workspace and then run `p4 files \\depot\...` on the depot to collect the informations. – John Doe Sep 12 '17 at 14:05
  • 1
    Why not just run `p4 files //CLIENTNAME/...` or `p4 files D:\workspace\...`? Again, this assumes you already have valid connection settings (you're not indicating whether this is a script you're running yourself or whether it needs to be able to cope with arbitrarily poorly configured client environments). As I suggested in your previous question, you probably want to be running this as a custom tool if you're running it from P4V, since that'll set up the environment for you. – Samwise Sep 12 '17 at 14:07
  • I tried that earlier but failed with `must create client 'station_name' to acces local files` – John Doe Sep 12 '17 at 14:10
  • 1
    That means you don't have P4CLIENT set up correctly -- you'll need to solve that part of the puzzle before you can go any further. (See part 2 of this answer for how to run `p4 clients` and search for a match -- or run this script as a custom tool and it will work magically.) – Samwise Sep 12 '17 at 14:11
  • Allright, then that would be really simple for me. Thanks again, Sam! I will search on the docs to make the proper config and come back to accept the solution. – John Doe Sep 12 '17 at 14:12
  • I tried to play with the `p4config`s, but I ran in a blocking point. The script which is executed, as you said, `should be able to cope with arbitrarily configured client environments`. The command that I will execute would be `p4 files D:\my_workspace\random_paths\project_known_top_folder`. But in order to be able to execute this command I must set the `P4CLIENT` env variable, which is hard to determine knowing just the path of the workspace. – John Doe Sep 13 '17 at 09:59
  • I think my question now is how to determine the client( or workspace name?) knowing a path? – John Doe Sep 13 '17 at 10:01
  • I broke this answer into three parts. part 1 assumes you know P4PORT and P4CLIENT. Part 2 assumes you know only P4PORT. Part 3 deals with if you don't know P4PORT (at which point it's basically impossible unless you have other specific information about the setup). Did I mention how you should set this up as a custom tool and require it to be run that way so that your script will always just know what P4PORT and P4CLIENT are, rather than trying to make a script that knows how to configure its connection settings out of thin air? – Samwise Sep 13 '17 at 13:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154364/discussion-between-sam-stafford-and-john-doe). – Samwise Sep 13 '17 at 16:25